1//! Compute the absolute value of a single-precision float.
2//!
3//! Method described at: <https://bits.stephan-brumme.com/absFloat.html>
4
5use super::{F32, SIGN_MASK};
6
7impl F32 {
8 /// Computes the absolute value of `self`.
9 ///
10 /// Returns [`Self::NAN`] if the number is [`Self::NAN`].
11 pub fn abs(self) -> Self {
12 Self::from_bits(self.to_bits() & !SIGN_MASK)
13 }
14}
15
16#[cfg(test)]
17mod tests {
18 use super::F32;
19
20 #[test]
21 fn sanity_check() {
22 assert_eq!(F32::ONE.abs(), 1.0);
23 assert_eq!(F32::ZERO.abs(), 0.0);
24 assert_eq!(F32(-1.0).abs(), 1.0);
25 }
26
27 #[test]
28 fn nan() {
29 assert!(F32::NAN.abs().is_nan());
30 }
31}
32