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 23 methods
fn deconstruct(self) -> Self::Inner;
fn from_parts(parts: Self::Inner) -> Self;
fn from_float(x: f64) -> Self;
fn from_rational_with_rounding<N>(
p: N,
q: N,
rounding: Rounding
) -> Result<Self, ()>
where
N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
Self::Inner: Into<N>;
fn zero() -> Self { ... }
fn is_zero(&self) -> bool { ... }
fn one() -> Self { ... }
fn is_one(&self) -> bool { ... }
fn less_epsilon(self) -> Self { ... }
fn try_less_epsilon(self) -> Result<Self, Self> { ... }
fn plus_epsilon(self) -> Self { ... }
fn try_plus_epsilon(self) -> Result<Self, Self> { ... }
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: MultiplyArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{ ... }
fn mul_ceil<N>(self, b: N) -> N
where
N: MultiplyArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{ ... }
fn saturating_reciprocal_mul<N>(self, b: N) -> N
where
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{ ... }
fn saturating_reciprocal_mul_floor<N>(self, b: N) -> N
where
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{ ... }
fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N
where
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{ ... }
fn from_fraction(x: f64) -> Self { ... }
fn from_rational<N>(p: N, q: N) -> Self
where
N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
Self::Inner: Into<N>,
{ ... }
fn from_rational_approximation<N>(p: N, q: N) -> Self
where
N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
Self::Inner: Into<N>,
{ ... }
}Expand description
Something that implements a fixed point ration with an arbitrary granularity X, as parts per
X.
Required Associated Types§
Required Associated Constants§
Required Methods§
sourcefn deconstruct(self) -> Self::Inner
fn deconstruct(self) -> Self::Inner
Consume self and return the number of parts per thing.
sourcefn from_parts(parts: Self::Inner) -> Self
fn from_parts(parts: Self::Inner) -> Self
Build this type from a number of parts per thing.
sourcefn from_float(x: f64) -> Self
fn from_float(x: f64) -> Self
Converts a fraction into Self.
sourcefn from_rational_with_rounding<N>(
p: N,
q: N,
rounding: Rounding
) -> Result<Self, ()>where
N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
Self::Inner: Into<N>,
fn from_rational_with_rounding<N>(
p: N,
q: N,
rounding: Rounding
) -> Result<Self, ()>where
N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
Self::Inner: Into<N>,
Approximate the fraction p/q into a per-thing fraction.
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>.
In the case of an overflow (or divide by zero), an Err is returned.
Rounding is determined by the parameter rounding, i.e.
// 989/100 is technically closer to 99%.
assert_eq!(
Percent::from_rational_with_rounding(989u64, 1000, Down).unwrap(),
Percent::from_parts(98),
);
assert_eq!(
Percent::from_rational_with_rounding(984u64, 1000, NearestPrefUp).unwrap(),
Percent::from_parts(98),
);
assert_eq!(
Percent::from_rational_with_rounding(985u64, 1000, NearestPrefDown).unwrap(),
Percent::from_parts(98),
);
assert_eq!(
Percent::from_rational_with_rounding(985u64, 1000, NearestPrefUp).unwrap(),
Percent::from_parts(99),
);
assert_eq!(
Percent::from_rational_with_rounding(986u64, 1000, NearestPrefDown).unwrap(),
Percent::from_parts(99),
);
assert_eq!(
Percent::from_rational_with_rounding(981u64, 1000, Up).unwrap(),
Percent::from_parts(99),
);
assert_eq!(
Percent::from_rational_with_rounding(1001u64, 1000, Up),
Err(()),
);assert_eq!(
Percent::from_rational_with_rounding(981u64, 1000, Up).unwrap(),
Percent::from_parts(99),
);Provided Methods§
sourcefn is_zero(&self) -> bool
fn is_zero(&self) -> bool
Return true if this is nothing.
Examples found in repository?
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
fn less_epsilon(self) -> Self {
if self.is_zero() {
return self
}
Self::from_parts(self.deconstruct() - One::one())
}
/// Return the next lower value to `self` or an error with the same value if `self` is already
/// zero.
fn try_less_epsilon(self) -> Result<Self, Self> {
if self.is_zero() {
return Err(self)
}
Ok(Self::from_parts(self.deconstruct() - One::one()))
}sourcefn one() -> Self
fn one() -> Self
Equivalent to Self::from_parts(Self::ACCURACY).
Examples found in repository?
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
fn left_from_one(self) -> Self {
Self::one().saturating_sub(self)
}
/// Multiplication that always rounds down to a whole number. The standard `Mul` rounds to the
/// nearest whole number.
///
/// ```rust
/// # use sp_arithmetic::{Percent, PerThing};
/// # fn main () {
/// // 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);
/// # }
/// ```
fn mul_floor<N>(self, b: N) -> N
where
N: MultiplyArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{
overflow_prune_mul::<N, Self>(b, self.deconstruct(), Rounding::Down)
}
/// Multiplication that always rounds the result up to a whole number. The standard `Mul`
/// rounds to the nearest whole number.
///
/// ```rust
/// # use sp_arithmetic::{Percent, PerThing};
/// # fn main () {
/// // 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 mul_ceil<N>(self, b: N) -> N
where
N: MultiplyArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{
overflow_prune_mul::<N, Self>(b, self.deconstruct(), Rounding::Up)
}
/// 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.
///
/// ```rust
/// # use sp_arithmetic::{Percent, PerThing};
/// # fn main () {
/// assert_eq!(Percent::from_percent(50).saturating_reciprocal_mul(10u64), 20);
/// # }
/// ```
fn saturating_reciprocal_mul<N>(self, b: N) -> N
where
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{
saturating_reciprocal_mul::<N, Self>(b, self.deconstruct(), Rounding::NearestPrefUp)
}
/// 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.
///
/// ```rust
/// # use sp_arithmetic::{Percent, PerThing};
/// # fn main () {
/// // 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_floor<N>(self, b: N) -> N
where
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{
saturating_reciprocal_mul::<N, Self>(b, self.deconstruct(), Rounding::Down)
}
/// 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.
///
/// ```rust
/// # use sp_arithmetic::{Percent, PerThing};
/// # fn main () {
/// // 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);
/// # }
/// ```
fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N
where
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{
saturating_reciprocal_mul::<N, Self>(b, self.deconstruct(), Rounding::Up)
}
/// Consume self and return the number of parts per thing.
fn deconstruct(self) -> Self::Inner;
/// Build this type from a number of parts per thing.
fn from_parts(parts: Self::Inner) -> Self;
/// Converts a fraction into `Self`.
#[cfg(feature = "std")]
fn from_float(x: f64) -> Self;
/// Same as `Self::from_float`.
#[deprecated = "Use from_float instead"]
#[cfg(feature = "std")]
fn from_fraction(x: f64) -> Self {
Self::from_float(x)
}
/// 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.
///
/// ```rust
/// # use sp_arithmetic::{Percent, PerThing};
/// # fn main () {
/// // 989/1000 is technically closer to 99%.
/// assert_eq!(
/// Percent::from_rational(989u64, 1000),
/// Percent::from_parts(98),
/// );
/// # }
/// ```
fn from_rational<N>(p: N, q: N) -> Self
where
N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
Self::Inner: Into<N>,
{
Self::from_rational_with_rounding(p, q, Rounding::Down).unwrap_or_else(|_| Self::one())
}sourcefn is_one(&self) -> bool
fn is_one(&self) -> bool
Return true if this is one.
Examples found in repository?
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
fn plus_epsilon(self) -> Self {
if self.is_one() {
return self
}
Self::from_parts(self.deconstruct() + One::one())
}
/// Return the next higher value to `self` or an error with the same value if `self` is already
/// one.
fn try_plus_epsilon(self) -> Result<Self, Self> {
if self.is_one() {
return Err(self)
}
Ok(Self::from_parts(self.deconstruct() + One::one()))
}sourcefn less_epsilon(self) -> Self
fn less_epsilon(self) -> Self
Return the next lower value to self or self if it is already zero.
sourcefn try_less_epsilon(self) -> Result<Self, Self>
fn try_less_epsilon(self) -> Result<Self, Self>
Return the next lower value to self or an error with the same value if self is already
zero.
sourcefn plus_epsilon(self) -> Self
fn plus_epsilon(self) -> Self
Return the next higher value to self or self if it is already one.
sourcefn try_plus_epsilon(self) -> Result<Self, Self>
fn try_plus_epsilon(self) -> Result<Self, Self>
Return the next higher value to self or an error with the same value if self is already
one.
sourcefn 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.
sourcefn left_from_one(self) -> Self
fn left_from_one(self) -> Self
Return the part left when self is saturating-subtracted from Self::one().
sourcefn mul_floor<N>(self, b: N) -> Nwhere
N: MultiplyArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
fn mul_floor<N>(self, b: N) -> Nwhere
N: MultiplyArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
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);sourcefn mul_ceil<N>(self, b: N) -> Nwhere
N: MultiplyArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
fn mul_ceil<N>(self, b: N) -> Nwhere
N: MultiplyArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
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);sourcefn saturating_reciprocal_mul<N>(self, b: N) -> Nwhere
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
fn saturating_reciprocal_mul<N>(self, b: N) -> Nwhere
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
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);sourcefn saturating_reciprocal_mul_floor<N>(self, b: N) -> Nwhere
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
fn saturating_reciprocal_mul_floor<N>(self, b: N) -> Nwhere
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
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);sourcefn saturating_reciprocal_mul_ceil<N>(self, b: N) -> Nwhere
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> Nwhere
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
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);sourcefn from_rational<N>(p: N, q: N) -> Selfwhere
N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
Self::Inner: Into<N>,
fn from_rational<N>(p: N, q: N) -> Selfwhere
N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
Self::Inner: Into<N>,
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/1000 is technically closer to 99%.
assert_eq!(
Percent::from_rational(989u64, 1000),
Percent::from_parts(98),
);Examples found in repository?
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
fn from_percent(x: Self::Inner) -> Self {
let a: Self::Inner = x.min(100.into());
let b: Self::Inner = 100.into();
Self::from_rational::<Self::Inner>(a, b)
}
/// Return the product of multiplication of this value by itself.
fn square(self) -> Self {
let p = Self::Upper::from(self.deconstruct());
let q = Self::Upper::from(Self::ACCURACY);
Self::from_rational::<Self::Upper>(p * p, q * q)
}
/// Return the part left when `self` is saturating-subtracted from `Self::one()`.
fn left_from_one(self) -> Self {
Self::one().saturating_sub(self)
}
/// Multiplication that always rounds down to a whole number. The standard `Mul` rounds to the
/// nearest whole number.
///
/// ```rust
/// # use sp_arithmetic::{Percent, PerThing};
/// # fn main () {
/// // 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);
/// # }
/// ```
fn mul_floor<N>(self, b: N) -> N
where
N: MultiplyArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{
overflow_prune_mul::<N, Self>(b, self.deconstruct(), Rounding::Down)
}
/// Multiplication that always rounds the result up to a whole number. The standard `Mul`
/// rounds to the nearest whole number.
///
/// ```rust
/// # use sp_arithmetic::{Percent, PerThing};
/// # fn main () {
/// // 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 mul_ceil<N>(self, b: N) -> N
where
N: MultiplyArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{
overflow_prune_mul::<N, Self>(b, self.deconstruct(), Rounding::Up)
}
/// 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.
///
/// ```rust
/// # use sp_arithmetic::{Percent, PerThing};
/// # fn main () {
/// assert_eq!(Percent::from_percent(50).saturating_reciprocal_mul(10u64), 20);
/// # }
/// ```
fn saturating_reciprocal_mul<N>(self, b: N) -> N
where
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{
saturating_reciprocal_mul::<N, Self>(b, self.deconstruct(), Rounding::NearestPrefUp)
}
/// 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.
///
/// ```rust
/// # use sp_arithmetic::{Percent, PerThing};
/// # fn main () {
/// // 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_floor<N>(self, b: N) -> N
where
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{
saturating_reciprocal_mul::<N, Self>(b, self.deconstruct(), Rounding::Down)
}
/// 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.
///
/// ```rust
/// # use sp_arithmetic::{Percent, PerThing};
/// # fn main () {
/// // 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);
/// # }
/// ```
fn saturating_reciprocal_mul_ceil<N>(self, b: N) -> N
where
N: ReciprocalArg + UniqueSaturatedInto<Self::Inner>,
Self::Inner: Into<N>,
{
saturating_reciprocal_mul::<N, Self>(b, self.deconstruct(), Rounding::Up)
}
/// Consume self and return the number of parts per thing.
fn deconstruct(self) -> Self::Inner;
/// Build this type from a number of parts per thing.
fn from_parts(parts: Self::Inner) -> Self;
/// Converts a fraction into `Self`.
#[cfg(feature = "std")]
fn from_float(x: f64) -> Self;
/// Same as `Self::from_float`.
#[deprecated = "Use from_float instead"]
#[cfg(feature = "std")]
fn from_fraction(x: f64) -> Self {
Self::from_float(x)
}
/// 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.
///
/// ```rust
/// # use sp_arithmetic::{Percent, PerThing};
/// # fn main () {
/// // 989/1000 is technically closer to 99%.
/// assert_eq!(
/// Percent::from_rational(989u64, 1000),
/// Percent::from_parts(98),
/// );
/// # }
/// ```
fn from_rational<N>(p: N, q: N) -> Self
where
N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
Self::Inner: Into<N>,
{
Self::from_rational_with_rounding(p, q, Rounding::Down).unwrap_or_else(|_| Self::one())
}
/// Approximate the fraction `p/q` into a per-thing fraction.
///
/// 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>`.
///
/// In the case of an overflow (or divide by zero), an `Err` is returned.
///
/// Rounding is determined by the parameter `rounding`, i.e.
///
/// ```rust
/// # use sp_arithmetic::{Percent, PerThing, Rounding::*};
/// # fn main () {
/// // 989/100 is technically closer to 99%.
/// assert_eq!(
/// Percent::from_rational_with_rounding(989u64, 1000, Down).unwrap(),
/// Percent::from_parts(98),
/// );
/// assert_eq!(
/// Percent::from_rational_with_rounding(984u64, 1000, NearestPrefUp).unwrap(),
/// Percent::from_parts(98),
/// );
/// assert_eq!(
/// Percent::from_rational_with_rounding(985u64, 1000, NearestPrefDown).unwrap(),
/// Percent::from_parts(98),
/// );
/// assert_eq!(
/// Percent::from_rational_with_rounding(985u64, 1000, NearestPrefUp).unwrap(),
/// Percent::from_parts(99),
/// );
/// assert_eq!(
/// Percent::from_rational_with_rounding(986u64, 1000, NearestPrefDown).unwrap(),
/// Percent::from_parts(99),
/// );
/// assert_eq!(
/// Percent::from_rational_with_rounding(981u64, 1000, Up).unwrap(),
/// Percent::from_parts(99),
/// );
/// assert_eq!(
/// Percent::from_rational_with_rounding(1001u64, 1000, Up),
/// Err(()),
/// );
/// # }
/// ```
///
/// ```rust
/// # use sp_arithmetic::{Percent, PerThing, Rounding::*};
/// # fn main () {
/// assert_eq!(
/// Percent::from_rational_with_rounding(981u64, 1000, Up).unwrap(),
/// Percent::from_parts(99),
/// );
/// # }
/// ```
fn from_rational_with_rounding<N>(p: N, q: N, rounding: Rounding) -> Result<Self, ()>
where
N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
Self::Inner: Into<N>;
/// Same as `Self::from_rational`.
#[deprecated = "Use from_rational instead"]
fn from_rational_approximation<N>(p: N, q: N) -> Self
where
N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
Self::Inner: Into<N>,
{
Self::from_rational(p, q)
}sourcefn from_rational_approximation<N>(p: N, q: N) -> Selfwhere
N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
Self::Inner: Into<N>,
fn from_rational_approximation<N>(p: N, q: N) -> Selfwhere
N: RationalArg + TryInto<Self::Inner> + TryInto<Self::Upper>,
Self::Inner: Into<N>,
Same as Self::from_rational.