| 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 | |
| 43 | /// The tangent of `x` (f64). |
| 44 | /// |
| 45 | /// `x` is specified in radians. |
| 46 | #[cfg_attr (all(test, assert_no_panic), no_panic::no_panic)] |
| 47 | pub fn tan(x: f64) -> f64 { |
| 48 | let x1p120 = f32::from_bits(0x7b800000); // 0x1p120f === 2 ^ 120 |
| 49 | |
| 50 | let ix = (f64::to_bits(x) >> 32) as u32 & 0x7fffffff; |
| 51 | /* |x| ~< pi/4 */ |
| 52 | if ix <= 0x3fe921fb { |
| 53 | if ix < 0x3e400000 { |
| 54 | /* |x| < 2**-27 */ |
| 55 | /* raise inexact if x!=0 and underflow if subnormal */ |
| 56 | force_eval!(if ix < 0x00100000 { |
| 57 | x / x1p120 as f64 |
| 58 | } else { |
| 59 | x + x1p120 as f64 |
| 60 | }); |
| 61 | return x; |
| 62 | } |
| 63 | return k_tan(x, 0.0, 0); |
| 64 | } |
| 65 | |
| 66 | /* tan(Inf or NaN) is NaN */ |
| 67 | if ix >= 0x7ff00000 { |
| 68 | return x - x; |
| 69 | } |
| 70 | |
| 71 | /* argument reduction */ |
| 72 | let (n, y0, y1) = rem_pio2(x); |
| 73 | k_tan(y0, y1, n & 1) |
| 74 | } |
| 75 | |