CheckedCeilDiv

Trait CheckedCeilDiv 

Source
pub trait CheckedCeilDiv: Sized {
    // Required method
    fn checked_ceil_div(&self, rhs: Self) -> Option<(Self, Self)>;
}
Expand description

Perform a division that does not truncate value from either side, returning the (quotient, divisor) as a tuple

When dividing integers, we are often left with a remainder, which can cause information to be lost. By checking for a remainder, adjusting the quotient, and recalculating the divisor, this provides the most fair calculation.

For example, 400 / 32 = 12, with a remainder cutting off 0.5 of amount. If we simply ceiling the quotient to 13, then we’re saying 400 / 32 = 13, which also cuts off value. To improve this result, we calculate the other way around and again check for a remainder: 400 / 13 = 30, with a remainder of 0.77, and we ceiling that value again. This gives us a final calculation of 400 / 31 = 13, which provides a ceiling calculation without cutting off more value than needed.

This calculation fails if the divisor is larger than the dividend, to avoid having a result like: 1 / 1000 = 1.

Required Methods§

Source

fn checked_ceil_div(&self, rhs: Self) -> Option<(Self, Self)>

Perform ceiling division

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl CheckedCeilDiv for u128

Source§

fn checked_ceil_div(&self, rhs: Self) -> Option<(Self, Self)>

Implementors§