Expand description

RoundToMultipleOfPowerOf2 and RoundToMultipleOfPowerOf2Assign, traits for rounding a number to a multiple of a power of 2.

§round_to_multiple_of_power_of_2

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

assert_eq!(10u8.round_to_multiple_of_power_of_2(2, Floor), (8, Less));
assert_eq!(
    10u8.round_to_multiple_of_power_of_2(2, Ceiling),
    (12, Greater)
);
assert_eq!(10u8.round_to_multiple_of_power_of_2(2, Down), (8, Less));
assert_eq!(10u8.round_to_multiple_of_power_of_2(2, Up), (12, Greater));
assert_eq!(10u8.round_to_multiple_of_power_of_2(2, Nearest), (8, Less));
assert_eq!(12u8.round_to_multiple_of_power_of_2(2, Exact), (12, Equal));

assert_eq!(
    (-10i8).round_to_multiple_of_power_of_2(2, Floor),
    (-12, Less)
);
assert_eq!(
    (-10i8).round_to_multiple_of_power_of_2(2, Ceiling),
    (-8, Greater)
);
assert_eq!(
    (-10i8).round_to_multiple_of_power_of_2(2, Down),
    (-8, Greater)
);
assert_eq!((-10i8).round_to_multiple_of_power_of_2(2, Up), (-12, Less));
assert_eq!(
    (-10i8).round_to_multiple_of_power_of_2(2, Nearest),
    (-8, Greater)
);
assert_eq!(
    (-12i8).round_to_multiple_of_power_of_2(2, Exact),
    (-12, Equal)
);

§round_to_multiple_of_power_of_2_assign

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

let mut x = 10u8;
assert_eq!(x.round_to_multiple_of_power_of_2_assign(2, Floor), Less);
assert_eq!(x, 8);

let mut x = 10u8;
assert_eq!(
    x.round_to_multiple_of_power_of_2_assign(2, Ceiling),
    Greater
);
assert_eq!(x, 12);

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

let mut x = 10u8;
assert_eq!(x.round_to_multiple_of_power_of_2_assign(2, Up), Greater);
assert_eq!(x, 12);

let mut x = 10u8;
assert_eq!(x.round_to_multiple_of_power_of_2_assign(2, Nearest), Less);
assert_eq!(x, 8);

let mut x = 12u8;
assert_eq!(x.round_to_multiple_of_power_of_2_assign(2, Exact), Equal);
assert_eq!(x, 12);

let mut x = -10i8;
assert_eq!(x.round_to_multiple_of_power_of_2_assign(2, Floor), Less);
assert_eq!(x, -12);

let mut x = -10i8;
assert_eq!(
    x.round_to_multiple_of_power_of_2_assign(2, Ceiling),
    Greater
);
assert_eq!(x, -8);

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

let mut x = -10i8;
assert_eq!(x.round_to_multiple_of_power_of_2_assign(2, Up), Less);
assert_eq!(x, -12);

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

let mut x = -12i8;
assert_eq!(x.round_to_multiple_of_power_of_2_assign(2, Exact), Equal);
assert_eq!(x, -12);