Trait MulDiv

Source
pub trait MulDiv<RHS = Self> {
    type Output;

    // Required methods
    fn mul_div_floor(self, num: RHS, denom: RHS) -> Option<Self::Output>;
    fn mul_div_ceil(self, num: RHS, denom: RHS) -> Option<Self::Output>;
    fn to_underflow_u64(self) -> u64;
}
Expand description

Trait for calculating val * num / denom with different rounding modes and overflow protection.

Implementations of this trait have to ensure that even if the result of the multiplication does not fit into the type, as long as it would fit after the division the correct result has to be returned instead of None. None only should be returned if the overall result does not fit into the type.

This specifically means that e.g. the u64 implementation must, depending on the arguments, be able to do 128 bit integer multiplication.

Required Associated Types§

Source

type Output

Output type for the methods of this trait.

Required Methods§

Source

fn mul_div_floor(self, num: RHS, denom: RHS) -> Option<Self::Output>

Source

fn mul_div_ceil(self, num: RHS, denom: RHS) -> Option<Self::Output>

Source

fn to_underflow_u64(self) -> u64

Return u64 not out of bounds

Implementations on Foreign Types§

Source§

impl MulDiv for u64

Source§

type Output = u64

Source§

fn mul_div_floor(self, num: Self, denom: Self) -> Option<Self::Output>

Source§

fn mul_div_ceil(self, num: Self, denom: Self) -> Option<Self::Output>

Source§

fn to_underflow_u64(self) -> u64

Implementors§