Expand description

RoundToMultiple and RoundToMultipleAssign, traits for rounding a number to a multiple of another number.

round_to_multiple

use malachite_base::num::arithmetic::traits::RoundToMultiple;
use malachite_base::rounding_modes::RoundingMode;

assert_eq!(5u32.round_to_multiple(0, RoundingMode::Down), 0);

assert_eq!(10u8.round_to_multiple(4, RoundingMode::Down), 8);
assert_eq!(10u16.round_to_multiple(4, RoundingMode::Up), 12);
assert_eq!(10u32.round_to_multiple(5, RoundingMode::Exact), 10);
assert_eq!(10u64.round_to_multiple(3, RoundingMode::Nearest), 9);
assert_eq!(20u128.round_to_multiple(3, RoundingMode::Nearest), 21);
assert_eq!(10usize.round_to_multiple(4, RoundingMode::Nearest), 8);
assert_eq!(14u8.round_to_multiple(4, RoundingMode::Nearest), 16);

assert_eq!((-5i32).round_to_multiple(0, RoundingMode::Down), 0);

assert_eq!((-10i8).round_to_multiple(4, RoundingMode::Down), -8);
assert_eq!((-10i16).round_to_multiple(4, RoundingMode::Up), -12);
assert_eq!((-10i32).round_to_multiple(5, RoundingMode::Exact), -10);
assert_eq!((-10i64).round_to_multiple(3, RoundingMode::Nearest), -9);
assert_eq!((-20i128).round_to_multiple(3, RoundingMode::Nearest), -21);
assert_eq!((-10isize).round_to_multiple(4, RoundingMode::Nearest), -8);
assert_eq!((-14i8).round_to_multiple(4, RoundingMode::Nearest), -16);

assert_eq!((-10i16).round_to_multiple(-4, RoundingMode::Down), -8);
assert_eq!((-10i32).round_to_multiple(-4, RoundingMode::Up), -12);
assert_eq!((-10i64).round_to_multiple(-5, RoundingMode::Exact), -10);
assert_eq!((-10i128).round_to_multiple(-3, RoundingMode::Nearest), -9);
assert_eq!((-20isize).round_to_multiple(-3, RoundingMode::Nearest), -21);
assert_eq!((-10i8).round_to_multiple(-4, RoundingMode::Nearest), -8);
assert_eq!((-14i16).round_to_multiple(-4, RoundingMode::Nearest), -16);

round_to_multiple_assign

use malachite_base::num::arithmetic::traits::RoundToMultipleAssign;
use malachite_base::rounding_modes::RoundingMode;

let mut x = 5u32;
x.round_to_multiple_assign(0, RoundingMode::Down);
assert_eq!(x, 0);

let mut x = 10u8;
x.round_to_multiple_assign(4, RoundingMode::Down);
assert_eq!(x, 8);

let mut x = 10u16;
x.round_to_multiple_assign(4, RoundingMode::Up);
assert_eq!(x, 12);

let mut x = 10u32;
x.round_to_multiple_assign(5, RoundingMode::Exact);
assert_eq!(x, 10);

let mut x = 10u64;
x.round_to_multiple_assign(3, RoundingMode::Nearest);
assert_eq!(x, 9);

let mut x = 20u128;
x.round_to_multiple_assign(3, RoundingMode::Nearest);
assert_eq!(x, 21);

let mut x = 10usize;
x.round_to_multiple_assign(4, RoundingMode::Nearest);
assert_eq!(x, 8);

let mut x = 14u8;
x.round_to_multiple_assign(4, RoundingMode::Nearest);
assert_eq!(x, 16);

let mut x = -5i32;
x.round_to_multiple_assign(0, RoundingMode::Down);
assert_eq!(x, 0);

let mut x = -10i8;
x.round_to_multiple_assign(4, RoundingMode::Down);
assert_eq!(x, -8);

let mut x = -10i16;
x.round_to_multiple_assign(4, RoundingMode::Up);
assert_eq!(x, -12);

let mut x = -10i32;
x.round_to_multiple_assign(5, RoundingMode::Exact);
assert_eq!(x, -10);

let mut x = -10i64;
x.round_to_multiple_assign(3, RoundingMode::Nearest);
assert_eq!(x, -9);

let mut x = -20i128;
x.round_to_multiple_assign(3, RoundingMode::Nearest);
assert_eq!(x, -21);

let mut x = -10isize;
x.round_to_multiple_assign(4, RoundingMode::Nearest);
assert_eq!(x, -8);

let mut x = -14i8;
x.round_to_multiple_assign(4, RoundingMode::Nearest);
assert_eq!(x, -16);

let mut x = -10i16;
x.round_to_multiple_assign(-4, RoundingMode::Down);
assert_eq!(x, -8);

let mut x = -10i32;
x.round_to_multiple_assign(-4, RoundingMode::Up);
assert_eq!(x, -12);

let mut x = -10i64;
x.round_to_multiple_assign(-5, RoundingMode::Exact);
assert_eq!(x, -10);

let mut x = -10i128;
x.round_to_multiple_assign(-3, RoundingMode::Nearest);
assert_eq!(x, -9);

let mut x = -20isize;
x.round_to_multiple_assign(-3, RoundingMode::Nearest);
assert_eq!(x, -21);

let mut x = -10i8;
x.round_to_multiple_assign(-4, RoundingMode::Nearest);
assert_eq!(x, -8);

let mut x = -14i16;
x.round_to_multiple_assign(-4, RoundingMode::Nearest);
assert_eq!(x, -16);