Trait sp_runtime::PerThing [−][src]
Re-export top-level arithmetic stuff.
Something that implements a fixed point ration with an arbitrary granularity X
, as parts per
X
.
Associated Types
type Inner: BaseArithmetic + Copy + Debug + Unsigned
[src]
The data type used to build this per-thingy.
type Upper: BaseArithmetic + UniqueSaturatedInto<Self::Inner> + From<Self::Inner> + TryInto<Self::Inner> + Copy + Debug + Unsigned
[src]
A data type larger than Self::Inner
, used to avoid overflow in some computations.
It must be able to compute ACCURACY^2
.
Associated Constants
Loading content...Required methods
pub fn deconstruct(self) -> Self::Inner
[src]
Consume self and return the number of parts per thing.
pub fn from_parts(parts: Self::Inner) -> Self
[src]
Build this type from a number of parts per thing.
pub fn from_fraction(x: f64) -> Self
[src]
Converts a fraction into Self
.
pub fn from_rational_approximation<N>(p: N, q: N) -> Self where
N: Clone + Ord + From<Self::Inner> + TryInto<Self::Inner> + TryInto<Self::Upper> + Div<N, Output = N> + Rem<N, Output = N> + Add<N, Output = N> + Unsigned,
[src]
N: Clone + Ord + From<Self::Inner> + TryInto<Self::Inner> + TryInto<Self::Upper> + Div<N, Output = N> + Rem<N, Output = N> + Add<N, Output = N> + Unsigned,
Approximate the fraction p/q
into a per-thing fraction. This will never overflow.
The computation of this approximation is performed in the generic type N
. Given
M
as the data type that can hold the maximum value of this per-thing (e.g. u32 for
perbill), this can only work if N == M
or N: From<M> + TryInto<M>
.
Note that this always rounds down, i.e.
// 989/100 is technically closer to 99%. assert_eq!( Percent::from_rational_approximation(989u64, 1000), Percent::from_parts(98), );
Provided methods
pub fn zero() -> Self
[src]
Equivalent to Self::from_parts(0)
.
pub fn is_zero(&self) -> bool
[src]
Return true
if this is nothing.
pub fn one() -> Self
[src]
Equivalent to Self::from_parts(Self::ACCURACY)
.
pub fn is_one(&self) -> bool
[src]
Return true
if this is one.
pub fn from_percent(x: Self::Inner) -> Self
[src]
Build this type from a percent. Equivalent to Self::from_parts(x * Self::ACCURACY / 100)
but more accurate.
pub fn square(self) -> Self
[src]
Return the product of multiplication of this value by itself.
pub fn mul_floor<N>(self, b: N) -> N where
N: Clone + From<Self::Inner> + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Unsigned,
[src]
N: Clone + From<Self::Inner> + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Unsigned,
Multiplication that always rounds down to a whole number. The standard Mul
rounds to the
nearest whole number.
// round to nearest assert_eq!(Percent::from_percent(34) * 10u64, 3); assert_eq!(Percent::from_percent(36) * 10u64, 4); // round down assert_eq!(Percent::from_percent(34).mul_floor(10u64), 3); assert_eq!(Percent::from_percent(36).mul_floor(10u64), 3);
pub fn mul_ceil<N>(self, b: N) -> N where
N: Clone + From<Self::Inner> + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Unsigned,
[src]
N: Clone + From<Self::Inner> + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Unsigned,
Multiplication that always rounds the result up to a whole number. The standard Mul
rounds to the nearest whole number.
// round to nearest assert_eq!(Percent::from_percent(34) * 10u64, 3); assert_eq!(Percent::from_percent(36) * 10u64, 4); // round up assert_eq!(Percent::from_percent(34).mul_ceil(10u64), 4); assert_eq!(Percent::from_percent(36).mul_ceil(10u64), 4);
pub fn saturating_reciprocal_mul<N>(self, b: N) -> N where
N: Clone + From<Self::Inner> + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Saturating + Unsigned,
[src]
N: Clone + From<Self::Inner> + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Saturating + Unsigned,
Saturating multiplication by the reciprocal of self
. The result is rounded to the
nearest whole number and saturates at the numeric bounds instead of overflowing.
assert_eq!(Percent::from_percent(50).saturating_reciprocal_mul(10u64), 20);
pub fn saturating_reciprocal_mul_floor<N>(self, b: N) -> N where
N: Clone + From<Self::Inner> + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Saturating + Unsigned,
[src]
N: Clone + From<Self::Inner> + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Saturating + Unsigned,
Saturating multiplication by the reciprocal of self
. The result is rounded down to the
nearest whole number and saturates at the numeric bounds instead of overflowing.
// round to nearest assert_eq!(Percent::from_percent(60).saturating_reciprocal_mul(10u64), 17); // round down assert_eq!(Percent::from_percent(60).saturating_reciprocal_mul_floor(10u64), 16);
pub fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N where
N: Clone + From<Self::Inner> + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Saturating + Unsigned,
[src]
N: Clone + From<Self::Inner> + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Saturating + Unsigned,
Saturating multiplication by the reciprocal of self
. The result is rounded up to the
nearest whole number and saturates at the numeric bounds instead of overflowing.
// round to nearest assert_eq!(Percent::from_percent(61).saturating_reciprocal_mul(10u64), 16); // round up assert_eq!(Percent::from_percent(61).saturating_reciprocal_mul_ceil(10u64), 17);
Implementors
impl PerThing for PerU16
[src]
type Inner = u16
type Upper = u32
pub const ACCURACY: <PerU16 as PerThing>::Inner
[src]
pub fn deconstruct(self) -> <PerU16 as PerThing>::Inner
[src]
Consume self and return the number of parts per thing.
pub fn from_parts(parts: <PerU16 as PerThing>::Inner) -> PerU16
[src]
Build this type from a number of parts per thing.
pub fn from_fraction(x: f64) -> PerU16
[src]
NOTE: saturate to 0 or 1 if x is beyond [0, 1]
pub fn from_rational_approximation<N>(p: N, q: N) -> PerU16 where
N: Clone + Ord + From<<PerU16 as PerThing>::Inner> + TryInto<<PerU16 as PerThing>::Inner> + TryInto<<PerU16 as PerThing>::Upper> + Div<N, Output = N> + Rem<N, Output = N> + Add<N, Output = N> + Unsigned,
[src]
N: Clone + Ord + From<<PerU16 as PerThing>::Inner> + TryInto<<PerU16 as PerThing>::Inner> + TryInto<<PerU16 as PerThing>::Upper> + Div<N, Output = N> + Rem<N, Output = N> + Add<N, Output = N> + Unsigned,
impl PerThing for Perbill
[src]
type Inner = u32
type Upper = u64
pub const ACCURACY: <Perbill as PerThing>::Inner
[src]
pub fn deconstruct(self) -> <Perbill as PerThing>::Inner
[src]
Consume self and return the number of parts per thing.
pub fn from_parts(parts: <Perbill as PerThing>::Inner) -> Perbill
[src]
Build this type from a number of parts per thing.
pub fn from_fraction(x: f64) -> Perbill
[src]
NOTE: saturate to 0 or 1 if x is beyond [0, 1]
pub fn from_rational_approximation<N>(p: N, q: N) -> Perbill where
N: Clone + Ord + From<<Perbill as PerThing>::Inner> + TryInto<<Perbill as PerThing>::Inner> + TryInto<<Perbill as PerThing>::Upper> + Div<N, Output = N> + Rem<N, Output = N> + Add<N, Output = N> + Unsigned,
[src]
N: Clone + Ord + From<<Perbill as PerThing>::Inner> + TryInto<<Perbill as PerThing>::Inner> + TryInto<<Perbill as PerThing>::Upper> + Div<N, Output = N> + Rem<N, Output = N> + Add<N, Output = N> + Unsigned,
impl PerThing for Percent
[src]
type Inner = u8
type Upper = u16
pub const ACCURACY: <Percent as PerThing>::Inner
[src]
pub fn deconstruct(self) -> <Percent as PerThing>::Inner
[src]
Consume self and return the number of parts per thing.
pub fn from_parts(parts: <Percent as PerThing>::Inner) -> Percent
[src]
Build this type from a number of parts per thing.
pub fn from_fraction(x: f64) -> Percent
[src]
NOTE: saturate to 0 or 1 if x is beyond [0, 1]
pub fn from_rational_approximation<N>(p: N, q: N) -> Percent where
N: Clone + Ord + From<<Percent as PerThing>::Inner> + TryInto<<Percent as PerThing>::Inner> + TryInto<<Percent as PerThing>::Upper> + Div<N, Output = N> + Rem<N, Output = N> + Add<N, Output = N> + Unsigned,
[src]
N: Clone + Ord + From<<Percent as PerThing>::Inner> + TryInto<<Percent as PerThing>::Inner> + TryInto<<Percent as PerThing>::Upper> + Div<N, Output = N> + Rem<N, Output = N> + Add<N, Output = N> + Unsigned,
impl PerThing for Permill
[src]
type Inner = u32
type Upper = u64
pub const ACCURACY: <Permill as PerThing>::Inner
[src]
pub fn deconstruct(self) -> <Permill as PerThing>::Inner
[src]
Consume self and return the number of parts per thing.
pub fn from_parts(parts: <Permill as PerThing>::Inner) -> Permill
[src]
Build this type from a number of parts per thing.
pub fn from_fraction(x: f64) -> Permill
[src]
NOTE: saturate to 0 or 1 if x is beyond [0, 1]
pub fn from_rational_approximation<N>(p: N, q: N) -> Permill where
N: Clone + Ord + From<<Permill as PerThing>::Inner> + TryInto<<Permill as PerThing>::Inner> + TryInto<<Permill as PerThing>::Upper> + Div<N, Output = N> + Rem<N, Output = N> + Add<N, Output = N> + Unsigned,
[src]
N: Clone + Ord + From<<Permill as PerThing>::Inner> + TryInto<<Permill as PerThing>::Inner> + TryInto<<Permill as PerThing>::Upper> + Div<N, Output = N> + Rem<N, Output = N> + Add<N, Output = N> + Unsigned,
impl PerThing for Perquintill
[src]
type Inner = u64
type Upper = u128
pub const ACCURACY: <Perquintill as PerThing>::Inner
[src]
pub fn deconstruct(self) -> <Perquintill as PerThing>::Inner
[src]
Consume self and return the number of parts per thing.
pub fn from_parts(parts: <Perquintill as PerThing>::Inner) -> Perquintill
[src]
Build this type from a number of parts per thing.
pub fn from_fraction(x: f64) -> Perquintill
[src]
NOTE: saturate to 0 or 1 if x is beyond [0, 1]
pub fn from_rational_approximation<N>(p: N, q: N) -> Perquintill where
N: Clone + Ord + From<<Perquintill as PerThing>::Inner> + TryInto<<Perquintill as PerThing>::Inner> + TryInto<<Perquintill as PerThing>::Upper> + Div<N, Output = N> + Rem<N, Output = N> + Add<N, Output = N> + Unsigned,
[src]
N: Clone + Ord + From<<Perquintill as PerThing>::Inner> + TryInto<<Perquintill as PerThing>::Inner> + TryInto<<Perquintill as PerThing>::Upper> + Div<N, Output = N> + Rem<N, Output = N> + Add<N, Output = N> + Unsigned,