Module malachite_base::num::arithmetic::shr_round

source ·
Expand description

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::rounding_modes::RoundingMode::*;
use std::cmp::Ordering::*;

assert_eq!(0x101u32.shr_round(8u8, Down), (1, Less));
assert_eq!(0x101u16.shr_round(8u16, Up), (2, Greater));

assert_eq!(0x101u64.shr_round(9u32, Down), (0, Less));
assert_eq!(0x101u32.shr_round(9u64, Up), (1, Greater));
assert_eq!(0x101u16.shr_round(9u8, Nearest), (1, Greater));
assert_eq!(0xffu8.shr_round(9u16, Nearest), (0, Less));
assert_eq!(0x100u32.shr_round(9u32, Nearest), (0, Less));

assert_eq!(0x100u32.shr_round(8u64, Exact), (1, Equal));

assert_eq!(0x101i32.shr_round(8u8, Down), (1, Less));
assert_eq!(0x101i16.shr_round(8u16, Up), (2, Greater));

assert_eq!((-0x101i32).shr_round(9u32, Down), (0, Greater));
assert_eq!((-0x101i64).shr_round(9u64, Up), (-1, Less));
assert_eq!((-0x101i16).shr_round(9u8, Nearest), (-1, Less));
assert_eq!((-0xffi32).shr_round(9u16, Nearest), (0, Greater));
assert_eq!((-0x100i64).shr_round(9u32, Nearest), (0, Greater));

assert_eq!(0x100i32.shr_round(8u64, Exact), (1, Equal));

assert_eq!(0x101u32.shr_round(8i8, Down), (1, Less));
assert_eq!(0x101u16.shr_round(8i16, Up), (2, Greater));

assert_eq!((-0x101i32).shr_round(9i32, Down), (0, Greater));
assert_eq!((-0x101i64).shr_round(9i64, Up), (-1, Less));
assert_eq!((-0x101i16).shr_round(9i8, Nearest), (-1, Less));
assert_eq!((-0xffi32).shr_round(9i16, Nearest), (0, Greater));
assert_eq!((-0x100i64).shr_round(9i32, Nearest), (0, Greater));

assert_eq!(0x100u32.shr_round(8i64, Exact), (1, Equal));

§shr_round_assign

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

let mut x = 0x101u32;
assert_eq!(x.shr_round_assign(8u8, Down), Less);
assert_eq!(x, 1);

let mut x = 0x101u16;
assert_eq!(x.shr_round_assign(8u16, Up), Greater);
assert_eq!(x, 2);

let mut x = 0x101u64;
assert_eq!(x.shr_round_assign(9u32, Down), Less);
assert_eq!(x, 0);

let mut x = 0x101u32;
assert_eq!(x.shr_round_assign(9u64, Up), Greater);
assert_eq!(x, 1);

let mut x = 0x101u16;
assert_eq!(x.shr_round_assign(9u8, Nearest), Greater);
assert_eq!(x, 1);

let mut x = 0xffu8;
assert_eq!(x.shr_round_assign(9u16, Nearest), Less);
assert_eq!(x, 0);

let mut x = 0x100u32;
assert_eq!(x.shr_round_assign(9u32, Nearest), Less);
assert_eq!(x, 0);

let mut x = 0x100u32;
assert_eq!(x.shr_round_assign(8u64, Exact), Equal);
assert_eq!(x, 1);

let mut x = 0x101i32;
assert_eq!(x.shr_round_assign(8u8, Down), Less);
assert_eq!(x, 1);

let mut x = 0x101i16;
assert_eq!(x.shr_round_assign(8u16, Up), Greater);
assert_eq!(x, 2);

let mut x = -0x101i32;
assert_eq!(x.shr_round_assign(9u32, Down), Greater);
assert_eq!(x, 0);

let mut x = -0x101i64;
assert_eq!(x.shr_round_assign(9u64, Up), Less);
assert_eq!(x, -1);

let mut x = -0x101i16;
assert_eq!(x.shr_round_assign(9u8, Nearest), Less);
assert_eq!(x, -1);

let mut x = -0xffi32;
assert_eq!(x.shr_round_assign(9u16, Nearest), Greater);
assert_eq!(x, 0);

let mut x = -0x100i64;
assert_eq!(x.shr_round_assign(9u32, Nearest), Greater);
assert_eq!(x, 0);

let mut x = 0x100u32;
assert_eq!(x.shr_round_assign(8i64, Exact), Equal);
assert_eq!(x, 1);

let mut x = 0x101u32;
assert_eq!(x.shr_round_assign(8i8, Down), Less);
assert_eq!(x, 1);

let mut x = 0x101u16;
assert_eq!(x.shr_round_assign(8i16, Up), Greater);
assert_eq!(x, 2);

let mut x = -0x101i32;
assert_eq!(x.shr_round_assign(9i32, Down), Greater);
assert_eq!(x, 0);

let mut x = -0x101i64;
assert_eq!(x.shr_round_assign(9i64, Up), Less);
assert_eq!(x, -1);

let mut x = -0x101i16;
assert_eq!(x.shr_round_assign(9i8, Nearest), Less);
assert_eq!(x, -1);

let mut x = -0xffi32;
assert_eq!(x.shr_round_assign(9i16, Nearest), Greater);
assert_eq!(x, 0);

let mut x = -0x100i64;
assert_eq!(x.shr_round_assign(9i32, Nearest), Greater);
assert_eq!(x, 0);

let mut x = 0x100u32;
assert_eq!(x.shr_round_assign(8i64, Exact), Equal);
assert_eq!(x, 1);