tanf.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* tanf.c
  2. *
  3. * Circular tangent
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float x, y, tanf();
  10. *
  11. * y = tanf( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns the circular tangent of the radian argument x.
  18. *
  19. * Range reduction is modulo pi/4. A polynomial approximation
  20. * is employed in the basic interval [0, pi/4].
  21. *
  22. *
  23. *
  24. * ACCURACY:
  25. *
  26. * Relative error:
  27. * arithmetic domain # trials peak rms
  28. * IEEE +-4096 100000 3.3e-7 4.5e-8
  29. *
  30. * ERROR MESSAGES:
  31. *
  32. * message condition value returned
  33. * tanf total loss x > 2^24 0.0
  34. *
  35. */
  36. /* cotf.c
  37. *
  38. * Circular cotangent
  39. *
  40. *
  41. *
  42. * SYNOPSIS:
  43. *
  44. * float x, y, cotf();
  45. *
  46. * y = cotf( x );
  47. *
  48. *
  49. *
  50. * DESCRIPTION:
  51. *
  52. * Returns the circular cotangent of the radian argument x.
  53. * A common routine computes either the tangent or cotangent.
  54. *
  55. *
  56. *
  57. * ACCURACY:
  58. *
  59. * Relative error:
  60. * arithmetic domain # trials peak rms
  61. * IEEE +-4096 100000 3.0e-7 4.5e-8
  62. *
  63. *
  64. * ERROR MESSAGES:
  65. *
  66. * message condition value returned
  67. * cot total loss x > 2^24 0.0
  68. * cot singularity x = 0 MAXNUMF
  69. *
  70. */
  71. /*
  72. Cephes Math Library Release 2.2: June, 1992
  73. Copyright 1984, 1987, 1989 by Stephen L. Moshier
  74. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  75. */
  76. /* Single precision circular tangent
  77. * test interval: [-pi/4, +pi/4]
  78. * trials: 10000
  79. * peak relative error: 8.7e-8
  80. * rms relative error: 2.8e-8
  81. */
  82. #include <math.h>
  83. extern float MAXNUMF;
  84. static float DP1 = 0.78515625;
  85. static float DP2 = 2.4187564849853515625e-4;
  86. static float DP3 = 3.77489497744594108e-8;
  87. float FOPI = 1.27323954473516; /* 4/pi */
  88. static float lossth = 8192.;
  89. /*static float T24M1 = 16777215.;*/
  90. static float tancotf( float xx, int cotflg )
  91. {
  92. float x, y, z, zz;
  93. long j;
  94. int sign;
  95. /* make argument positive but save the sign */
  96. if( xx < 0.0 )
  97. {
  98. x = -xx;
  99. sign = -1;
  100. }
  101. else
  102. {
  103. x = xx;
  104. sign = 1;
  105. }
  106. if( x > lossth )
  107. {
  108. if( cotflg )
  109. mtherr( "cotf", TLOSS );
  110. else
  111. mtherr( "tanf", TLOSS );
  112. return(0.0);
  113. }
  114. /* compute x mod PIO4 */
  115. j = FOPI * x; /* integer part of x/(PI/4) */
  116. y = j;
  117. /* map zeros and singularities to origin */
  118. if( j & 1 )
  119. {
  120. j += 1;
  121. y += 1.0;
  122. }
  123. z = ((x - y * DP1) - y * DP2) - y * DP3;
  124. zz = z * z;
  125. if( x > 1.0e-4 )
  126. {
  127. /* 1.7e-8 relative error in [-pi/4, +pi/4] */
  128. y =
  129. ((((( 9.38540185543E-3 * zz
  130. + 3.11992232697E-3) * zz
  131. + 2.44301354525E-2) * zz
  132. + 5.34112807005E-2) * zz
  133. + 1.33387994085E-1) * zz
  134. + 3.33331568548E-1) * zz * z
  135. + z;
  136. }
  137. else
  138. {
  139. y = z;
  140. }
  141. if( j & 2 )
  142. {
  143. if( cotflg )
  144. y = -y;
  145. else
  146. y = -1.0/y;
  147. }
  148. else
  149. {
  150. if( cotflg )
  151. y = 1.0/y;
  152. }
  153. if( sign < 0 )
  154. y = -y;
  155. return( y );
  156. }
  157. float tanf( float x )
  158. {
  159. return( tancotf(x,0) );
  160. }
  161. float cotf( float x )
  162. {
  163. if( x == 0.0 )
  164. {
  165. mtherr( "cotf", SING );
  166. return( MAXNUMF );
  167. }
  168. return( tancotf(x,1) );
  169. }