Trait rug::ops::RemRounding

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

    fn rem_trunc(self, rhs: Rhs) -> Self::Output;
    fn rem_ceil(self, rhs: Rhs) -> Self::Output;
    fn rem_floor(self, rhs: Rhs) -> Self::Output;
    fn rem_euc(self, rhs: Rhs) -> Self::Output;
}
Expand description

Rounding variants of the remainder operation.

Examples

use rug::ops::RemRounding;
struct I(i32);
impl RemRounding<i32> for I {
    type Output = i32;
    fn rem_trunc(self, rhs: i32) -> i32 {
        self.0 % rhs
    }
    fn rem_ceil(self, rhs: i32) -> i32 {
        let r = self.0 % rhs;
        let change = if rhs > 0 { r > 0 } else { r < 0 };
        if change {
            r - rhs
        } else {
            r
        }
    }
    fn rem_floor(self, rhs: i32) -> i32 {
        let r = self.0 % rhs;
        let change = if rhs > 0 { r < 0 } else { r > 0 };
        if change {
            r + rhs
        } else {
            r
        }
    }
    fn rem_euc(self, rhs: i32) -> i32 {
        let r = self.0 % rhs;
        if r < 0 {
            if rhs < 0 {
                r - rhs
            } else {
                r + rhs
            }
        } else {
            r
        }
    }
}
assert_eq!(I(-10).rem_trunc(-3), -1);
assert_eq!(I(-10).rem_ceil(-3), 2);
assert_eq!(I(-10).rem_floor(-3), -1);
assert_eq!(I(-10).rem_euc(-3), 2);

Required Associated Types

The resulting type from the remainder operation.

Required Methods

Finds the remainder when the quotient is rounded towards zero.

Finds the remainder when the quotient is rounded up.

Finds the remainder when the quotient is rounded down.

Finds the positive remainder from Euclidean division.

Implementations on Foreign Types

Implementors