Module shr_round

Source
Expand description

Implementations of ShlRound and ShrRoundAssign, traits for dividing a number by a power of 2 and rounding according to a specified RoundingMode. For Floats, rounding is only necessary in the cases of overflow and underflow.

§shr_round

use malachite_base::num::arithmetic::traits::ShrRound;
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use std::cmp::Ordering::*;

let (shifted, o) = Float::from(std::f64::consts::PI).shr_round(10u8, Nearest);
assert_eq!(shifted.to_string(), "0.003067961575771282");
assert_eq!(o, Equal);

let (shifted, o) = Float::from(std::f64::consts::PI).shr_round(-10i8, Nearest);
assert_eq!(shifted.to_string(), "3216.990877275948");
assert_eq!(o, Equal);

let (shifted, o) = Float::from(std::f64::consts::PI).shr_round(u32::MAX, Floor);
assert_eq!(shifted.to_string(), "0.0");
assert_eq!(o, Less);

let (shifted, o) = Float::from(std::f64::consts::PI).shr_round(u32::MAX, Ceiling);
assert_eq!(shifted.to_string(), "too_small");
assert_eq!(o, Greater);

let (shifted, o) = (&Float::from(std::f64::consts::PI)).shr_round(10u8, Nearest);
assert_eq!(shifted.to_string(), "0.003067961575771282");
assert_eq!(o, Equal);

let (shifted, o) = (&Float::from(std::f64::consts::PI)).shr_round(-10i8, Nearest);
assert_eq!(shifted.to_string(), "3216.990877275948");
assert_eq!(o, Equal);

let (shifted, o) = (&Float::from(std::f64::consts::PI)).shr_round(u32::MAX, Floor);
assert_eq!(shifted.to_string(), "0.0");
assert_eq!(o, Less);

let (shifted, o) = (&Float::from(std::f64::consts::PI)).shr_round(u32::MAX, Ceiling);
assert_eq!(shifted.to_string(), "too_small");
assert_eq!(o, Greater);

§shr_assign

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

let mut x = Float::from(std::f64::consts::PI);
assert_eq!(x.shr_round_assign(10u8, Nearest), Equal);
assert_eq!(x.to_string(), "0.003067961575771282");

let mut x = Float::from(std::f64::consts::PI);
assert_eq!(x.shr_round_assign(-10i8, Nearest), Equal);
assert_eq!(x.to_string(), "3216.990877275948");

let mut x = Float::from(std::f64::consts::PI);
assert_eq!(x.shr_round_assign(u32::MAX, Floor), Less);
assert_eq!(x.to_string(), "0.0");

let mut x = Float::from(std::f64::consts::PI);
assert_eq!(x.shr_round_assign(u32::MAX, Ceiling), Greater);
assert_eq!(x.to_string(), "too_small");