MCPcopy
hub / github.com/shopspring/decimal / Cos

Method Cos

decimal.go:2235–2279  ·  view source on GitHub ↗

Cos returns the cosine of the radian argument x.

()

Source from the content-addressed store, hash-verified

2233
2234// Cos returns the cosine of the radian argument x.
2235func (d Decimal) Cos() Decimal {
2236
2237 PI4A := NewFromFloat(7.85398125648498535156e-1) // 0x3fe921fb40000000, Pi/4 split into three parts
2238 PI4B := NewFromFloat(3.77489470793079817668e-8) // 0x3e64442d00000000,
2239 PI4C := NewFromFloat(2.69515142907905952645e-15) // 0x3ce8469898cc5170,
2240 M4PI := NewFromFloat(1.273239544735162542821171882678754627704620361328125) // 4/pi
2241
2242 // make argument positive
2243 sign := false
2244 if d.LessThan(NewFromFloat(0.0)) {
2245 d = d.Neg()
2246 }
2247
2248 j := d.Mul(M4PI).IntPart() // integer part of x/(Pi/4), as integer for tests on the phase angle
2249 y := NewFromFloat(float64(j)) // integer part of x/(Pi/4), as float
2250
2251 // map zeros to origin
2252 if j&1 == 1 {
2253 j++
2254 y = y.Add(NewFromFloat(1.0))
2255 }
2256 j &= 7 // octant modulo 2Pi radians (360 degrees)
2257 // reflect in x axis
2258 if j > 3 {
2259 sign = !sign
2260 j -= 4
2261 }
2262 if j > 1 {
2263 sign = !sign
2264 }
2265
2266 z := d.Sub(y.Mul(PI4A)).Sub(y.Mul(PI4B)).Sub(y.Mul(PI4C)) // Extended precision modular arithmetic
2267 zz := z.Mul(z)
2268
2269 if j == 1 || j == 2 {
2270 y = z.Add(z.Mul(zz).Mul(_sin[0].Mul(zz).Add(_sin[1]).Mul(zz).Add(_sin[2]).Mul(zz).Add(_sin[3]).Mul(zz).Add(_sin[4]).Mul(zz).Add(_sin[5])))
2271 } else {
2272 w := zz.Mul(zz).Mul(_cos[0].Mul(zz).Add(_cos[1]).Mul(zz).Add(_cos[2]).Mul(zz).Add(_cos[3]).Mul(zz).Add(_cos[4]).Mul(zz).Add(_cos[5]))
2273 y = NewFromFloat(1.0).Sub(NewFromFloat(0.5).Mul(zz)).Add(w)
2274 }
2275 if sign {
2276 y = y.Neg()
2277 }
2278 return y
2279}
2280
2281var _tanP = [...]Decimal{
2282 NewFromFloat(-1.30936939181383777646e+4), // 0xc0c992d8d24f3f38

Callers 1

TestCosFunction · 0.80

Calls 7

LessThanMethod · 0.95
NegMethod · 0.95
MulMethod · 0.95
SubMethod · 0.95
NewFromFloatFunction · 0.85
IntPartMethod · 0.80
AddMethod · 0.80

Tested by 1

TestCosFunction · 0.64