1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use core::f64;

use super::{copysign, trunc};

#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn round(x: f64) -> f64 {
    trunc(x + copysign(0.5 - 0.25 * f64::EPSILON, 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());
    }

    #[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);
    }
}