Trait num::integer::IntegerExperimental [-]  [+] [src]

pub trait Integer: Num + PartialOrd + Div<Self, Self> + Rem<Self, Self> {
    fn div_floor(&self, other: &Self) -> Self;
    fn mod_floor(&self, other: &Self) -> Self;
    fn gcd(&self, other: &Self) -> Self;
    fn lcm(&self, other: &Self) -> Self;
    fn divides(&self, other: &Self) -> bool;
    fn is_multiple_of(&self, other: &Self) -> bool;
    fn is_even(&self) -> bool;
    fn is_odd(&self) -> bool;

    fn div_rem(&self, other: &Self) -> (Self, Self) { ... }
    fn div_mod_floor(&self, other: &Self) -> (Self, Self) { ... }
}

Required Methods

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

Floored integer division.

Examples

extern crate num; fn main() { use num::Integer; assert!(( 8i).div_floor(& 3) == 2); assert!(( 8i).div_floor(&-3) == -3); assert!((-8i).div_floor(& 3) == -3); assert!((-8i).div_floor(&-3) == 2); assert!(( 1i).div_floor(& 2) == 0); assert!(( 1i).div_floor(&-2) == -1); assert!((-1i).div_floor(& 2) == -1); assert!((-1i).div_floor(&-2) == 0); }
assert!(( 8i).div_floor(& 3) ==  2);
assert!(( 8i).div_floor(&-3) == -3);
assert!((-8i).div_floor(& 3) == -3);
assert!((-8i).div_floor(&-3) ==  2);

assert!(( 1i).div_floor(& 2) ==  0);
assert!(( 1i).div_floor(&-2) == -1);
assert!((-1i).div_floor(& 2) == -1);
assert!((-1i).div_floor(&-2) ==  0);

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

Floored integer modulo, satisfying:

extern crate num; fn main() { use num::Integer; let n = 1i; let d = 1i; assert!(n.div_floor(&d) * d + n.mod_floor(&d) == n) }
assert!(n.div_floor(&d) * d + n.mod_floor(&d) == n)

Examples

extern crate num; fn main() { use num::Integer; assert!(( 8i).mod_floor(& 3) == 2); assert!(( 8i).mod_floor(&-3) == -1); assert!((-8i).mod_floor(& 3) == 1); assert!((-8i).mod_floor(&-3) == -2); assert!(( 1i).mod_floor(& 2) == 1); assert!(( 1i).mod_floor(&-2) == -1); assert!((-1i).mod_floor(& 2) == 1); assert!((-1i).mod_floor(&-2) == -1); }
assert!(( 8i).mod_floor(& 3) ==  2);
assert!(( 8i).mod_floor(&-3) == -1);
assert!((-8i).mod_floor(& 3) ==  1);
assert!((-8i).mod_floor(&-3) == -2);

assert!(( 1i).mod_floor(& 2) ==  1);
assert!(( 1i).mod_floor(&-2) == -1);
assert!((-1i).mod_floor(& 2) ==  1);
assert!((-1i).mod_floor(&-2) == -1);

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

Greatest Common Divisor (GCD).

Examples

extern crate num; fn main() { use num::Integer; assert_eq!(6i.gcd(&8), 2); assert_eq!(7i.gcd(&3), 1); }
assert_eq!(6i.gcd(&8), 2);
assert_eq!(7i.gcd(&3), 1);

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

Lowest Common Multiple (LCM).

Examples

extern crate num; fn main() { use num::Integer; assert_eq!(7i.lcm(&3), 21); assert_eq!(2i.lcm(&4), 4); }
assert_eq!(7i.lcm(&3), 21);
assert_eq!(2i.lcm(&4), 4);

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

Deprecated, use is_multiple_of instead.

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

Returns true if other is a multiple of self.

Examples

extern crate num; fn main() { use num::Integer; assert_eq!(9i.is_multiple_of(&3), true); assert_eq!(3i.is_multiple_of(&9), false); }
assert_eq!(9i.is_multiple_of(&3), true);
assert_eq!(3i.is_multiple_of(&9), false);

fn is_even(&self) -> bool

Returns true if the number is even.

Examples

extern crate num; fn main() { use num::Integer; assert_eq!(3i.is_even(), false); assert_eq!(4i.is_even(), true); }
assert_eq!(3i.is_even(), false);
assert_eq!(4i.is_even(), true);

fn is_odd(&self) -> bool

Returns true if the number is odd.

Examples

extern crate num; fn main() { use num::Integer; assert_eq!(3i.is_odd(), true); assert_eq!(4i.is_odd(), false); }
assert_eq!(3i.is_odd(), true);
assert_eq!(4i.is_odd(), false);

Provided Methods

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

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

Examples

extern crate num; fn main() { use num::Integer; assert_eq!(( 8i).div_rem( &3), ( 2, 2)); assert_eq!(( 8i).div_rem(&-3), (-2, 2)); assert_eq!((-8i).div_rem( &3), (-2, -2)); assert_eq!((-8i).div_rem(&-3), ( 2, -2)); assert_eq!(( 1i).div_rem( &2), ( 0, 1)); assert_eq!(( 1i).div_rem(&-2), ( 0, 1)); assert_eq!((-1i).div_rem( &2), ( 0, -1)); assert_eq!((-1i).div_rem(&-2), ( 0, -1)); }
assert_eq!(( 8i).div_rem( &3), ( 2,  2));
assert_eq!(( 8i).div_rem(&-3), (-2,  2));
assert_eq!((-8i).div_rem( &3), (-2, -2));
assert_eq!((-8i).div_rem(&-3), ( 2, -2));

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

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

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

Examples

extern crate num; fn main() { use num::Integer; assert_eq!(( 8i).div_mod_floor( &3), ( 2, 2)); assert_eq!(( 8i).div_mod_floor(&-3), (-3, -1)); assert_eq!((-8i).div_mod_floor( &3), (-3, 1)); assert_eq!((-8i).div_mod_floor(&-3), ( 2, -2)); assert_eq!(( 1i).div_mod_floor( &2), ( 0, 1)); assert_eq!(( 1i).div_mod_floor(&-2), (-1, -1)); assert_eq!((-1i).div_mod_floor( &2), (-1, 1)); assert_eq!((-1i).div_mod_floor(&-2), ( 0, -1)); }
assert_eq!(( 8i).div_mod_floor( &3), ( 2,  2));
assert_eq!(( 8i).div_mod_floor(&-3), (-3, -1));
assert_eq!((-8i).div_mod_floor( &3), (-3,  1));
assert_eq!((-8i).div_mod_floor(&-3), ( 2, -2));

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

Implementors