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§
sourcetype Error
type Error
Usually ArithmeticError
.
Required Methods§
sourcefn cmul(self, rhs: Rhs) -> Result<Self::Output, Self::Error>
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§
sourcefn saturating_mul(self, rhs: Rhs) -> Self::Outputwhere
Self: PartialOrd + Zero + Sized,
Rhs: PartialOrd + Zero,
Self::Output: Bounded,
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.
impl<P: Precision> CheckedMul<FixedPoint<i16, P>> for i16
Available on crate feature
i16
only.type Output = FixedPoint<i16, P>
type Error = ArithmeticError
fn cmul( self, rhs: FixedPoint<i16, P> ) -> Result<FixedPoint<i16, P>, ArithmeticError>
fn saturating_mul(self, rhs: FixedPoint<i16, P>) -> Self::Output
source§impl<P: Precision> CheckedMul<FixedPoint<i128, P>> for i128
Available on crate feature i128
only.
impl<P: Precision> CheckedMul<FixedPoint<i128, P>> for i128
Available on crate feature
i128
only.type Output = FixedPoint<i128, P>
type Error = ArithmeticError
fn cmul( self, rhs: FixedPoint<i128, P> ) -> Result<FixedPoint<i128, P>, ArithmeticError>
fn saturating_mul(self, rhs: FixedPoint<i128, P>) -> Self::Output
source§impl<P: Precision> CheckedMul<FixedPoint<i64, P>> for i64
Available on crate feature i64
only.
impl<P: Precision> CheckedMul<FixedPoint<i64, P>> for i64
Available on crate feature
i64
only.type Output = FixedPoint<i64, P>
type Error = ArithmeticError
fn cmul( self, rhs: FixedPoint<i64, P> ) -> Result<FixedPoint<i64, P>, ArithmeticError>
fn saturating_mul(self, rhs: FixedPoint<i64, P>) -> Self::Output
source§impl CheckedMul<i32> for i32
impl CheckedMul<i32> for i32
source§impl CheckedMul<i128> for i128
impl CheckedMul<i128> for i128
source§impl CheckedMul<i64> for i64
impl CheckedMul<i64> for i64
source§impl<P: Precision> CheckedMul<FixedPoint<i32, P>> for i32
Available on crate feature i32
only.
impl<P: Precision> CheckedMul<FixedPoint<i32, P>> for i32
Available on crate feature
i32
only.type Output = FixedPoint<i32, P>
type Error = ArithmeticError
fn cmul( self, rhs: FixedPoint<i32, P> ) -> Result<FixedPoint<i32, P>, ArithmeticError>
fn saturating_mul(self, rhs: FixedPoint<i32, P>) -> Self::Output
source§impl CheckedMul<i8> for i8
impl CheckedMul<i8> for i8
source§impl CheckedMul<i16> for i16
impl CheckedMul<i16> for i16
Implementors§
source§impl<P: Precision> CheckedMul<i16> for FixedPoint<i16, P>
Available on crate feature i16
only.
impl<P: Precision> CheckedMul<i16> for FixedPoint<i16, P>
Available on crate feature
i16
only.type Output = FixedPoint<i16, P>
type Error = ArithmeticError
source§impl<P: Precision> CheckedMul<i32> for FixedPoint<i32, P>
Available on crate feature i32
only.
impl<P: Precision> CheckedMul<i32> for FixedPoint<i32, P>
Available on crate feature
i32
only.type Output = FixedPoint<i32, P>
type Error = ArithmeticError
source§impl<P: Precision> CheckedMul<i64> for FixedPoint<i64, P>
Available on crate feature i64
only.
impl<P: Precision> CheckedMul<i64> for FixedPoint<i64, P>
Available on crate feature
i64
only.type Output = FixedPoint<i64, P>
type Error = ArithmeticError
source§impl<P: Precision> CheckedMul<i128> for FixedPoint<i128, P>
Available on crate feature i128
only.
impl<P: Precision> CheckedMul<i128> for FixedPoint<i128, P>
Available on crate feature
i128
only.