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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
pub(super) fn sqrt_approx(x: f32) -> f32 {
f32::from_bits((x.to_bits() + 0x3f80_0000) >> 1)
}
#[cfg(test)]
pub(super) mod tests {
use super::sqrt_approx;
pub(crate) const MAX_ERROR: f32 = 0.05;
pub(crate) const TEST_VECTORS: &[(f32, f32)] = &[
(1.0, 1.0),
(2.0, 1.414),
(3.0, 1.732),
(4.0, 2.0),
(5.0, 2.236),
(10.0, 3.162),
(100.0, 10.0),
(250.0, 15.811),
(500.0, 22.36),
(1000.0, 31.622),
(2500.0, 50.0),
(5000.0, 70.710),
(1000000.0, 1000.0),
(2500000.0, 1581.138),
(5000000.0, 2236.067),
(10000000.0, 3162.277),
(25000000.0, 5000.0),
(50000000.0, 7071.067),
(100000000.0, 10000.0),
];
#[test]
fn sanity_check() {
for (x, expected) in TEST_VECTORS {
let sqrt_x = sqrt_approx(*x);
let allowed_delta = x * MAX_ERROR;
let actual_delta = sqrt_x - expected;
assert!(
actual_delta <= allowed_delta,
"delta {} too large: {} vs {}",
actual_delta,
sqrt_x,
expected
);
}
}
}