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
29
30
31
use super::SoftF64;

pub(crate) const fn round(x: SoftF64) -> SoftF64 {
    SoftF64::trunc(x.add(SoftF64::copysign(
        SoftF64(0.5).sub(SoftF64(0.25).mul(SoftF64(f64::EPSILON))),
        x,
    )))
}

#[cfg(test)]
mod tests {
    use super::SoftF64;

    #[test]
    fn negative_zero() {
        assert_eq!(
            SoftF64::round(SoftF64(-0.0)).to_bits(),
            SoftF64(-0.0).to_bits()
        );
    }

    #[test]
    fn sanity_check() {
        assert_eq!((SoftF64(-1.0)).round().0, -1.0);
        assert_eq!((SoftF64(2.8)).round().0, 3.0);
        assert_eq!((SoftF64(-0.5)).round().0, -1.0);
        assert_eq!((SoftF64(0.5)).round().0, 1.0);
        assert_eq!((SoftF64(-1.5)).round().0, -2.0);
        assert_eq!((SoftF64(1.5)).round().0, 2.0);
    }
}