Skip to main content

embedded_maths/embedded-maths-functions/
inv.rs

1// SPDX-License-Identifier: GPL-2.0-or-later
2//! Reciprocal operations for `f32`.
3
4/// Returns the multiplicative inverse of `x`.
5#[inline(always)]
6pub fn inv(x: f32) -> f32 {
7    1.0 / x
8}
9
10#[cfg(test)]
11mod tests {
12    use super::inv;
13
14    #[test]
15    fn known_values() {
16        assert_eq!(inv(4.0), 0.25);
17        assert_eq!(inv(-2.0), -0.5);
18        assert_eq!(inv(1.0), 1.0);
19    }
20
21    #[test]
22    fn zero_and_infinity() {
23        assert_eq!(inv(0.0), f32::INFINITY);
24        assert_eq!(inv(f32::INFINITY), 0.0);
25    }
26}