OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package math | 5 package math |
6 | 6 |
7 /* | 7 /* |
8 Floating-point sine and cosine. | 8 Floating-point sine and cosine. |
9 */ | 9 */ |
10 | 10 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 // Cos(NaN) = NaN | 116 // Cos(NaN) = NaN |
117 func Cos(x float64) float64 | 117 func Cos(x float64) float64 |
118 | 118 |
119 func cos(x float64) float64 { | 119 func cos(x float64) float64 { |
120 const ( | 120 const ( |
121 PI4A = 7.85398125648498535156E-1 //
0x3fe921fb40000000, Pi/4 split into three parts | 121 PI4A = 7.85398125648498535156E-1 //
0x3fe921fb40000000, Pi/4 split into three parts |
122 PI4B = 3.77489470793079817668E-8 //
0x3e64442d00000000, | 122 PI4B = 3.77489470793079817668E-8 //
0x3e64442d00000000, |
123 PI4C = 2.69515142907905952645E-15 //
0x3ce8469898cc5170, | 123 PI4C = 2.69515142907905952645E-15 //
0x3ce8469898cc5170, |
124 M4PI = 1.273239544735162542821171882678754627704620361328125 //
4/pi | 124 M4PI = 1.273239544735162542821171882678754627704620361328125 //
4/pi |
125 ) | 125 ) |
126 // TODO(rsc): Remove manual inlining of IsNaN, IsInf | |
127 // when compiler does it for us | |
128 // special cases | 126 // special cases |
129 switch { | 127 switch { |
130 » case x != x || x < -MaxFloat64 || x > MaxFloat64: // IsNaN(x) || IsInf(x
, 0): | 128 » case IsNaN(x) || IsInf(x, 0): |
131 return NaN() | 129 return NaN() |
132 } | 130 } |
133 | 131 |
134 // make argument positive | 132 // make argument positive |
135 sign := false | 133 sign := false |
136 if x < 0 { | 134 if x < 0 { |
137 x = -x | 135 x = -x |
138 } | 136 } |
139 | 137 |
140 j := int64(x * M4PI) // integer part of x/(Pi/4), as integer for tests o
n the phase angle | 138 j := int64(x * M4PI) // integer part of x/(Pi/4), as integer for tests o
n the phase angle |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 // Sin(NaN) = NaN | 173 // Sin(NaN) = NaN |
176 func Sin(x float64) float64 | 174 func Sin(x float64) float64 |
177 | 175 |
178 func sin(x float64) float64 { | 176 func sin(x float64) float64 { |
179 const ( | 177 const ( |
180 PI4A = 7.85398125648498535156E-1 //
0x3fe921fb40000000, Pi/4 split into three parts | 178 PI4A = 7.85398125648498535156E-1 //
0x3fe921fb40000000, Pi/4 split into three parts |
181 PI4B = 3.77489470793079817668E-8 //
0x3e64442d00000000, | 179 PI4B = 3.77489470793079817668E-8 //
0x3e64442d00000000, |
182 PI4C = 2.69515142907905952645E-15 //
0x3ce8469898cc5170, | 180 PI4C = 2.69515142907905952645E-15 //
0x3ce8469898cc5170, |
183 M4PI = 1.273239544735162542821171882678754627704620361328125 //
4/pi | 181 M4PI = 1.273239544735162542821171882678754627704620361328125 //
4/pi |
184 ) | 182 ) |
185 // TODO(rsc): Remove manual inlining of IsNaN, IsInf | |
186 // when compiler does it for us | |
187 // special cases | 183 // special cases |
188 switch { | 184 switch { |
189 » case x == 0 || x != x: // x == 0 || IsNaN(): | 185 » case x == 0 || IsNaN(x): |
190 return x // return ±0 || NaN() | 186 return x // return ±0 || NaN() |
191 » case x < -MaxFloat64 || x > MaxFloat64: // IsInf(x, 0): | 187 » case IsInf(x, 0): |
192 return NaN() | 188 return NaN() |
193 } | 189 } |
194 | 190 |
195 // make argument positive but save the sign | 191 // make argument positive but save the sign |
196 sign := false | 192 sign := false |
197 if x < 0 { | 193 if x < 0 { |
198 x = -x | 194 x = -x |
199 sign = true | 195 sign = true |
200 } | 196 } |
201 | 197 |
(...skipping 17 matching lines...) Expand all Loading... |
219 if j == 1 || j == 2 { | 215 if j == 1 || j == 2 { |
220 y = 1.0 - 0.5*zz + zz*zz*((((((_cos[0]*zz)+_cos[1])*zz+_cos[2])*
zz+_cos[3])*zz+_cos[4])*zz+_cos[5]) | 216 y = 1.0 - 0.5*zz + zz*zz*((((((_cos[0]*zz)+_cos[1])*zz+_cos[2])*
zz+_cos[3])*zz+_cos[4])*zz+_cos[5]) |
221 } else { | 217 } else { |
222 y = z + z*zz*((((((_sin[0]*zz)+_sin[1])*zz+_sin[2])*zz+_sin[3])*
zz+_sin[4])*zz+_sin[5]) | 218 y = z + z*zz*((((((_sin[0]*zz)+_sin[1])*zz+_sin[2])*zz+_sin[3])*
zz+_sin[4])*zz+_sin[5]) |
223 } | 219 } |
224 if sign { | 220 if sign { |
225 y = -y | 221 y = -y |
226 } | 222 } |
227 return y | 223 return y |
228 } | 224 } |
OLD | NEW |