/rust/registry/src/index.crates.io-1949cf8c6b5b557f/libm-0.2.16/src/math/sin.rs
Line | Count | Source |
1 | | // origin: FreeBSD /usr/src/lib/msun/src/s_sin.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_cos, k_sin, rem_pio2}; |
13 | | |
14 | | // sin(x) |
15 | | // Return sine function of x. |
16 | | // |
17 | | // kernel function: |
18 | | // k_sin ... sine function on [-pi/4,pi/4] |
19 | | // k_cos ... cose function on [-pi/4,pi/4] |
20 | | // rem_pio2 ... argument reduction routine |
21 | | // |
22 | | // Method. |
23 | | // Let S,C and T denote the sin, cos and tan respectively on |
24 | | // [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 |
25 | | // in [-pi/4 , +pi/4], and let n = k mod 4. |
26 | | // We have |
27 | | // |
28 | | // n sin(x) cos(x) tan(x) |
29 | | // ---------------------------------------------------------- |
30 | | // 0 S C T |
31 | | // 1 C -S -1/T |
32 | | // 2 -S -C T |
33 | | // 3 -C S -1/T |
34 | | // ---------------------------------------------------------- |
35 | | // |
36 | | // Special cases: |
37 | | // Let trig be any of sin, cos, or tan. |
38 | | // trig(+-INF) is NaN, with signals; |
39 | | // trig(NaN) is that NaN; |
40 | | // |
41 | | // Accuracy: |
42 | | // TRIG(x) returns trig(x) nearly rounded |
43 | | |
44 | | /// The sine of `x` (f64). |
45 | | /// |
46 | | /// `x` is specified in radians. |
47 | | #[cfg_attr(assert_no_panic, no_panic::no_panic)] |
48 | 0 | pub fn sin(x: f64) -> f64 { |
49 | 0 | let x1p120 = f64::from_bits(0x4770000000000000); // 0x1p120f === 2 ^ 120 |
50 | | |
51 | | /* High word of x. */ |
52 | 0 | let ix = (f64::to_bits(x) >> 32) as u32 & 0x7fffffff; |
53 | | |
54 | | /* |x| ~< pi/4 */ |
55 | 0 | if ix <= 0x3fe921fb { |
56 | 0 | if ix < 0x3e500000 { |
57 | | /* |x| < 2**-26 */ |
58 | | /* raise inexact if x != 0 and underflow if subnormal*/ |
59 | 0 | if ix < 0x00100000 { |
60 | 0 | force_eval!(x / x1p120); |
61 | 0 | } else { |
62 | 0 | force_eval!(x + x1p120); |
63 | 0 | } |
64 | 0 | return x; |
65 | 0 | } |
66 | 0 | return k_sin(x, 0.0, 0); |
67 | 0 | } |
68 | | |
69 | | /* sin(Inf or NaN) is NaN */ |
70 | 0 | if ix >= 0x7ff00000 { |
71 | 0 | return x - x; |
72 | 0 | } |
73 | | |
74 | | /* argument reduction needed */ |
75 | 0 | let (n, y0, y1) = rem_pio2(x); |
76 | 0 | match n & 3 { |
77 | 0 | 0 => k_sin(y0, y1, 1), |
78 | 0 | 1 => k_cos(y0, y1), |
79 | 0 | 2 => -k_sin(y0, y1, 1), |
80 | 0 | _ => -k_cos(y0, y1), |
81 | | } |
82 | 0 | } |
83 | | |
84 | | #[cfg(test)] |
85 | | mod tests { |
86 | | use super::*; |
87 | | |
88 | | #[test] |
89 | | #[cfg_attr(x86_no_sse, ignore = "FIXME(i586): possible incorrect rounding")] |
90 | | fn test_near_pi() { |
91 | | let x = f64::from_bits(0x400921fb000FD5DD); // 3.141592026217707 |
92 | | let sx = f64::from_bits(0x3ea50d15ced1a4a2); // 6.273720864039205e-7 |
93 | | assert_eq!(sin(x), sx); |
94 | | } |
95 | | } |