atanf.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* atanf.c
  2. *
  3. * Inverse circular tangent
  4. * (arctangent)
  5. *
  6. *
  7. *
  8. * SYNOPSIS:
  9. *
  10. * float x, y, atanf();
  11. *
  12. * y = atanf( x );
  13. *
  14. *
  15. *
  16. * DESCRIPTION:
  17. *
  18. * Returns radian angle between -pi/2 and +pi/2 whose tangent
  19. * is x.
  20. *
  21. * Range reduction is from four intervals into the interval
  22. * from zero to tan( pi/8 ). A polynomial approximates
  23. * the function in this basic interval.
  24. *
  25. *
  26. *
  27. * ACCURACY:
  28. *
  29. * Relative error:
  30. * arithmetic domain # trials peak rms
  31. * IEEE -10, 10 100000 1.9e-7 4.1e-8
  32. *
  33. */
  34. /* atan2f()
  35. *
  36. * Quadrant correct inverse circular tangent
  37. *
  38. *
  39. *
  40. * SYNOPSIS:
  41. *
  42. * float x, y, z, atan2f();
  43. *
  44. * z = atan2f( y, x );
  45. *
  46. *
  47. *
  48. * DESCRIPTION:
  49. *
  50. * Returns radian angle whose tangent is y/x.
  51. * Define compile time symbol ANSIC = 1 for ANSI standard,
  52. * range -PI < z <= +PI, args (y,x); else ANSIC = 0 for range
  53. * 0 to 2PI, args (x,y).
  54. *
  55. *
  56. *
  57. * ACCURACY:
  58. *
  59. * Relative error:
  60. * arithmetic domain # trials peak rms
  61. * IEEE -10, 10 100000 1.9e-7 4.1e-8
  62. * See atan.c.
  63. *
  64. */
  65. /* atan.c */
  66. /*
  67. Cephes Math Library Release 2.2: June, 1992
  68. Copyright 1984, 1987, 1989, 1992 by Stephen L. Moshier
  69. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  70. */
  71. /* Single precision circular arcsine
  72. * test interval: [-tan(pi/8), +tan(pi/8)]
  73. * trials: 10000
  74. * peak relative error: 7.7e-8
  75. * rms relative error: 2.9e-8
  76. */
  77. #include <math.h>
  78. extern float PIF, PIO2F, PIO4F;
  79. float atanf( float xx )
  80. {
  81. float x, y, z;
  82. int sign;
  83. x = xx;
  84. /* make argument positive and save the sign */
  85. if( xx < 0.0 )
  86. {
  87. sign = -1;
  88. x = -xx;
  89. }
  90. else
  91. {
  92. sign = 1;
  93. x = xx;
  94. }
  95. /* range reduction */
  96. if( x > 2.414213562373095 ) /* tan 3pi/8 */
  97. {
  98. y = PIO2F;
  99. x = -( 1.0/x );
  100. }
  101. else if( x > 0.4142135623730950 ) /* tan pi/8 */
  102. {
  103. y = PIO4F;
  104. x = (x-1.0)/(x+1.0);
  105. }
  106. else
  107. y = 0.0;
  108. z = x * x;
  109. y +=
  110. ((( 8.05374449538e-2 * z
  111. - 1.38776856032E-1) * z
  112. + 1.99777106478E-1) * z
  113. - 3.33329491539E-1) * z * x
  114. + x;
  115. if( sign < 0 )
  116. y = -y;
  117. return( y );
  118. }
  119. float atan2f( float y, float x )
  120. {
  121. float z, w;
  122. int code;
  123. code = 0;
  124. if( x < 0.0 )
  125. code = 2;
  126. if( y < 0.0 )
  127. code |= 1;
  128. if( x == 0.0 )
  129. {
  130. if( code & 1 )
  131. {
  132. #if ANSIC
  133. return( -PIO2F );
  134. #else
  135. return( 3.0*PIO2F );
  136. #endif
  137. }
  138. if( y == 0.0 )
  139. return( 0.0 );
  140. return( PIO2F );
  141. }
  142. if( y == 0.0 )
  143. {
  144. if( code & 2 )
  145. return( PIF );
  146. return( 0.0 );
  147. }
  148. switch( code )
  149. {
  150. default:
  151. #if ANSIC
  152. case 0:
  153. case 1: w = 0.0; break;
  154. case 2: w = PIF; break;
  155. case 3: w = -PIF; break;
  156. #else
  157. case 0: w = 0.0; break;
  158. case 1: w = 2.0 * PIF; break;
  159. case 2:
  160. case 3: w = PIF; break;
  161. #endif
  162. }
  163. z = atanf( y/x );
  164. return( w + z );
  165. }