Module malachite_nz::natural::arithmetic::shr_round

source ·
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

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

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

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

§shr_round_assign

use core::cmp::Ordering::*;
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);
assert_eq!(n.shr_round_assign(8u8, Down), Less);
assert_eq!(n, 1);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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