tandgf.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* tandgf.c
  2. *
  3. * Circular tangent of angle in degrees
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float x, y, tandgf();
  10. *
  11. * y = tandgf( x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns the circular tangent of the radian argument x.
  18. *
  19. * Range reduction is into intervals of 45 degrees.
  20. *
  21. *
  22. *
  23. *
  24. * ACCURACY:
  25. *
  26. * Relative error:
  27. * arithmetic domain # trials peak rms
  28. * IEEE +-2^24 50000 2.4e-7 4.8e-8
  29. *
  30. * ERROR MESSAGES:
  31. *
  32. * message condition value returned
  33. * tanf total loss x > 2^24 0.0
  34. *
  35. */
  36. /* cotdgf.c
  37. *
  38. * Circular cotangent of angle in degrees
  39. *
  40. *
  41. *
  42. * SYNOPSIS:
  43. *
  44. * float x, y, cotdgf();
  45. *
  46. * y = cotdgf( x );
  47. *
  48. *
  49. *
  50. * DESCRIPTION:
  51. *
  52. * Range reduction is into intervals of 45 degrees.
  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 +-2^24 50000 2.4e-7 4.8e-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, 1992 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 T24M1 = 16777215.;
  85. static float PI180 = 0.0174532925199432957692; /* pi/180 */
  86. static float tancotf( float xx, int cotflg )
  87. {
  88. float x, y, z, zz;
  89. long j;
  90. int sign;
  91. /* make argument positive but save the sign */
  92. if( xx < 0.0 )
  93. {
  94. x = -xx;
  95. sign = -1;
  96. }
  97. else
  98. {
  99. x = xx;
  100. sign = 1;
  101. }
  102. if( x > T24M1 )
  103. {
  104. if( cotflg )
  105. mtherr( "cotdgf", TLOSS );
  106. else
  107. mtherr( "tandgf", TLOSS );
  108. return(0.0);
  109. }
  110. /* compute x mod PIO4 */
  111. j = 0.022222222222222222222 * x; /* integer part of x/45 */
  112. y = j;
  113. /* map zeros and singularities to origin */
  114. if( j & 1 )
  115. {
  116. j += 1;
  117. y += 1.0;
  118. }
  119. z = x - y * 45.0;
  120. z *= PI180; /* multiply by pi/180 to convert to radians */
  121. zz = z * z;
  122. if( x > 1.0e-4 )
  123. {
  124. /* 1.7e-8 relative error in [-pi/4, +pi/4] */
  125. y =
  126. ((((( 9.38540185543E-3 * zz
  127. + 3.11992232697E-3) * zz
  128. + 2.44301354525E-2) * zz
  129. + 5.34112807005E-2) * zz
  130. + 1.33387994085E-1) * zz
  131. + 3.33331568548E-1) * zz * z
  132. + z;
  133. }
  134. else
  135. {
  136. y = z;
  137. }
  138. if( j & 2 )
  139. {
  140. if( cotflg )
  141. y = -y;
  142. else
  143. {
  144. if( y != 0.0 )
  145. {
  146. y = -1.0/y;
  147. }
  148. else
  149. {
  150. mtherr( "tandgf", SING );
  151. y = MAXNUMF;
  152. }
  153. }
  154. }
  155. else
  156. {
  157. if( cotflg )
  158. {
  159. if( y != 0.0 )
  160. y = 1.0/y;
  161. else
  162. {
  163. mtherr( "cotdgf", SING );
  164. y = MAXNUMF;
  165. }
  166. }
  167. }
  168. if( sign < 0 )
  169. y = -y;
  170. return( y );
  171. }
  172. float tandgf( float x )
  173. {
  174. return( tancotf(x,0) );
  175. }
  176. float cotdgf( float x )
  177. {
  178. if( x == 0.0 )
  179. {
  180. mtherr( "cotdgf", SING );
  181. return( MAXNUMF );
  182. }
  183. return( tancotf(x,1) );
  184. }