1 | use super::copysign; |
2 | use super::trunc; |
3 | use core::f64; |
4 | |
5 | #[cfg_attr (all(test, assert_no_panic), no_panic::no_panic)] |
6 | pub fn round(x: f64) -> f64 { |
7 | trunc(x + copysign(x:0.5 - 0.25 * f64::EPSILON, y:x)) |
8 | } |
9 | |
10 | #[cfg (test)] |
11 | mod tests { |
12 | use super::round; |
13 | |
14 | #[test ] |
15 | fn negative_zero() { |
16 | assert_eq!(round(-0.0_f64).to_bits(), (-0.0_f64).to_bits()); |
17 | } |
18 | |
19 | #[test ] |
20 | fn sanity_check() { |
21 | assert_eq!(round(-1.0), -1.0); |
22 | assert_eq!(round(2.8), 3.0); |
23 | assert_eq!(round(-0.5), -1.0); |
24 | assert_eq!(round(0.5), 1.0); |
25 | assert_eq!(round(-1.5), -2.0); |
26 | assert_eq!(round(1.5), 2.0); |
27 | } |
28 | } |
29 | |