tan.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package math
  5. /*
  6. Floating-point tangent.
  7. */
  8. // The original C code, the long comment, and the constants
  9. // below were from http://netlib.sandia.gov/cephes/cmath/sin.c,
  10. // available from http://www.netlib.org/cephes/cmath.tgz.
  11. // The go code is a simplified version of the original C.
  12. //
  13. // tan.c
  14. //
  15. // Circular tangent
  16. //
  17. // SYNOPSIS:
  18. //
  19. // double x, y, tan();
  20. // y = tan( x );
  21. //
  22. // DESCRIPTION:
  23. //
  24. // Returns the circular tangent of the radian argument x.
  25. //
  26. // Range reduction is modulo pi/4. A rational function
  27. // x + x**3 P(x**2)/Q(x**2)
  28. // is employed in the basic interval [0, pi/4].
  29. //
  30. // ACCURACY:
  31. // Relative error:
  32. // arithmetic domain # trials peak rms
  33. // DEC +-1.07e9 44000 4.1e-17 1.0e-17
  34. // IEEE +-1.07e9 30000 2.9e-16 8.1e-17
  35. //
  36. // Partial loss of accuracy begins to occur at x = 2**30 = 1.074e9. The loss
  37. // is not gradual, but jumps suddenly to about 1 part in 10e7. Results may
  38. // be meaningless for x > 2**49 = 5.6e14.
  39. // [Accuracy loss statement from sin.go comments.]
  40. //
  41. // Cephes Math Library Release 2.8: June, 2000
  42. // Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
  43. //
  44. // The readme file at http://netlib.sandia.gov/cephes/ says:
  45. // Some software in this archive may be from the book _Methods and
  46. // Programs for Mathematical Functions_ (Prentice-Hall or Simon & Schuster
  47. // International, 1989) or from the Cephes Mathematical Library, a
  48. // commercial product. In either event, it is copyrighted by the author.
  49. // What you see here may be used freely but it comes with no support or
  50. // guarantee.
  51. //
  52. // The two known misprints in the book are repaired here in the
  53. // source listings for the gamma function and the incomplete beta
  54. // integral.
  55. //
  56. // Stephen L. Moshier
  57. // moshier@na-net.ornl.gov
  58. // tan coefficients
  59. var _tanP = [...]float64{
  60. -1.30936939181383777646e4, // 0xc0c992d8d24f3f38
  61. 1.15351664838587416140e6, // 0x413199eca5fc9ddd
  62. -1.79565251976484877988e7, // 0xc1711fead3299176
  63. }
  64. var _tanQ = [...]float64{
  65. 1.00000000000000000000e0,
  66. 1.36812963470692954678e4, //0x40cab8a5eeb36572
  67. -1.32089234440210967447e6, //0xc13427bc582abc96
  68. 2.50083801823357915839e7, //0x4177d98fc2ead8ef
  69. -5.38695755929454629881e7, //0xc189afe03cbe5a31
  70. }
  71. // Tan returns the tangent of the radian argument x.
  72. //
  73. // Special cases are:
  74. // Tan(±0) = ±0
  75. // Tan(±Inf) = NaN
  76. // Tan(NaN) = NaN
  77. func Tan(x float64) float64 {
  78. return libc_tan(x)
  79. }
  80. //extern tan
  81. func libc_tan(float64) float64
  82. func tan(x float64) float64 {
  83. const (
  84. PI4A = 7.85398125648498535156e-1 // 0x3fe921fb40000000, Pi/4 split into three parts
  85. PI4B = 3.77489470793079817668e-8 // 0x3e64442d00000000,
  86. PI4C = 2.69515142907905952645e-15 // 0x3ce8469898cc5170,
  87. )
  88. // special cases
  89. switch {
  90. case x == 0 || IsNaN(x):
  91. return x // return ±0 || NaN()
  92. case IsInf(x, 0):
  93. return NaN()
  94. }
  95. // make argument positive but save the sign
  96. sign := false
  97. if x < 0 {
  98. x = -x
  99. sign = true
  100. }
  101. var j uint64
  102. var y, z float64
  103. if x >= reduceThreshold {
  104. j, z = trigReduce(x)
  105. } else {
  106. j = uint64(x * (4 / Pi)) // integer part of x/(Pi/4), as integer for tests on the phase angle
  107. y = float64(j) // integer part of x/(Pi/4), as float
  108. /* map zeros and singularities to origin */
  109. if j&1 == 1 {
  110. j++
  111. y++
  112. }
  113. z = ((x - y*PI4A) - y*PI4B) - y*PI4C
  114. }
  115. zz := z * z
  116. if zz > 1e-14 {
  117. y = z + z*(zz*(((_tanP[0]*zz)+_tanP[1])*zz+_tanP[2])/((((zz+_tanQ[1])*zz+_tanQ[2])*zz+_tanQ[3])*zz+_tanQ[4]))
  118. } else {
  119. y = z
  120. }
  121. if j&2 == 2 {
  122. y = -1 / y
  123. }
  124. if sign {
  125. y = -y
  126. }
  127. return y
  128. }