Trait fixnum::ops::CheckedMul

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

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

    // Provided method
    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§

source

type Output

Result of multiplication.

source

type Error

Usually ArithmeticError.

Required Methods§

source

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

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§

source

fn saturating_mul(self, rhs: Rhs) -> Self::Outputwhere Self: PartialOrd + Zero + Sized, Rhs: PartialOrd + Zero, Self::Output: Bounded,

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§

source§

impl<P: Precision> CheckedMul<FixedPoint<i16, P>> for i16

Available on crate feature i16 only.
source§

impl<P: Precision> CheckedMul<FixedPoint<i128, P>> for i128

Available on crate feature i128 only.
source§

impl<P: Precision> CheckedMul<FixedPoint<i64, P>> for i64

Available on crate feature i64 only.
source§

impl CheckedMul<i32> for i32

§

type Output = i32

§

type Error = ArithmeticError

source§

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

source§

fn saturating_mul(self, rhs: Self) -> Self::Output

source§

impl CheckedMul<i128> for i128

§

type Output = i128

§

type Error = ArithmeticError

source§

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

source§

fn saturating_mul(self, rhs: Self) -> Self::Output

source§

impl CheckedMul<i64> for i64

§

type Output = i64

§

type Error = ArithmeticError

source§

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

source§

fn saturating_mul(self, rhs: Self) -> Self::Output

source§

impl<P: Precision> CheckedMul<FixedPoint<i32, P>> for i32

Available on crate feature i32 only.
source§

impl CheckedMul<i8> for i8

§

type Output = i8

§

type Error = ArithmeticError

source§

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

source§

fn saturating_mul(self, rhs: Self) -> Self::Output

source§

impl CheckedMul<i16> for i16

§

type Output = i16

§

type Error = ArithmeticError

source§

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

source§

fn saturating_mul(self, rhs: Self) -> Self::Output

Implementors§

source§

impl<P: Precision> CheckedMul<i16> for FixedPoint<i16, P>

Available on crate feature i16 only.
source§

impl<P: Precision> CheckedMul<i32> for FixedPoint<i32, P>

Available on crate feature i32 only.
source§

impl<P: Precision> CheckedMul<i64> for FixedPoint<i64, P>

Available on crate feature i64 only.
source§

impl<P: Precision> CheckedMul<i128> for FixedPoint<i128, P>

Available on crate feature i128 only.