1 | /// Absolute value (magnitude) (f64) |
2 | /// |
3 | /// Calculates the absolute value (magnitude) of the argument `x`, |
4 | /// by direct manipulation of the bit representation of `x`. |
5 | #[cfg_attr (all(test, assert_no_panic), no_panic::no_panic)] |
6 | pub fn fabs(x: f64) -> f64 { |
7 | select_implementation! { |
8 | name: fabs, |
9 | use_arch: all(target_arch = "wasm32" , intrinsics_enabled), |
10 | args: x, |
11 | } |
12 | |
13 | super::generic::fabs(x) |
14 | } |
15 | |
16 | #[cfg (test)] |
17 | mod tests { |
18 | use super::*; |
19 | |
20 | #[test ] |
21 | fn sanity_check() { |
22 | assert_eq!(fabs(-1.0), 1.0); |
23 | assert_eq!(fabs(2.8), 2.8); |
24 | } |
25 | |
26 | /// The spec: https://en.cppreference.com/w/cpp/numeric/math/fabs |
27 | #[test ] |
28 | fn spec_tests() { |
29 | assert!(fabs(f64::NAN).is_nan()); |
30 | for f in [0.0, -0.0].iter().copied() { |
31 | assert_eq!(fabs(f), 0.0); |
32 | } |
33 | for f in [f64::INFINITY, f64::NEG_INFINITY].iter().copied() { |
34 | assert_eq!(fabs(f), f64::INFINITY); |
35 | } |
36 | } |
37 | } |
38 | |