Module malachite_base::num::arithmetic::round_to_multiple

source ·
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::*;
use std::cmp::Ordering::*;

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

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

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

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

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

§round_to_multiple_assign

use malachite_base::num::arithmetic::traits::RoundToMultipleAssign;
use malachite_base::rounding_modes::RoundingMode::*;
use std::cmp::Ordering::*;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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