Skip to main content

Module balanced_mod

Module balanced_mod 

Source
Expand description

BalancedMod and BalancedModAssign, traits for finding the representative of a number modulo another number that is closest to zero.

§balanced_mod

use malachite_base::num::arithmetic::traits::BalancedMod;

assert_eq!(23u32.balanced_mod(10), 3);
// 7 is more than half of 10, so the closest representative is negative
assert_eq!(27u32.balanced_mod(10), -3);
// exactly half the modulus is the top of the range, so it stays positive
assert_eq!(25u32.balanced_mod(10), 5);

assert_eq!((-23i32).balanced_mod(10), -3);
// only the magnitude of the modulus matters
assert_eq!(27i32.balanced_mod(-10), -3);

§balanced_mod_assign

use malachite_base::num::arithmetic::traits::BalancedModAssign;

let mut x = 27i32;
x.balanced_mod_assign(10);
assert_eq!(x, -3);