Expand description

Implementations of ShrRound and ShrRoundAssign, traits for dividing a number by a power of 2 and rounding according to a specified RoundingMode.

shr_round

extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_base::rounding_modes::RoundingMode;
use malachite_base::num::arithmetic::traits::ShrRound;
use malachite_nz::natural::Natural;

assert_eq!(Natural::from(0x101u32).shr_round(8u8, RoundingMode::Down), 1);
assert_eq!(Natural::from(0x101u32).shr_round(8u16, RoundingMode::Up), 2);
assert_eq!(Natural::from(0x101u32).shr_round(9u32, RoundingMode::Down), 0);
assert_eq!(Natural::from(0x101u32).shr_round(9u64, RoundingMode::Up), 1);
assert_eq!(Natural::from(0x101u32).shr_round(9u8, RoundingMode::Nearest), 1);
assert_eq!(Natural::from(0xffu32).shr_round(9u16, RoundingMode::Nearest), 0);
assert_eq!(Natural::from(0x100u32).shr_round(9u32, RoundingMode::Nearest), 0);
assert_eq!(Natural::from(0x100u32).shr_round(8u64, RoundingMode::Exact), 1);
assert_eq!((&Natural::from(0x101u32)).shr_round(8u8, RoundingMode::Down), 1);
assert_eq!((&Natural::from(0x101u32)).shr_round(8u16, RoundingMode::Up), 2);
assert_eq!((&Natural::from(0x101u32)).shr_round(9u32, RoundingMode::Down), 0);
assert_eq!((&Natural::from(0x101u32)).shr_round(9u64, RoundingMode::Up), 1);
assert_eq!((&Natural::from(0x101u32)).shr_round(9u8, RoundingMode::Nearest), 1);
assert_eq!((&Natural::from(0xffu32)).shr_round(9u16, RoundingMode::Nearest), 0);
assert_eq!((&Natural::from(0x100u32)).shr_round(9u32, RoundingMode::Nearest), 0);
assert_eq!((&Natural::from(0x100u32)).shr_round(8u64, RoundingMode::Exact), 1);

assert_eq!(Natural::from(0x101u32).shr_round(8i8, RoundingMode::Down), 1);
assert_eq!(Natural::from(0x101u32).shr_round(8i16, RoundingMode::Up), 2);
assert_eq!(Natural::from(0x101u32).shr_round(9i32, RoundingMode::Down), 0);
assert_eq!(Natural::from(0x101u32).shr_round(9i64, RoundingMode::Up), 1);
assert_eq!(Natural::from(0x101u32).shr_round(9i8, RoundingMode::Nearest), 1);
assert_eq!(Natural::from(0xffu32).shr_round(9i16, RoundingMode::Nearest), 0);
assert_eq!(Natural::from(0x100u32).shr_round(9i32, RoundingMode::Nearest), 0);
assert_eq!(Natural::from(0x100u32).shr_round(8i64, RoundingMode::Exact), 1);
assert_eq!(Natural::ZERO.shr_round(-10i8, RoundingMode::Exact), 0);
assert_eq!(Natural::from(123u32).shr_round(-2i16, RoundingMode::Exact), 492);
assert_eq!(
    Natural::from(123u32).shr_round(-100i32, RoundingMode::Exact).to_string(),
    "155921023828072216384094494261248"
);
assert_eq!((&Natural::from(0x101u32)).shr_round(8i8, RoundingMode::Down), 1);
assert_eq!((&Natural::from(0x101u32)).shr_round(8i16, RoundingMode::Up), 2);
assert_eq!((&Natural::from(0x101u32)).shr_round(9i32, RoundingMode::Down), 0);
assert_eq!((&Natural::from(0x101u32)).shr_round(9i64, RoundingMode::Up), 1);
assert_eq!((&Natural::from(0x101u32)).shr_round(9i8, RoundingMode::Nearest), 1);
assert_eq!((&Natural::from(0xffu32)).shr_round(9i16, RoundingMode::Nearest), 0);
assert_eq!((&Natural::from(0x100u32)).shr_round(9i32, RoundingMode::Nearest), 0);
assert_eq!((&Natural::from(0x100u32)).shr_round(8i64, RoundingMode::Exact), 1);
assert_eq!((&Natural::ZERO).shr_round(-10i8, RoundingMode::Exact), 0);
assert_eq!((&Natural::from(123u32)).shr_round(-2i16, RoundingMode::Exact), 492);
assert_eq!(
    (&Natural::from(123u32)).shr_round(-100i32, RoundingMode::Exact).to_string(),
    "155921023828072216384094494261248"
);

shr_round_assign

extern crate malachite_base;

use malachite_base::num::arithmetic::traits::ShrRoundAssign;
use malachite_base::num::basic::traits::One;
use malachite_base::rounding_modes::RoundingMode;
use malachite_nz::natural::Natural;

let mut n = Natural::from(0x101u32);
n.shr_round_assign(8u8, RoundingMode::Down);
assert_eq!(n, 1);

let mut n = Natural::from(0x101u32);
n.shr_round_assign(8u16, RoundingMode::Up);
assert_eq!(n, 2);

let mut n = Natural::from(0x101u32);
n.shr_round_assign(9u32, RoundingMode::Down);
assert_eq!(n, 0);

let mut n = Natural::from(0x101u32);
n.shr_round_assign(9u64, RoundingMode::Up);
assert_eq!(n, 1);

let mut n = Natural::from(0x101u32);
n.shr_round_assign(9u8, RoundingMode::Nearest);
assert_eq!(n, 1);

let mut n = Natural::from(0xffu32);
n.shr_round_assign(9u16, RoundingMode::Nearest);
assert_eq!(n, 0);

let mut n = Natural::from(0x100u32);
n.shr_round_assign(9u32, RoundingMode::Nearest);
assert_eq!(n, 0);

let mut n = Natural::from(0x100u32);
n.shr_round_assign(8u64, RoundingMode::Exact);
assert_eq!(n, 1);

let mut n = Natural::from(0x101u32);
n.shr_round_assign(8i8, RoundingMode::Down);
assert_eq!(n, 1);

let mut n = Natural::from(0x101u32);
n.shr_round_assign(8i16, RoundingMode::Up);
assert_eq!(n, 2);

let mut n = Natural::from(0x101u32);
n.shr_round_assign(9i32, RoundingMode::Down);
assert_eq!(n, 0);

let mut n = Natural::from(0x101u32);
n.shr_round_assign(9i64, RoundingMode::Up);
assert_eq!(n, 1);

let mut n = Natural::from(0x101u32);
n.shr_round_assign(9i8, RoundingMode::Nearest);
assert_eq!(n, 1);

let mut n = Natural::from(0xffu32);
n.shr_round_assign(9i16, RoundingMode::Nearest);
assert_eq!(n, 0);

let mut n = Natural::from(0x100u32);
n.shr_round_assign(9i32, RoundingMode::Nearest);
assert_eq!(n, 0);

let mut n = Natural::from(0x100u32);
n.shr_round_assign(8i64, RoundingMode::Exact);
assert_eq!(n, 1);

let mut x = Natural::ONE;
x.shr_round_assign(-1i8, RoundingMode::Exact);
x.shr_round_assign(-2i16, RoundingMode::Exact);
x.shr_round_assign(-3i32, RoundingMode::Exact);
x.shr_round_assign(-4i64, RoundingMode::Exact);
assert_eq!(x, 1024);