Trait Integer

trait Integer: Sized + Num + PartialOrd + Ord + Eq

Required Methods

fn div_floor(self: &Self, other: &Self) -> Self

Floored integer division.

Examples

# use num_integer::Integer;
assert!(( 8).div_floor(& 3) ==  2);
assert!(( 8).div_floor(&-3) == -3);
assert!((-8).div_floor(& 3) == -3);
assert!((-8).div_floor(&-3) ==  2);

assert!(( 1).div_floor(& 2) ==  0);
assert!(( 1).div_floor(&-2) == -1);
assert!((-1).div_floor(& 2) == -1);
assert!((-1).div_floor(&-2) ==  0);
fn mod_floor(self: &Self, other: &Self) -> Self

Floored integer modulo, satisfying:

# use num_integer::Integer;
# let n = 1; let d = 1;
assert!(n.div_floor(&d) * d + n.mod_floor(&d) == n)

Examples

# use num_integer::Integer;
assert!(( 8).mod_floor(& 3) ==  2);
assert!(( 8).mod_floor(&-3) == -1);
assert!((-8).mod_floor(& 3) ==  1);
assert!((-8).mod_floor(&-3) == -2);

assert!(( 1).mod_floor(& 2) ==  1);
assert!(( 1).mod_floor(&-2) == -1);
assert!((-1).mod_floor(& 2) ==  1);
assert!((-1).mod_floor(&-2) == -1);
fn gcd(self: &Self, other: &Self) -> Self

Greatest Common Divisor (GCD).

Examples

# use num_integer::Integer;
assert_eq!(6.gcd(&8), 2);
assert_eq!(7.gcd(&3), 1);
fn lcm(self: &Self, other: &Self) -> Self

Lowest Common Multiple (LCM).

Examples

# use num_integer::Integer;
assert_eq!(7.lcm(&3), 21);
assert_eq!(2.lcm(&4), 4);
assert_eq!(0.lcm(&0), 0);
fn is_multiple_of(self: &Self, other: &Self) -> bool

Returns true if self is a multiple of other.

Examples

# use num_integer::Integer;
assert_eq!(9.is_multiple_of(&3), true);
assert_eq!(3.is_multiple_of(&9), false);
fn is_even(self: &Self) -> bool

Returns true if the number is even.

Examples

# use num_integer::Integer;
assert_eq!(3.is_even(), false);
assert_eq!(4.is_even(), true);
fn is_odd(self: &Self) -> bool

Returns true if the number is odd.

Examples

# use num_integer::Integer;
assert_eq!(3.is_odd(), true);
assert_eq!(4.is_odd(), false);
fn div_rem(self: &Self, other: &Self) -> (Self, Self)

Simultaneous truncated integer division and modulus. Returns (quotient, remainder).

Examples

# use num_integer::Integer;
assert_eq!(( 8).div_rem( &3), ( 2,  2));
assert_eq!(( 8).div_rem(&-3), (-2,  2));
assert_eq!((-8).div_rem( &3), (-2, -2));
assert_eq!((-8).div_rem(&-3), ( 2, -2));

assert_eq!(( 1).div_rem( &2), ( 0,  1));
assert_eq!(( 1).div_rem(&-2), ( 0,  1));
assert_eq!((-1).div_rem( &2), ( 0, -1));
assert_eq!((-1).div_rem(&-2), ( 0, -1));

Provided Methods

fn div_ceil(self: &Self, other: &Self) -> Self

Ceiled integer division.

Examples

# use num_integer::Integer;
assert_eq!(( 8).div_ceil( &3),  3);
assert_eq!(( 8).div_ceil(&-3), -2);
assert_eq!((-8).div_ceil( &3), -2);
assert_eq!((-8).div_ceil(&-3),  3);

assert_eq!(( 1).div_ceil( &2), 1);
assert_eq!(( 1).div_ceil(&-2), 0);
assert_eq!((-1).div_ceil( &2), 0);
assert_eq!((-1).div_ceil(&-2), 1);
fn gcd_lcm(self: &Self, other: &Self) -> (Self, Self)

