Trait sp_arithmetic::per_things::PerThing
source · [−]pub trait PerThing: Sized + Saturating + Copy + Default + Eq + PartialEq + Ord + PartialOrd + Bounded + Debug + Div<Output = Self> + Mul<Output = Self> + Pow<usize, Output = Self> {
type Inner: BaseArithmetic + Unsigned + Copy + Into<u128> + Debug;
type Upper: BaseArithmetic + Copy + From<Self::Inner> + TryInto<Self::Inner> + UniqueSaturatedInto<Self::Inner> + Unsigned + Debug;
const ACCURACY: Self::Inner;
Show 18 methods
fn deconstruct(self) -> Self::Inner;
fn from_parts(parts: Self::Inner) -> Self;
fn from_float(x: f64) -> Self;
fn from_rational<N>(p: N, q: N) -> Self
where
N: Clone + Ord + TryInto<Self::Inner> + TryInto<Self::Upper> + Div<N, Output = N> + Rem<N, Output = N> + Add<N, Output = N> + Unsigned,
Self::Inner: Into<N>;
fn zero() -> Self { ... }
fn is_zero(&self) -> bool { ... }
fn one() -> Self { ... }
fn is_one(&self) -> bool { ... }
fn from_percent(x: Self::Inner) -> Self { ... }
fn square(self) -> Self { ... }
fn left_from_one(self) -> Self { ... }
fn mul_floor<N>(self, b: N) -> N
where
N: Clone + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Unsigned,
Self::Inner: Into<N>,
{ ... }
fn mul_ceil<N>(self, b: N) -> N
where
N: Clone + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Unsigned,
Self::Inner: Into<N>,
{ ... }
fn saturating_reciprocal_mul<N>(self, b: N) -> N
where
N: Clone + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Saturating + Unsigned,
Self::Inner: Into<N>,
{ ... }
fn saturating_reciprocal_mul_floor<N>(self, b: N) -> N
where
N: Clone + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Saturating + Unsigned,
Self::Inner: Into<N>,
{ ... }
fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N
where
N: Clone + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Saturating + Unsigned,
Self::Inner: Into<N>,
{ ... }
fn from_fraction(x: f64) -> Self { ... }
fn from_rational_approximation<N>(p: N, q: N) -> Self
where
N: Clone + Ord + TryInto<Self::Inner> + TryInto<Self::Upper> + Div<N, Output = N> + Rem<N, Output = N> + Add<N, Output = N> + Unsigned + Zero + One,
Self::Inner: Into<N>,
{ ... }
}
Expand description
Something that implements a fixed point ration with an arbitrary granularity X
, as parts per
X
.
Associated Types
The data type used to build this per-thingy.
Associated Constants
Required methods
fn deconstruct(self) -> Self::Inner
fn deconstruct(self) -> Self::Inner
Consume self and return the number of parts per thing.
fn from_parts(parts: Self::Inner) -> Self
fn from_parts(parts: Self::Inner) -> Self
Build this type from a number of parts per thing.
fn from_float(x: f64) -> Self
fn from_float(x: f64) -> Self
Converts a fraction into Self
.
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(989u64, 1000),
Percent::from_parts(98),
);
Provided methods
fn from_percent(x: Self::Inner) -> Self
fn from_percent(x: Self::Inner) -> Self
Build this type from a percent. Equivalent to Self::from_parts(x * Self::ACCURACY / 100)
but more accurate and can cope with potential type overflows.
fn left_from_one(self) -> Self
fn left_from_one(self) -> Self
Return the part left when self
is saturating-subtracted from Self::one()
.
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);
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);
fn saturating_reciprocal_mul<N>(self, b: N) -> N where
N: Clone + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Saturating + Unsigned,
Self::Inner: Into<N>,
fn saturating_reciprocal_mul<N>(self, b: N) -> N where
N: Clone + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Saturating + Unsigned,
Self::Inner: Into<N>,
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);
fn saturating_reciprocal_mul_floor<N>(self, b: N) -> N where
N: Clone + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Saturating + Unsigned,
Self::Inner: Into<N>,
fn saturating_reciprocal_mul_floor<N>(self, b: N) -> N where
N: Clone + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Saturating + Unsigned,
Self::Inner: Into<N>,
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);
fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N where
N: Clone + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Saturating + Unsigned,
Self::Inner: Into<N>,
fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N where
N: Clone + UniqueSaturatedInto<Self::Inner> + Rem<N, Output = N> + Div<N, Output = N> + Mul<N, Output = N> + Add<N, Output = N> + Saturating + Unsigned,
Self::Inner: Into<N>,
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);
Use from_rational instead
Same as Self::from_rational
.