Skip to content

Commit d731dcb

Browse files
committed
Implement atan
1 parent d30c103 commit d731dcb

File tree

4 files changed

+173
-3
lines changed

4 files changed

+173
-3
lines changed

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,6 @@ pub trait F64Ext: private::Sealed + Sized {
374374

375375
fn acos(self) -> Self;
376376

377-
#[cfg(todo)]
378377
fn atan(self) -> Self;
379378

380379
#[cfg(todo)]
@@ -541,7 +540,6 @@ impl F64Ext for f64 {
541540
acos(self)
542541
}
543542

544-
#[cfg(todo)]
545543
#[inline]
546544
fn atan(self) -> Self {
547545
atan(self)

src/math/atan.rs

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/* atan(x)
2+
* Method
3+
* 1. Reduce x to positive by atan(x) = -atan(-x).
4+
* 2. According to the integer k=4t+0.25 chopped, t=x, the argument
5+
* is further reduced to one of the following intervals and the
6+
* arctangent of t is evaluated by the corresponding formula:
7+
*
8+
* [0,7/16] atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...)
9+
* [7/16,11/16] atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) )
10+
* [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) )
11+
* [19/16,39/16] atan(x) = atan(3/2) + atan( (t-1.5)/(1+1.5t) )
12+
* [39/16,INF] atan(x) = atan(INF) + atan( -1/t )
13+
*
14+
* Constants:
15+
* The hexadecimal values are the intended ones for the following
16+
* constants. The decimal values may be used, provided that the
17+
* compiler will convert from decimal to binary accurately enough
18+
* to produce the hexadecimal values shown.
19+
*/
20+
21+
use super::fabs;
22+
use core::f64;
23+
24+
const ATANHI: [f64; 4] = [
25+
4.63647609000806093515e-01, /* atan(0.5)hi 0x3FDDAC67, 0x0561BB4F */
26+
7.85398163397448278999e-01, /* atan(1.0)hi 0x3FE921FB, 0x54442D18 */
27+
9.82793723247329054082e-01, /* atan(1.5)hi 0x3FEF730B, 0xD281F69B */
28+
1.57079632679489655800e+00, /* atan(inf)hi 0x3FF921FB, 0x54442D18 */
29+
];
30+
31+
const ATANLO: [f64; 4] = [
32+
2.26987774529616870924e-17, /* atan(0.5)lo 0x3C7A2B7F, 0x222F65E2 */
33+
3.06161699786838301793e-17, /* atan(1.0)lo 0x3C81A626, 0x33145C07 */
34+
1.39033110312309984516e-17, /* atan(1.5)lo 0x3C700788, 0x7AF0CBBD */
35+
6.12323399573676603587e-17, /* atan(inf)lo 0x3C91A626, 0x33145C07 */
36+
];
37+
38+
const AT: [f64; 11] = [
39+
3.33333333333329318027e-01, /* 0x3FD55555, 0x5555550D */
40+
-1.99999999998764832476e-01, /* 0xBFC99999, 0x9998EBC4 */
41+
1.42857142725034663711e-01, /* 0x3FC24924, 0x920083FF */
42+
-1.11111104054623557880e-01, /* 0xBFBC71C6, 0xFE231671 */
43+
9.09088713343650656196e-02, /* 0x3FB745CD, 0xC54C206E */
44+
-7.69187620504482999495e-02, /* 0xBFB3B0F2, 0xAF749A6D */
45+
6.66107313738753120669e-02, /* 0x3FB10D66, 0xA0D03D51 */
46+
-5.83357013379057348645e-02, /* 0xBFADDE2D, 0x52DEFD9A */
47+
4.97687799461593236017e-02, /* 0x3FA97B4B, 0x24760DEB */
48+
-3.65315727442169155270e-02, /* 0xBFA2B444, 0x2C6A6C2F */
49+
1.62858201153657823623e-02, /* 0x3F90AD3A, 0xE322DA11 */
50+
];
51+
52+
#[inline]
53+
pub fn atan(x: f64) -> f64 {
54+
let mut x = x;
55+
let mut ix = (x.to_bits() >> 32) as u32;
56+
let sign = ix >> 31;
57+
ix &= 0x7fff_ffff;
58+
if ix >= 0x4410_0000 {
59+
if x.is_nan() {
60+
return x;
61+
}
62+
63+
let z = ATANHI[3] + f64::from_bits(0x0380_0000); // 0x1p-120f
64+
return if sign != 0 { -z } else { z };
65+
}
66+
67+
let id = if ix < 0x3fdc_0000 {
68+
/* |x| < 0.4375 */
69+
if ix < 0x3e40_0000 {
70+
/* |x| < 2^-27 */
71+
if ix < 0x0010_0000 {
72+
/* raise underflow for subnormal x */
73+
force_eval!(x as f32);
74+
}
75+
76+
return x;
77+
}
78+
79+
-1
80+
} else {
81+
x = fabs(x);
82+
if ix < 0x3ff30000 {
83+
/* |x| < 1.1875 */
84+
if ix < 0x3fe60000 {
85+
/* 7/16 <= |x| < 11/16 */
86+
x = (2. * x - 1.) / (2. + x);
87+
0
88+
} else {
89+
/* 11/16 <= |x| < 19/16 */
90+
x = (x - 1.) / (x + 1.);
91+
1
92+
}
93+
} else {
94+
if ix < 0x40038000 {
95+
/* |x| < 2.4375 */
96+
x = (x - 1.5) / (1. + 1.5 * x);
97+
2
98+
} else {
99+
/* 2.4375 <= |x| < 2^66 */
100+
x = -1. / x;
101+
3
102+
}
103+
}
104+
};
105+
106+
let z = x * x;
107+
let w = z * z;
108+
/* break sum from i=0 to 10 AT[i]z**(i+1) into odd and even poly */
109+
let s1 = z * (AT[0] + w * (AT[2] + w * (AT[4] + w * (AT[6] + w * (AT[8] + w * AT[10])))));
110+
let s2 = w * (AT[1] + w * (AT[3] + w * (AT[5] + w * (AT[7] + w * AT[9]))));
111+
112+
if id < 0 {
113+
return x - x * (s1 + s2);
114+
}
115+
116+
let z = ATANHI[id as usize] - (x * (s1 + s2) - ATANLO[id as usize] - x);
117+
118+
if sign != 0 {
119+
-z
120+
} else {
121+
z
122+
}
123+
}
124+
125+
#[cfg(test)]
126+
mod tests {
127+
use super::atan;
128+
use core::f64;
129+
130+
#[test]
131+
fn sanity_check() {
132+
for (input, answer) in [
133+
(3.0_f64.sqrt() / 3.0, f64::consts::FRAC_PI_6),
134+
(1.0, f64::consts::FRAC_PI_4),
135+
(3.0_f64.sqrt(), f64::consts::FRAC_PI_3),
136+
(-3.0_f64.sqrt() / 3.0, -f64::consts::FRAC_PI_6),
137+
(-1.0, -f64::consts::FRAC_PI_4),
138+
(-3.0_f64.sqrt(), -f64::consts::FRAC_PI_3),
139+
].iter()
140+
{
141+
assert!(
142+
(atan(*input) - answer) / answer < 1e-5,
143+
"\natan({:.4}/16) = {:.4}, actual: {}",
144+
input * 16.0,
145+
answer,
146+
atan(*input)
147+
);
148+
}
149+
}
150+
151+
#[test]
152+
fn zero() {
153+
assert_eq!(atan(0.0), 0.0);
154+
}
155+
156+
#[test]
157+
fn infinity() {
158+
assert_eq!(atan(f64::INFINITY), f64::consts::FRAC_PI_2);
159+
}
160+
161+
#[test]
162+
fn minus_infinity() {
163+
assert_eq!(atan(f64::NEG_INFINITY), -f64::consts::FRAC_PI_2);
164+
}
165+
166+
#[test]
167+
fn nan() {
168+
assert!(atan(f64::NAN).is_nan());
169+
}
170+
}

src/math/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod acos;
1111
mod acosf;
1212
mod asin;
1313
mod asinf;
14+
mod atan;
1415
mod atan2f;
1516
mod atanf;
1617
mod cbrt;
@@ -70,6 +71,7 @@ pub use self::acos::acos;
7071
pub use self::acosf::acosf;
7172
pub use self::asin::asin;
7273
pub use self::asinf::asinf;
74+
pub use self::atan::atan;
7375
pub use self::atan2f::atan2f;
7476
pub use self::atanf::atanf;
7577
pub use self::cbrt::cbrt;

test-generator/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ f32i32_f32! {
745745
f64_f64! {
746746
acos,
747747
asin,
748-
// atan,
748+
atan,
749749
cbrt,
750750
ceil,
751751
cos,

0 commit comments

Comments
 (0)