Greatest Common Divisor (GCD) and Lowest Common Multiple (LCM) together.

Potentially more efficient than calling gcd and lcm individually for identical inputs.

Examples

# use num_integer::Integer;
assert_eq!(10.gcd_lcm(&4), (2, 20));
assert_eq!(8.gcd_lcm(&9), (1, 72));
fn extended_gcd(self: &Self, other: &Self) -> ExtendedGcd<Self>
where
    Self: Clone

Greatest common divisor and Bézout coefficients.

Examples

# fn main() {
# use num_integer::{ExtendedGcd, Integer};
# use num_traits::NumAssign;
fn check<A: Copy + Integer + NumAssign>(a: A, b: A) -> bool {
    let ExtendedGcd { gcd, x, y, .. } = a.extended_gcd(&b);
    gcd == x * a + y * b
}
assert!(check(10isize, 4isize));
assert!(check(8isize,  9isize));
# }
fn extended_gcd_lcm(self: &Self, other: &Self) -> (ExtendedGcd<Self>, Self)
where
    Self: Clone + Signed

Greatest common divisor, least common multiple, and Bézout coefficients.

fn divides(self: &Self, other: &Self) -> bool

Deprecated, use is_multiple_of instead.

fn div_mod_floor(self: &Self, other: &Self) -> (Self, Self)

Simultaneous floored integer division and modulus. Returns (quotient, remainder).

Examples

# use num_integer::Integer;
assert_eq!(( 8).div_mod_floor( &3), ( 2,  2));
assert_eq!(( 8).div_mod_floor(&-3), (-3, -1));
assert_eq!((-8).div_mod_floor( &3), (-3,  1));
assert_eq!((-8).div_mod_floor(&-3), ( 2, -2));

assert_eq!(( 1).div_mod_floor( &2), ( 0,  1));
assert_eq!(( 1).div_mod_floor(&-2), (-1, -1));
assert_eq!((-1).div_mod_floor( &2), (-1,  1));
assert_eq!((-1).div_mod_floor(&-2), ( 0, -1));
fn next_multiple_of(self: &Self, other: &Self) -> Self
where
    Self: Clone

Rounds up to nearest multiple of argument.

Notes

For signed types, a.next_multiple_of(b) = a.prev_multiple_of(b.neg()).

Examples

# use num_integer::Integer;
assert_eq!(( 16).next_multiple_of(& 8),  16);
assert_eq!(( 23).next_multiple_of(& 8),  24);
assert_eq!(( 16).next_multiple_of(&-8),  16);
assert_eq!(( 23).next_multiple_of(&-8),  16);
assert_eq!((-16).next_multiple_of(& 8), -16);
assert_eq!((-23).next_multiple_of(& 8), -16);
assert_eq!((-16).next_multiple_of(&-8), -16);
assert_eq!((-23).next_multiple_of(&-8), -24);
fn prev_multiple_of(self: &Self, other: &Self) -> Self
where
    Self: Clone

Rounds down to nearest multiple of argument.

Notes

For signed types, a.prev_multiple_of(b) = a.next_multiple_of(b.neg()).

Examples

# use num_integer::Integer;
assert_eq!(( 16).prev_multiple_of(& 8),  16);
assert_eq!(( 23).prev_multiple_of(& 8),  16);
assert_eq!(( 16).prev_multiple_of(&-8),  16);
assert_eq!(( 23).prev_multiple_of(&-8),  24);
assert_eq!((-16).prev_multiple_of(& 8), -16);
assert_eq!((-23).prev_multiple_of(& 8), -24);
assert_eq!((-16).prev_multiple_of(&-8), -16);
assert_eq!((-23).prev_multiple_of(&-8), -16);
fn dec(self: &mut Self)
where
    Self: Clone

Decrements self by one.

Examples

# use num_integer::Integer;
let mut x: i32 = 43;
x.dec();
assert_eq!(x, 42);
fn inc(self: &mut Self)
where
    Self: Clone

Increments self by one.

Examples

# use num_integer::Integer;
let mut x: i32 = 41;
x.inc();
assert_eq!(x, 42);

Implementors