]>
Commit | Line | Data |
---|---|---|
49aad941 FG |
1 | // origin: FreeBSD /usr/src/lib/msun/src/s_tan.c */ |
2 | // | |
3 | // ==================================================== | |
4 | // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. | |
5 | // | |
6 | // Developed at SunPro, a Sun Microsystems, Inc. business. | |
7 | // Permission to use, copy, modify, and distribute this | |
8 | // software is freely granted, provided that this notice | |
9 | // is preserved. | |
10 | // ==================================================== | |
11 | ||
12 | use super::{k_tan, rem_pio2}; | |
13 | ||
14 | // tan(x) | |
15 | // Return tangent function of x. | |
16 | // | |
17 | // kernel function: | |
18 | // k_tan ... tangent function on [-pi/4,pi/4] | |
19 | // rem_pio2 ... argument reduction routine | |
20 | // | |
21 | // Method. | |
22 | // Let S,C and T denote the sin, cos and tan respectively on | |
23 | // [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 | |
24 | // in [-pi/4 , +pi/4], and let n = k mod 4. | |
25 | // We have | |
26 | // | |
27 | // n sin(x) cos(x) tan(x) | |
28 | // ---------------------------------------------------------- | |
29 | // 0 S C T | |
30 | // 1 C -S -1/T | |
31 | // 2 -S -C T | |
32 | // 3 -C S -1/T | |
33 | // ---------------------------------------------------------- | |
34 | // | |
35 | // Special cases: | |
36 | // Let trig be any of sin, cos, or tan. | |
37 | // trig(+-INF) is NaN, with signals; | |
38 | // trig(NaN) is that NaN; | |
39 | // | |
40 | // Accuracy: | |
41 | // TRIG(x) returns trig(x) nearly rounded | |
42 | #[inline] | |
43 | #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] | |
44 | pub fn tan(x: f64) -> f64 { | |
45 | let x1p120 = f32::from_bits(0x7b800000); // 0x1p120f === 2 ^ 120 | |
46 | ||
47 | let ix = (f64::to_bits(x) >> 32) as u32 & 0x7fffffff; | |
48 | /* |x| ~< pi/4 */ | |
49 | if ix <= 0x3fe921fb { | |
50 | if ix < 0x3e400000 { | |
51 | /* |x| < 2**-27 */ | |
52 | /* raise inexact if x!=0 and underflow if subnormal */ | |
53 | force_eval!(if ix < 0x00100000 { | |
54 | x / x1p120 as f64 | |
55 | } else { | |
56 | x + x1p120 as f64 | |
57 | }); | |
58 | return x; | |
59 | } | |
60 | return k_tan(x, 0.0, 0); | |
61 | } | |
62 | ||
63 | /* tan(Inf or NaN) is NaN */ | |
64 | if ix >= 0x7ff00000 { | |
65 | return x - x; | |
66 | } | |
67 | ||
68 | /* argument reduction */ | |
69 | let (n, y0, y1) = rem_pio2(x); | |
70 | k_tan(y0, y1, n & 1) | |
71 | } |