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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use super::F32;
use core::f32::consts::FRAC_PI_2;
impl F32 {
pub fn atan(self) -> Self {
FRAC_PI_2 * self.atan_norm()
}
pub fn atan_norm(self) -> Self {
const SIGN_MASK: u32 = 0x8000_0000;
const B: f32 = 0.596_227;
let ux_s = SIGN_MASK & self.to_bits();
let bx_a = (B * self).abs();
let n = bx_a + self * self;
let atan_1q = n / (1.0 + bx_a + n);
Self::from_bits(ux_s | atan_1q.to_bits())
}
}
#[cfg(test)]
mod tests {
use super::F32;
use core::f32::consts;
const MAX_ERROR: f32 = 0.003;
#[test]
fn sanity_check() {
let test_vectors: &[(f32, f32)] = &[
(3.0_f32.sqrt() / 3.0, consts::FRAC_PI_6),
(1.0, consts::FRAC_PI_4),
(3.0_f32.sqrt(), consts::FRAC_PI_3),
(-(3.0_f32.sqrt()) / 3.0, -consts::FRAC_PI_6),
(-1.0, -consts::FRAC_PI_4),
(-(3.0_f32.sqrt()), -consts::FRAC_PI_3),
];
for &(x, expected) in test_vectors {
let actual = F32(x).atan().0;
let delta = actual - expected;
assert!(
delta <= MAX_ERROR,
"delta {} too large: {} vs {}",
delta,
actual,
expected
);
}
}
#[test]
fn zero() {
assert_eq!(F32::ZERO.atan(), F32::ZERO);
}
#[test]
fn nan() {
assert!(F32::NAN.atan().is_nan());
}
}