pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
Show 16 methods
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_ceil(&self, other: &Self) -> Self { ... }
fn gcd_lcm(&self, other: &Self) -> (Self, Self) { ... }
fn extended_gcd(&self, other: &Self) -> ExtendedGcd<Self>
where
Self: Clone,
{ ... }
fn extended_gcd_lcm(&self, other: &Self) -> (ExtendedGcd<Self>, Self)
where
Self: Clone + Signed,
{ ... }
fn div_mod_floor(&self, other: &Self) -> (Self, Self) { ... }
fn next_multiple_of(&self, other: &Self) -> Self
where
Self: Clone,
{ ... }
fn prev_multiple_of(&self, other: &Self) -> Self
where
Self: Clone,
{ ... }
}Floored integer division.
Examples
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);Floored integer modulo, satisfying:
assert!(n.div_floor(&d) * d + n.mod_floor(&d) == n)Examples
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);Lowest Common Multiple (LCM).
Examples
assert_eq!(7.lcm(&3), 21);
assert_eq!(2.lcm(&4), 4);
assert_eq!(0.lcm(&0), 0);Deprecated, use is_multiple_of instead.
Returns true if self is a multiple of other.
Examples
assert_eq!(9.is_multiple_of(&3), true);
assert_eq!(3.is_multiple_of(&9), false);Returns true if the number is even.
Examples
assert_eq!(3.is_even(), false);
assert_eq!(4.is_even(), true);Returns true if the number is odd.
Examples
assert_eq!(3.is_odd(), true);
assert_eq!(4.is_odd(), false);Simultaneous truncated integer division and modulus.
Returns (quotient, remainder).
Examples
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));Ceiled integer division.
Examples
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);Greatest Common Divisor (GCD) and Lowest Common Multiple (LCM) together.
Potentially more efficient than calling gcd and lcm
individually for identical inputs.
Examples
assert_eq!(10.gcd_lcm(&4), (2, 20));
assert_eq!(8.gcd_lcm(&9), (1, 72));Greatest common divisor and Bézout coefficients.
Examples
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));Greatest common divisor, least common multiple, and Bézout coefficients.
Simultaneous floored integer division and modulus.
Returns (quotient, remainder).
Examples
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));Rounds up to nearest multiple of argument.
Notes
For signed types, a.next_multiple_of(b) = a.prev_multiple_of(b.neg()).
Examples
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);Rounds down to nearest multiple of argument.
Notes
For signed types, a.prev_multiple_of(b) = a.next_multiple_of(b.neg()).
Examples
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);sourceimpl Integer for i8
impl Integer for i8
source
Floored integer division
source
Floored integer modulo
source
Calculates div_floor and mod_floor simultaneously
source
Calculates the Greatest Common Divisor (GCD) of the number and
other. The result is always non-negative.
source
Calculates the Lowest Common Multiple (LCM) of the number and
other.
source
Calculates the Greatest Common Divisor (GCD) and
Lowest Common Multiple (LCM) of the number and other.
source
Deprecated, use is_multiple_of instead.
source
Returns true if the number is a multiple of other.
source
Returns true if the number is divisible by 2
source
Returns true if the number is not divisible by 2
source
Simultaneous truncated integer division and modulus.
source
Rounds up to nearest multiple of argument.
source
Rounds down to nearest multiple of argument.
sourceimpl Integer for i16
impl Integer for i16
source
Floored integer division
source
Floored integer modulo
source
Calculates div_floor and mod_floor simultaneously
source
Calculates the Greatest Common Divisor (GCD) of the number and
other. The result is always non-negative.
source
Calculates the Lowest Common Multiple (LCM) of the number and
other.
source
Calculates the Greatest Common Divisor (GCD) and
Lowest Common Multiple (LCM) of the number and other.
source
Deprecated, use is_multiple_of instead.
source
Returns true if the number is a multiple of other.
source
Returns true if the number is divisible by 2
source
Returns true if the number is not divisible by 2
source
Simultaneous truncated integer division and modulus.
source
Rounds up to nearest multiple of argument.
source
Rounds down to nearest multiple of argument.
sourceimpl Integer for i32
impl Integer for i32
source
Floored integer division
source
Floored integer modulo
source
Calculates div_floor and mod_floor simultaneously
source
Calculates the Greatest Common Divisor (GCD) of the number and
other. The result is always non-negative.
source
Calculates the Lowest Common Multiple (LCM) of the number and
other.
source
Calculates the Greatest Common Divisor (GCD) and
Lowest Common Multiple (LCM) of the number and other.
source
Deprecated, use is_multiple_of instead.
source
Returns true if the number is a multiple of other.
source
Returns true if the number is divisible by 2
source
Returns true if the number is not divisible by 2
source
Simultaneous truncated integer division and modulus.
source
Rounds up to nearest multiple of argument.
source
Rounds down to nearest multiple of argument.
sourceimpl Integer for i64
impl Integer for i64
source
Floored integer division
source
Floored integer modulo
source
Calculates div_floor and mod_floor simultaneously
source
Calculates the Greatest Common Divisor (GCD) of the number and
other. The result is always non-negative.
source
Calculates the Lowest Common Multiple (LCM) of the number and
other.
source
Calculates the Greatest Common Divisor (GCD) and
Lowest Common Multiple (LCM) of the number and other.
source
Deprecated, use is_multiple_of instead.
source
Returns true if the number is a multiple of other.
source
Returns true if the number is divisible by 2
source
Returns true if the number is not divisible by 2
source
Simultaneous truncated integer division and modulus.
source
Rounds up to nearest multiple of argument.
source
Rounds down to nearest multiple of argument.
sourceimpl Integer for isize
impl Integer for isize
source
Floored integer division
source
Floored integer modulo
source
Calculates div_floor and mod_floor simultaneously
source
Calculates the Greatest Common Divisor (GCD) of the number and
other. The result is always non-negative.
source
Calculates the Lowest Common Multiple (LCM) of the number and
other.
source
Calculates the Greatest Common Divisor (GCD) and
Lowest Common Multiple (LCM) of the number and other.
source
Deprecated, use is_multiple_of instead.
source
Returns true if the number is a multiple of other.
source
Returns true if the number is divisible by 2
source
Returns true if the number is not divisible by 2
source
Simultaneous truncated integer division and modulus.
source
Rounds up to nearest multiple of argument.
source
Rounds down to nearest multiple of argument.
sourceimpl Integer for i128
impl Integer for i128
source
Floored integer division
source
Floored integer modulo
source
Calculates div_floor and mod_floor simultaneously
source
Calculates the Greatest Common Divisor (GCD) of the number and
other. The result is always non-negative.
source
Calculates the Lowest Common Multiple (LCM) of the number and
other.
source
Calculates the Greatest Common Divisor (GCD) and
Lowest Common Multiple (LCM) of the number and other.
source
Deprecated, use is_multiple_of instead.
source
Returns true if the number is a multiple of other.
source
Returns true if the number is divisible by 2
source
Returns true if the number is not divisible by 2
source
Simultaneous truncated integer division and modulus.
source
Rounds up to nearest multiple of argument.
source
Rounds down to nearest multiple of argument.
sourceimpl Integer for u8
impl Integer for u8
source
Unsigned integer division. Returns the same result as div (/).
source
Unsigned integer modulo operation. Returns the same result as rem (%).
source
Calculates the Greatest Common Divisor (GCD) of the number and other
source
Calculates the Lowest Common Multiple (LCM) of the number and other.
source
Calculates the Greatest Common Divisor (GCD) and
Lowest Common Multiple (LCM) of the number and other.
source
Deprecated, use is_multiple_of instead.
source
Returns true if the number is a multiple of other.
source
Returns true if the number is divisible by 2.
source
Returns true if the number is not divisible by 2.
source
Simultaneous truncated integer division and modulus.
sourceimpl Integer for u16
impl Integer for u16
source
Unsigned integer division. Returns the same result as div (/).
source
Unsigned integer modulo operation. Returns the same result as rem (%).
source
Calculates the Greatest Common Divisor (GCD) of the number and other
source
Calculates the Lowest Common Multiple (LCM) of the number and other.
source
Calculates the Greatest Common Divisor (GCD) and
Lowest Common Multiple (LCM) of the number and other.
source
Deprecated, use is_multiple_of instead.
source
Returns true if the number is a multiple of other.
source
Returns true if the number is divisible by 2.
source
Returns true if the number is not divisible by 2.
source
Simultaneous truncated integer division and modulus.
sourceimpl Integer for u32
impl Integer for u32
source
Unsigned integer division. Returns the same result as div (/).
source
Unsigned integer modulo operation. Returns the same result as rem (%).
source
Calculates the Greatest Common Divisor (GCD) of the number and other
source
Calculates the Lowest Common Multiple (LCM) of the number and other.
source
Calculates the Greatest Common Divisor (GCD) and
Lowest Common Multiple (LCM) of the number and other.
source
Deprecated, use is_multiple_of instead.
source
Returns true if the number is a multiple of other.
source
Returns true if the number is divisible by 2.
source
Returns true if the number is not divisible by 2.
source
Simultaneous truncated integer division and modulus.
sourceimpl Integer for u64
impl Integer for u64
source
Unsigned integer division. Returns the same result as div (/).
source
Unsigned integer modulo operation. Returns the same result as rem (%).
source
Calculates the Greatest Common Divisor (GCD) of the number and other
source
Calculates the Lowest Common Multiple (LCM) of the number and other.
source
Calculates the Greatest Common Divisor (GCD) and
Lowest Common Multiple (LCM) of the number and other.
source
Deprecated, use is_multiple_of instead.
source
Returns true if the number is a multiple of other.
source
Returns true if the number is divisible by 2.
source
Returns true if the number is not divisible by 2.
source
Simultaneous truncated integer division and modulus.
sourceimpl Integer for usize
impl Integer for usize
source
Unsigned integer division. Returns the same result as div (/).
source
Unsigned integer modulo operation. Returns the same result as rem (%).
source
Calculates the Greatest Common Divisor (GCD) of the number and other
source
Calculates the Lowest Common Multiple (LCM) of the number and other.
source
Calculates the Greatest Common Divisor (GCD) and
Lowest Common Multiple (LCM) of the number and other.
source
Deprecated, use is_multiple_of instead.
source
Returns true if the number is a multiple of other.
source
Returns true if the number is divisible by 2.
source
Returns true if the number is not divisible by 2.
source
Simultaneous truncated integer division and modulus.
sourceimpl Integer for u128
impl Integer for u128
source
Unsigned integer division. Returns the same result as div (/).
source
Unsigned integer modulo operation. Returns the same result as rem (%).
source
Calculates the Greatest Common Divisor (GCD) of the number and other
source
Calculates the Lowest Common Multiple (LCM) of the number and other.
source
Calculates the Greatest Common Divisor (GCD) and
Lowest Common Multiple (LCM) of the number and other.
source
Deprecated, use is_multiple_of instead.
source
Returns true if the number is a multiple of other.
source
Returns true if the number is divisible by 2.
source
Returns true if the number is not divisible by 2.
source
Simultaneous truncated integer division and modulus.