const_soft_float/soft_f64/
round.rs

1use super::SoftF64;
2
3pub(crate) const fn round(x: SoftF64) -> SoftF64 {
4    SoftF64::trunc(x.add(SoftF64::copysign(
5        SoftF64(0.5).sub(SoftF64(0.25).mul(SoftF64(f64::EPSILON))),
6        x,
7    )))
8}
9
10#[cfg(test)]
11mod tests {
12    use super::SoftF64;
13
14    #[test]
15    fn negative_zero() {
16        assert_eq!(
17            SoftF64::round(SoftF64(-0.0)).to_bits(),
18            SoftF64(-0.0).to_bits()
19        );
20    }
21
22    #[test]
23    fn sanity_check() {
24        assert_eq!((SoftF64(-1.0)).round().0, -1.0);
25        assert_eq!((SoftF64(2.8)).round().0, 3.0);
26        assert_eq!((SoftF64(-0.5)).round().0, -1.0);
27        assert_eq!((SoftF64(0.5)).round().0, 1.0);
28        assert_eq!((SoftF64(-1.5)).round().0, -2.0);
29        assert_eq!((SoftF64(1.5)).round().0, 2.0);
30    }
31}