1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
pub(super) fn inv_approx(x: f32) -> f32 {
f32::from_bits(0x7f00_0000 - x.to_bits())
}
#[cfg(test)]
pub(super) mod tests {
use super::inv_approx;
pub(crate) const MAX_ERROR: f32 = 0.08;
#[test]
fn sanity_check() {
for x in 0..100 {
let x = x as f32;
let inv_x = inv_approx(x);
let expected = 1.0 / x;
let allowed_delta = x * MAX_ERROR;
let actual_delta = inv_x - expected;
assert!(
actual_delta <= allowed_delta,
"delta {} too large: {} vs {}",
actual_delta,
inv_x,
expected
);
}
}
}