Module malachite_base::num::arithmetic::mod_op
source · Expand description
Traits for finding the remainder of two numbers, subject to various rounding rules.
These are the traits:
rounding | by value or reference | by mutable reference (assignment) |
---|---|---|
towards $-\infty$ | Mod | ModAssign |
towards $\infty$ | CeilingMod | CeilingModAssign |
towards $\infty$ | NegMod | NegModAssign |
CeilingMod
and NegMod
are similar. The difference is
that CeilingMod
returns a remainder less than or equal to 0, so that the
usual relation $x = qy + r$ is satisfied, while NegMod
returns a remainder
greater than or equal to zero. This allows the remainder to have an unsigned type, but modifies
the relation to $x = qy - r$.
The Rem
trait in the standard library rounds towards 0.
§mod_op
use malachite_base::num::arithmetic::traits::Mod;
// 2 * 10 + 3 = 23
assert_eq!(23u8.mod_op(10), 3);
// 9 * 5 + 0 = 45
assert_eq!(45u32.mod_op(5), 0);
// 2 * 10 + 3 = 23
assert_eq!(23i8.mod_op(10), 3);
// -3 * -10 + -7 = 23
assert_eq!(23i16.mod_op(-10), -7);
// -3 * 10 + 7 = -23
assert_eq!((-23i32).mod_op(10), 7);
// 2 * -10 + -3 = -23
assert_eq!((-23i64).mod_op(-10), -3);
§mod_assign
use malachite_base::num::arithmetic::traits::ModAssign;
// 2 * 10 + 3 = 23
let mut x = 23u8;
x.mod_assign(10);
assert_eq!(x, 3);
// 9 * 5 + 0 = 45
let mut x = 45u32;
x.mod_assign(5);
assert_eq!(x, 0);
// 2 * 10 + 3 = 23
let mut x = 23i8;
x.mod_assign(10);
assert_eq!(x, 3);
// -3 * -10 + -7 = 23
let mut x = 23i16;
x.mod_assign(-10);
assert_eq!(x, -7);
// -3 * 10 + 7 = -23
let mut x = -23i32;
x.mod_assign(10);
assert_eq!(x, 7);
// 2 * -10 + -3 = -23
let mut x = -23i64;
x.mod_assign(-10);
assert_eq!(x, -3);
§neg_mod
use malachite_base::num::arithmetic::traits::NegMod;
// 3 * 10 - 7 = 23
assert_eq!(23u8.neg_mod(10), 7);
// 9 * 5 + 0 = 45
assert_eq!(45u32.neg_mod(5), 0);
§neg_mod_assign
use malachite_base::num::arithmetic::traits::NegModAssign;
// 3 * 10 - 7 = 23
let mut x = 23u8;
x.neg_mod_assign(10);
assert_eq!(x, 7);
// 9 * 5 + 0 = 45
let mut x = 45u32;
x.neg_mod_assign(5);
assert_eq!(x, 0);
§ceiling_mod
use malachite_base::num::arithmetic::traits::CeilingMod;
// 3 * 10 + -7 = 23
assert_eq!(23i8.ceiling_mod(10), -7);
// -2 * -10 + 3 = 23
assert_eq!(23i16.ceiling_mod(-10), 3);
// -2 * 10 + -3 = -23
assert_eq!((-23i32).ceiling_mod(10), -3);
// 3 * -10 + 7 = -23
assert_eq!((-23i64).ceiling_mod(-10), 7);
§ceiling_mod_assign
use malachite_base::num::arithmetic::traits::CeilingModAssign;
// 3 * 10 + -7 = 23
let mut x = 23i8;
x.ceiling_mod_assign(10);
assert_eq!(x, -7);
// -2 * -10 + 3 = 23
let mut x = 23i16;
x.ceiling_mod_assign(-10);
assert_eq!(x, 3);
// -2 * 10 + -3 = -23
let mut x = -23i32;
x.ceiling_mod_assign(10);
assert_eq!(x, -3);
// 3 * -10 + 7 = -23
let mut x = -23i64;
x.ceiling_mod_assign(-10);
assert_eq!(x, 7);