1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/// Compute the absolute value of `n`
/// Method described at: <https://bits.stephan-brumme.com/absFloat.html>
///
/// Constant-time, data-independent implementation.
pub(super) fn abs(n: f32) -> f32 {
    f32::from_bits(n.to_bits() & 0x7FFF_FFFF)
}

#[cfg(test)]
mod tests {
    use super::abs;

    #[test]
    fn sanity_check() {
        assert_eq!(abs(1.0), 1.0);
        assert_eq!(abs(0.0), 0.0);
        assert_eq!(abs(-1.0), 1.0);
    }
}