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.
Calculates floor(val * num / denom)
, i.e. the next integer to the result of the division
with the smaller absolute value.
extern crate muldiv;
use muldiv::MulDiv;
let x = 3i8.mul_div_floor(4, 2);
let x = 5i8.mul_div_floor(2, 3);
let x = (-5i8).mul_div_floor(2, 3);
let x = 3i8.mul_div_floor(3, 2);
let x = (-3i8).mul_div_floor(3, 2);
let x = 127i8.mul_div_floor(4, 3);
Calculates round(val * num / denom)
, i.e. the closest integer to the result of the
division. If both surrounding integers are the same distance, the one with the bigger
absolute value is returned.
extern crate muldiv;
use muldiv::MulDiv;
let x = 3i8.mul_div_round(4, 2);
let x = 5i8.mul_div_round(2, 3);
let x = (-5i8).mul_div_round(2, 3);
let x = 3i8.mul_div_round(3, 2);
let x = (-3i8).mul_div_round(3, 2);
let x = 127i8.mul_div_floor(4, 3);
Calculates ceil(val * num / denom)
, i.e. the next integer to the result of the division
with the bigger absolute value.
extern crate muldiv;
use muldiv::MulDiv;
let x = 3i8.mul_div_ceil(4, 2);
let x = 5i8.mul_div_ceil(2, 3);
let x = (-5i8).mul_div_ceil(2, 3);
let x = 3i8.mul_div_ceil(3, 2);
let x = (-3i8).mul_div_ceil(3, 2);
let x = (127i8).mul_div_ceil(4, 3);