Math package for float32 / compex64 types https://godoc.org/pkg/math/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cos.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package math32
  2. import "math"
  3. /*
  4. Floating-point sine and cosine.
  5. */
  6. // The original C code, the long comment, and the constants
  7. // below were from http://netlib.sandia.gov/cephes/cmath/sin.c,
  8. // available from http://www.netlib.org/cephes/cmath.tgz.
  9. // The go code is a simplified version of the original C.
  10. //
  11. // For more information see:
  12. // https://github.com/golang/go/blob/master/src/math/sin.go
  13. //
  14. // Cephes Math Library Release 2.8: June, 2000
  15. // Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
  16. //
  17. // The readme file at http://netlib.sandia.gov/cephes/ says:
  18. // Some software in this archive may be from the book _Methods and
  19. // Programs for Mathematical Functions_ (Prentice-Hall or Simon & Schuster
  20. // International, 1989) or from the Cephes Mathematical Library, a
  21. // commercial product. In either event, it is copyrighted by the author.
  22. // What you see here may be used freely but it comes with no support or
  23. // guarantee.
  24. //
  25. // The two known misprints in the book are repaired here in the
  26. // source listings for the gamma function and the incomplete beta
  27. // integral.
  28. //
  29. // Stephen L. Moshier
  30. // moshier@na-net.ornl.gov
  31. // cos coefficients
  32. var _cos = [...]float32{
  33. -1.13585365213876817300E-11, // 0xbda8fa49a0861a9b
  34. 2.08757008419747316778E-9, // 0x3e21ee9d7b4e3f05
  35. -2.75573141792967388112E-7, // 0xbe927e4f7eac4bc6
  36. 2.48015872888517045348E-5, // 0x3efa01a019c844f5
  37. -1.38888888888730564116E-3, // 0xbf56c16c16c14f91
  38. 4.16666666666665929218E-2, // 0x3fa555555555554b
  39. }
  40. // Cos returns the cosine of the radian argument x.
  41. //
  42. // Special cases are:
  43. // Cos(±Inf) = NaN
  44. // Cos(NaN) = NaN
  45. func Cos(x float32) float32
  46. func cos(x float32) float32 {
  47. const (
  48. PI4A = 7.85398125648498535156E-1 // 0x3fe921fb40000000, Pi/4 split into three parts
  49. PI4B = 3.77489470793079817668E-8 // 0x3e64442d00000000,
  50. PI4C = 2.69515142907905952645E-15 // 0x3ce8469898cc5170,
  51. M4PI = 1.273239544735162542821171882678754627704620361328125 // 4/pi
  52. )
  53. // special cases
  54. switch {
  55. case IsNaN(x) || IsInf(x, 0):
  56. return NaN()
  57. }
  58. // make argument positive
  59. sign := false
  60. if x < 0 {
  61. x = -x
  62. }
  63. j := int32(x * M4PI) // integer part of x/(Pi/4), as integer for tests on the phase angle
  64. y := float32(j) // integer part of x/(Pi/4), as float
  65. // map zeros to origin
  66. if j&1 == 1 {
  67. j++
  68. y++
  69. }
  70. j &= 7 // octant modulo 2Pi radians (360 degrees)
  71. if j > 3 {
  72. j -= 4
  73. sign = !sign
  74. }
  75. if j > 1 {
  76. sign = !sign
  77. }
  78. z := ((x - y*PI4A) - y*PI4B) - y*PI4C // Extended precision modular arithmetic
  79. zz := z * z
  80. if j == 1 || j == 2 {
  81. y = z + z*zz*((((((_sin[0]*zz)+_sin[1])*zz+_sin[2])*zz+_sin[3])*zz+_sin[4])*zz+_sin[5])
  82. } else {
  83. 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])
  84. }
  85. if sign {
  86. y = -y
  87. }
  88. return y
  89. }
  90. func Cosh(x float32) float32 {
  91. return float32(math.Cosh(float64(x)))
  92. }
  93. // CosGo is the pure Go Cos function.
  94. var CosGo = cos