libm/math/
roundf.rs

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
use core::f32;

use super::{copysignf, truncf};

#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn roundf(x: f32) -> f32 {
    truncf(x + copysignf(0.5 - 0.25 * f32::EPSILON, x))
}

// PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
#[cfg(not(target_arch = "powerpc64"))]
#[cfg(test)]
mod tests {
    use super::roundf;

    #[test]
    fn negative_zero() {
        assert_eq!(roundf(-0.0_f32).to_bits(), (-0.0_f32).to_bits());
    }

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