Module malachite_base::num::arithmetic::div_mod
source · Expand description
Traits for simultaneously finding the quotient and remainder of two numbers, subject to various rounding rules.
These are the traits:
rounding | by value or reference | by mutable reference (assignment) |
---|---|---|
towards $-\infty$ | DivMod | DivAssignMod |
towards 0 | DivRem | DivAssignRem |
towards $\infty$ | CeilingDivMod | CeilingDivAssignMod |
towards $\infty$ | CeilingDivNegMod | CeilingDivAssignNegMod |
CeilingDivMod
and CeilingDivNegMod
are
similar. The difference is that CeilingDivMod
returns a remainder
less than or equal to 0, so that the usual relation $x = qy + r$ is satisfied, while
CeilingDivNegMod
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$.
§div_mod
use malachite_base::num::arithmetic::traits::DivMod;
// 2 * 10 + 3 = 23
assert_eq!(23u8.div_mod(10), (2, 3));
// 9 * 5 + 0 = 45
assert_eq!(45u32.div_mod(5), (9, 0));
// 2 * 10 + 3 = 23
assert_eq!(23i8.div_mod(10), (2, 3));
// -3 * -10 + -7 = 23
assert_eq!(23i16.div_mod(-10), (-3, -7));
// -3 * 10 + 7 = -23
assert_eq!((-23i32).div_mod(10), (-3, 7));
// 2 * -10 + -3 = -23
assert_eq!((-23i64).div_mod(-10), (2, -3));
§div_assign_mod
use malachite_base::num::arithmetic::traits::DivAssignMod;
// 2 * 10 + 3 = 23
let mut x = 23u8;
assert_eq!(x.div_assign_mod(10), 3);
assert_eq!(x, 2);
// 9 * 5 + 0 = 45
let mut x = 45u32;
assert_eq!(x.div_assign_mod(5), 0);
assert_eq!(x, 9);
// 2 * 10 + 3 = 23
let mut x = 23i8;
assert_eq!(x.div_assign_mod(10), 3);
assert_eq!(x, 2);
// -3 * -10 + -7 = 23
let mut x = 23i16;
assert_eq!(x.div_assign_mod(-10), -7);
assert_eq!(x, -3);
// -3 * 10 + 7 = -23
let mut x = -23i32;
assert_eq!(x.div_assign_mod(10), 7);
assert_eq!(x, -3);
// 2 * -10 + -3 = -23
let mut x = -23i64;
assert_eq!(x.div_assign_mod(-10), -3);
assert_eq!(x, 2);
§div_rem
use malachite_base::num::arithmetic::traits::DivRem;
// 2 * 10 + 3 = 23
assert_eq!(23u8.div_rem(10), (2, 3));
// 9 * 5 + 0 = 45
assert_eq!(45u32.div_rem(5), (9, 0));
// 2 * 10 + 3 = 23
assert_eq!(23i8.div_rem(10), (2, 3));
// -2 * -10 + 3 = 23
assert_eq!(23i16.div_rem(-10), (-2, 3));
// -2 * 10 + -3 = -23
assert_eq!((-23i32).div_rem(10), (-2, -3));
// 2 * -10 + -3 = -23
assert_eq!((-23i64).div_rem(-10), (2, -3));
§div_assign_rem
use malachite_base::num::arithmetic::traits::DivAssignRem;
// 2 * 10 + 3 = 23
let mut x = 23u8;
assert_eq!(x.div_assign_rem(10), 3);
assert_eq!(x, 2);
// 9 * 5 + 0 = 45
let mut x = 45u32;
assert_eq!(x.div_assign_rem(5), 0);
assert_eq!(x, 9);
// 2 * 10 + 3 = 23
let mut x = 23i8;
assert_eq!(x.div_assign_rem(10), 3);
assert_eq!(x, 2);
// -2 * -10 + 3 = 23
let mut x = 23i16;
assert_eq!(x.div_assign_rem(-10), 3);
assert_eq!(x, -2);
// -2 * 10 + -3 = -23
let mut x = -23i32;
assert_eq!(x.div_assign_rem(10), -3);
assert_eq!(x, -2);
// 2 * -10 + -3 = -23
let mut x = -23i64;
assert_eq!(x.div_assign_rem(-10), -3);
assert_eq!(x, 2);
§ceiling_div_neg_mod
use malachite_base::num::arithmetic::traits::CeilingDivNegMod;
// 3 * 10 - 7 = 23
assert_eq!(23u8.ceiling_div_neg_mod(10), (3, 7));
// 9 * 5 + 0 = 45
assert_eq!(45u32.ceiling_div_neg_mod(5), (9, 0));
§ceiling_div_assign_neg_mod
use malachite_base::num::arithmetic::traits::CeilingDivAssignNegMod;
// 3 * 10 - 7 = 23
let mut x = 23u8;
assert_eq!(x.ceiling_div_assign_neg_mod(10), 7);
assert_eq!(x, 3);
// 9 * 5 + 0 = 45
let mut x = 45u32;
assert_eq!(x.ceiling_div_assign_neg_mod(5), 0);
assert_eq!(x, 9);
§ceiling_div_mod
use malachite_base::num::arithmetic::traits::CeilingDivMod;
// 3 * 10 + -7 = 23
assert_eq!(23i8.ceiling_div_mod(10), (3, -7));
// -2 * -10 + 3 = 23
assert_eq!(23i16.ceiling_div_mod(-10), (-2, 3));
// -2 * 10 + -3 = -23
assert_eq!((-23i32).ceiling_div_mod(10), (-2, -3));
// 3 * -10 + 7 = -23
assert_eq!((-23i64).ceiling_div_mod(-10), (3, 7));
§ceiling_div_assign_mod
use malachite_base::num::arithmetic::traits::CeilingDivAssignMod;
// 3 * 10 + -7 = 23
let mut x = 23i8;
assert_eq!(x.ceiling_div_assign_mod(10), -7);
assert_eq!(x, 3);
// -2 * -10 + 3 = 23
let mut x = 23i16;
assert_eq!(x.ceiling_div_assign_mod(-10), 3);
assert_eq!(x, -2);
// -2 * 10 + -3 = -23
let mut x = -23i32;
assert_eq!(x.ceiling_div_assign_mod(10), -3);
assert_eq!(x, -2);
// 3 * -10 + 7 = -23
let mut x = -23i64;
assert_eq!(x.ceiling_div_assign_mod(-10), 7);
assert_eq!(x, 3);