pub struct Percent(/* private fields */);Expand description
A fixed point representation of a number in the range [0, 1].
Percent
Implementations§
Source§impl Percent
impl Percent
Sourcepub const fn from_parts(parts: u8) -> Self
pub const fn from_parts(parts: u8) -> Self
From an explicitly defined number of parts per maximum of the type.
Sourcepub const fn from_percent(x: u8) -> Self
pub const fn from_percent(x: u8) -> Self
Converts a percent into Self. Equal to x / 100.
This can be created at compile time.
Sourcepub const fn one() -> Self
pub const fn one() -> Self
See PerThing::one
Sourcepub fn is_one(&self) -> bool
pub fn is_one(&self) -> bool
See PerThing::is_one.
Sourcepub const fn zero() -> Self
pub const fn zero() -> Self
See PerThing::zero.
Sourcepub fn is_zero(&self) -> bool
pub fn is_zero(&self) -> bool
See PerThing::is_zero.
Sourcepub const fn deconstruct(self) -> u8
pub const fn deconstruct(self) -> u8
Sourcepub fn square(self) -> Self
pub fn square(self) -> Self
See PerThing::square.
Sourcepub fn from_fraction(x: f64) -> Self
pub fn from_fraction(x: f64) -> Self
Sourcepub fn from_rational_approximation<N>(p: N, q: N) -> Self
pub fn from_rational_approximation<N>(p: N, q: N) -> Self
Sourcepub fn mul_floor<N>(self, b: N) -> N
pub fn mul_floor<N>(self, b: N) -> N
See PerThing::mul_floor.
Sourcepub fn mul_ceil<N>(self, b: N) -> N
pub fn mul_ceil<N>(self, b: N) -> N
See PerThing::mul_ceil.
Sourcepub fn saturating_reciprocal_mul<N>(self, b: N) -> N
pub fn saturating_reciprocal_mul<N>(self, b: N) -> N
Sourcepub fn saturating_reciprocal_mul_floor<N>(self, b: N) -> N
pub fn saturating_reciprocal_mul_floor<N>(self, b: N) -> N
Sourcepub fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N
pub fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N
Trait Implementations§
Source§impl CompactAs for Percent
Implementation makes any compact encoding of PerThing::Inner valid,
when decoding it will saturate up to PerThing::ACCURACY.
impl CompactAs for Percent
Implementation makes any compact encoding of PerThing::Inner valid,
when decoding it will saturate up to PerThing::ACCURACY.
Source§impl Decode for Percent
impl Decode for Percent
Source§impl<'de> Deserialize<'de> for Percent
impl<'de> Deserialize<'de> for Percent
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Encode for Percent
impl Encode for Percent
Source§fn encode_to<__CodecOutputEdqy: Output + ?Sized>(
&self,
__codec_dest_edqy: &mut __CodecOutputEdqy,
)
fn encode_to<__CodecOutputEdqy: Output + ?Sized>( &self, __codec_dest_edqy: &mut __CodecOutputEdqy, )
Source§fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R
Source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
Source§fn encoded_size(&self) -> usize
fn encoded_size(&self) -> usize
Source§impl<N> Mul<N> for Percent
Non-overflow multiplication.
impl<N> Mul<N> for Percent
Non-overflow multiplication.
This is tailored to be used with a balance type.
Source§impl Ord for Percent
impl Ord for Percent
Source§impl PartialOrd for Percent
impl PartialOrd for Percent
Source§impl PerThing for Percent
impl PerThing for Percent
Source§fn deconstruct(self) -> Self::Inner
fn deconstruct(self) -> Self::Inner
Consume self and return the number of parts per thing.
Source§fn from_parts(parts: Self::Inner) -> Self
fn from_parts(parts: Self::Inner) -> Self
Build this type from a number of parts per thing.
Source§fn from_fraction(x: f64) -> Self
fn from_fraction(x: f64) -> Self
NOTE: saturate to 0 or 1 if x is beyond [0, 1]
Source§const ACCURACY: Self::Inner = {transmute(0x64): <per_things::Percent as per_things::PerThing>::Inner}
const ACCURACY: Self::Inner = {transmute(0x64): <per_things::Percent as per_things::PerThing>::Inner}
Source§type Upper = u16
type Upper = u16
Self::Inner, used to avoid overflow in some computations.
It must be able to compute ACCURACY^2.Source§fn from_rational_approximation<N>(p: N, q: N) -> Self
fn from_rational_approximation<N>(p: N, q: N) -> Self
p/q into a per-thing fraction. This will never overflow. Read moreSource§fn from_percent(x: Self::Inner) -> Self
fn from_percent(x: Self::Inner) -> Self
Self::from_parts(x * Self::ACCURACY / 100)
but more accurate and can cope with potential type overflows.Source§fn mul_floor<N>(self, b: N) -> N
fn mul_floor<N>(self, b: N) -> N
Mul rounds to the
nearest whole number. Read moreSource§fn mul_ceil<N>(self, b: N) -> N
fn mul_ceil<N>(self, b: N) -> N
Mul
rounds to the nearest whole number. Read moreSource§fn saturating_reciprocal_mul<N>(self, b: N) -> N
fn saturating_reciprocal_mul<N>(self, b: N) -> N
self. The result is rounded to the
nearest whole number and saturates at the numeric bounds instead of overflowing. Read moreSource§fn saturating_reciprocal_mul_floor<N>(self, b: N) -> N
fn saturating_reciprocal_mul_floor<N>(self, b: N) -> N
self. The result is rounded down to the
nearest whole number and saturates at the numeric bounds instead of overflowing. Read moreSource§fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N
fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N
self. The result is rounded up to the
nearest whole number and saturates at the numeric bounds instead of overflowing. Read moreSource§impl Saturating for Percent
impl Saturating for Percent
Source§fn saturating_add(self, rhs: Self) -> Self
fn saturating_add(self, rhs: Self) -> Self
Saturating addition. Compute self + rhs, saturating at the numeric bounds instead of
overflowing. This operation is lossless if it does not saturate.
Source§fn saturating_sub(self, rhs: Self) -> Self
fn saturating_sub(self, rhs: Self) -> Self
Saturating subtraction. Compute self - rhs, saturating at the numeric bounds instead of
overflowing. This operation is lossless if it does not saturate.
Source§fn saturating_mul(self, rhs: Self) -> Self
fn saturating_mul(self, rhs: Self) -> Self
Saturating multiply. Compute self * rhs, saturating at the numeric bounds instead of
overflowing. This operation is lossy.
Source§fn saturating_pow(self, exp: usize) -> Self
fn saturating_pow(self, exp: usize) -> Self
Saturating exponentiation. Computes self.pow(exp), saturating at the numeric
bounds instead of overflowing. This operation is lossy.
impl Copy for Percent
impl EncodeLike for Percent
impl Eq for Percent
impl StructuralPartialEq for Percent
Auto Trait Implementations§
impl Freeze for Percent
impl RefUnwindSafe for Percent
impl Send for Percent
impl Sync for Percent
impl Unpin for Percent
impl UnwindSafe for Percent
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DecodeLimit for Twhere
T: Decode,
impl<T> DecodeLimit for Twhere
T: Decode,
Source§impl<T> HasCompact for T
impl<T> HasCompact for T
Source§impl<T> LowerBounded for Twhere
T: Bounded,
impl<T> LowerBounded for Twhere
T: Bounded,
Source§impl<T> SaturatedConversion for T
impl<T> SaturatedConversion for T
Source§fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
Source§fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
T. Read moreSource§impl<T, S> UniqueSaturatedFrom<T> for S
impl<T, S> UniqueSaturatedFrom<T> for S
Source§fn unique_saturated_from(t: T) -> S
fn unique_saturated_from(t: T) -> S
T into an equivalent instance of Self.Source§impl<T, S> UniqueSaturatedInto<T> for S
impl<T, S> UniqueSaturatedInto<T> for S
Source§fn unique_saturated_into(self) -> T
fn unique_saturated_into(self) -> T
T.