pub trait CheckedMul<Rhs = Self> {
    type Output;
    type Error;

    fn cmul(self, rhs: Rhs) -> Result<Self::Output, Self::Error>;

    fn saturating_mul(self, rhs: Rhs) -> Self::Output
    where
        Self: PartialOrd + Zero + Sized,
        Rhs: PartialOrd + Zero,
        Self::Output: Bounded
, { ... } }
Expand description

Checked multiplication.

Required Associated Types

Result of multiplication.

Usually ArithmeticError.

Required Methods

Checked multiplication. Returns Err on overflow. This is multiplication without rounding, hence it’s available only when at least one operand is integer.

use fixnum::{FixedPoint, typenum::U9, ops::CheckedMul};

type Amount = FixedPoint<i64, U9>;

let a: Amount = "0.000000001".parse()?;
let b: Amount = "0.000000012".parse()?;
assert_eq!(a.cmul(12)?, b);
assert_eq!(12.cmul(a)?, b);

Provided Methods

Saturating multiplication. Computes self * rhs, saturating at the numeric bounds (MIN, MAX) instead of overflowing. This is multiplication without rounding, hence it’s available only when at least one operand is integer.

use fixnum::{FixedPoint, typenum::U9, ops::{Zero, Bounded, RoundMode::*, CheckedMul}};

type Amount = FixedPoint<i64, U9>;

let a: Amount = "0.000000001".parse()?;
let b: Amount = "0.000000012".parse()?;
assert_eq!(a.saturating_mul(12), b);
assert_eq!(12.saturating_mul(a), b);

// i64::MAX * 1e-9 = MAX
assert_eq!(a.saturating_mul(i64::MAX), Amount::MAX);

let c: Amount = "-1.000000001".parse()?;
// -1.000000001 * (SaturatingCeil) MAX = MIN
assert_eq!(c.saturating_mul(i64::MAX), Amount::MIN);

Implementations on Foreign Types

Implementors