diff options
Diffstat (limited to 'vendor/libm/src/math/round.rs')
-rw-r--r-- | vendor/libm/src/math/round.rs | 49 |
1 files changed, 20 insertions, 29 deletions
diff --git a/vendor/libm/src/math/round.rs b/vendor/libm/src/math/round.rs index 9a9723cfb..46fabc90f 100644 --- a/vendor/libm/src/math/round.rs +++ b/vendor/libm/src/math/round.rs @@ -1,37 +1,28 @@ +use super::copysign; +use super::trunc; use core::f64; -const TOINT: f64 = 1.0 / f64::EPSILON; - -#[inline] #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] -pub fn round(mut x: f64) -> f64 { - let (f, i) = (x, x.to_bits()); - let e: u64 = i >> 52 & 0x7ff; - let mut y: f64; +pub fn round(x: f64) -> f64 { + trunc(x + copysign(0.5 - 0.25 * f64::EPSILON, x)) +} - if e >= 0x3ff + 52 { - return x; - } - if i >> 63 != 0 { - x = -x; - } - if e < 0x3ff - 1 { - // raise inexact if x!=0 - force_eval!(x + TOINT); - return 0.0 * f; - } - y = x + TOINT - TOINT - x; - if y > 0.5 { - y = y + x - 1.0; - } else if y <= -0.5 { - y = y + x + 1.0; - } else { - y = y + x; +#[cfg(test)] +mod tests { + use super::round; + + #[test] + fn negative_zero() { + assert_eq!(round(-0.0_f64).to_bits(), (-0.0_f64).to_bits()); } - if i >> 63 != 0 { - -y - } else { - y + #[test] + fn sanity_check() { + assert_eq!(round(-1.0), -1.0); + assert_eq!(round(2.8), 3.0); + assert_eq!(round(-0.5), -1.0); + assert_eq!(round(0.5), 1.0); + assert_eq!(round(-1.5), -2.0); + assert_eq!(round(1.5), 2.0); } } |