/rust/registry/src/index.crates.io-1949cf8c6b5b557f/libm-0.2.11/src/math/round.rs
Line | Count | Source |
1 | | use core::f64; |
2 | | |
3 | | use super::{copysign, trunc}; |
4 | | |
5 | | #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] |
6 | 0 | pub fn round(x: f64) -> f64 { |
7 | 0 | trunc(x + copysign(0.5 - 0.25 * f64::EPSILON, x)) |
8 | 0 | } |
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 | | } |