atan.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* atan.c
  2. *
  3. * Inverse circular tangent
  4. * (arctangent)
  5. *
  6. *
  7. *
  8. * SYNOPSIS:
  9. *
  10. * double x, y, atan();
  11. *
  12. * y = atan( 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 three intervals into the interval
  22. * from zero to 0.66. The approximant uses a rational
  23. * function of degree 4/5 of the form x + x**3 P(x)/Q(x).
  24. *
  25. *
  26. *
  27. * ACCURACY:
  28. *
  29. * Relative error:
  30. * arithmetic domain # trials peak rms
  31. * DEC -10, 10 50000 2.4e-17 8.3e-18
  32. * IEEE -10, 10 10^6 1.8e-16 5.0e-17
  33. *
  34. */
  35. /* atan2()
  36. *
  37. * Quadrant correct inverse circular tangent
  38. *
  39. *
  40. *
  41. * SYNOPSIS:
  42. *
  43. * double x, y, z, atan2();
  44. *
  45. * z = atan2( y, x );
  46. *
  47. *
  48. *
  49. * DESCRIPTION:
  50. *
  51. * Returns radian angle whose tangent is y/x.
  52. * Define compile time symbol ANSIC = 1 for ANSI standard,
  53. * range -PI < z <= +PI, args (y,x); else ANSIC = 0 for range
  54. * 0 to 2PI, args (x,y).
  55. *
  56. *
  57. *
  58. * ACCURACY:
  59. *
  60. * Relative error:
  61. * arithmetic domain # trials peak rms
  62. * IEEE -10, 10 10^6 2.5e-16 6.9e-17
  63. * See atan.c.
  64. *
  65. */
  66. /* atan.c */
  67. /*
  68. Cephes Math Library Release 2.8: June, 2000
  69. Copyright 1984, 1995, 2000 by Stephen L. Moshier
  70. */
  71. #include <math.h>
  72. /* arctan(x) = x + x^3 P(x^2)/Q(x^2)
  73. 0 <= x <= 0.66
  74. Peak relative error = 2.6e-18 */
  75. #ifdef UNK
  76. static double P[5] = {
  77. -8.750608600031904122785E-1,
  78. -1.615753718733365076637E1,
  79. -7.500855792314704667340E1,
  80. -1.228866684490136173410E2,
  81. -6.485021904942025371773E1,
  82. };
  83. static double Q[5] = {
  84. /* 1.000000000000000000000E0, */
  85. 2.485846490142306297962E1,
  86. 1.650270098316988542046E2,
  87. 4.328810604912902668951E2,
  88. 4.853903996359136964868E2,
  89. 1.945506571482613964425E2,
  90. };
  91. /* tan( 3*pi/8 ) */
  92. static double T3P8 = 2.41421356237309504880;
  93. #endif
  94. #ifdef DEC
  95. static short P[20] = {
  96. 0140140,0001775,0007671,0026242,
  97. 0141201,0041242,0155534,0001715,
  98. 0141626,0002141,0132100,0011625,
  99. 0141765,0142771,0064055,0150453,
  100. 0141601,0131517,0164507,0062164,
  101. };
  102. static short Q[20] = {
  103. /* 0040200,0000000,0000000,0000000, */
  104. 0041306,0157042,0154243,0000742,
  105. 0042045,0003352,0016707,0150452,
  106. 0042330,0070306,0113425,0170730,
  107. 0042362,0130770,0116602,0047520,
  108. 0042102,0106367,0156753,0013541,
  109. };
  110. /* tan( 3*pi/8 ) = 2.41421356237309504880 */
  111. static unsigned short T3P8A[] = {040432,0101171,0114774,0167462,};
  112. #define T3P8 *(double *)T3P8A
  113. #endif
  114. #ifdef IBMPC
  115. static short P[20] = {
  116. 0x2594,0xa1f7,0x007f,0xbfec,
  117. 0x807a,0x5b6b,0x2854,0xc030,
  118. 0x0273,0x3688,0xc08c,0xc052,
  119. 0xba25,0x2d05,0xb8bf,0xc05e,
  120. 0xec8e,0xfd28,0x3669,0xc050,
  121. };
  122. static short Q[20] = {
  123. /* 0x0000,0x0000,0x0000,0x3ff0, */
  124. 0x603c,0x5b14,0xdbc4,0x4038,
  125. 0xfa25,0x43b8,0xa0dd,0x4064,
  126. 0xbe3b,0xd2e2,0x0e18,0x407b,
  127. 0x49ea,0x13b0,0x563f,0x407e,
  128. 0x62ec,0xfbbd,0x519e,0x4068,
  129. };
  130. /* tan( 3*pi/8 ) = 2.41421356237309504880 */
  131. static unsigned short T3P8A[] = {0x9de6,0x333f,0x504f,0x4003};
  132. #define T3P8 *(double *)T3P8A
  133. #endif
  134. #ifdef MIEEE
  135. static short P[20] = {
  136. 0xbfec,0x007f,0xa1f7,0x2594,
  137. 0xc030,0x2854,0x5b6b,0x807a,
  138. 0xc052,0xc08c,0x3688,0x0273,
  139. 0xc05e,0xb8bf,0x2d05,0xba25,
  140. 0xc050,0x3669,0xfd28,0xec8e,
  141. };
  142. static short Q[20] = {
  143. /* 0x3ff0,0x0000,0x0000,0x0000, */
  144. 0x4038,0xdbc4,0x5b14,0x603c,
  145. 0x4064,0xa0dd,0x43b8,0xfa25,
  146. 0x407b,0x0e18,0xd2e2,0xbe3b,
  147. 0x407e,0x563f,0x13b0,0x49ea,
  148. 0x4068,0x519e,0xfbbd,0x62ec,
  149. };
  150. /* tan( 3*pi/8 ) = 2.41421356237309504880 */
  151. static unsigned short T3P8A[] = {
  152. 0x4003,0x504f,0x333f,0x9de6
  153. };
  154. #define T3P8 *(double *)T3P8A
  155. #endif
  156. #ifdef ANSIPROT
  157. extern double polevl ( double, void *, int );
  158. extern double p1evl ( double, void *, int );
  159. extern double atan ( double );
  160. extern double fabs ( double );
  161. extern int signbit ( double );
  162. extern int isnan ( double );
  163. #else
  164. double polevl(), p1evl(), atan(), fabs();
  165. //int signbit(), isnan();
  166. #endif
  167. extern double PI, PIO2, PIO4, INFINITY, NEGZERO, MAXNUM;
  168. /* pi/2 = PIO2 + MOREBITS. */
  169. #ifdef DEC
  170. #define MOREBITS 5.721188726109831840122E-18
  171. #else
  172. #define MOREBITS 6.123233995736765886130E-17
  173. #endif
  174. double atan(x)
  175. double x;
  176. {
  177. double y, z;
  178. short sign, flag;
  179. #ifdef MINUSZERO
  180. if( x == 0.0 )
  181. return(x);
  182. #endif
  183. #ifdef INFINITIES
  184. if(x == INFINITY)
  185. return(PIO2);
  186. if(x == -INFINITY)
  187. return(-PIO2);
  188. #endif
  189. /* make argument positive and save the sign */
  190. sign = 1;
  191. if( x < 0.0 )
  192. {
  193. sign = -1;
  194. x = -x;
  195. }
  196. /* range reduction */
  197. flag = 0;
  198. if( x > T3P8 )
  199. {
  200. y = PIO2;
  201. flag = 1;
  202. x = -( 1.0/x );
  203. }
  204. else if( x <= 0.66 )
  205. {
  206. y = 0.0;
  207. }
  208. else
  209. {
  210. y = PIO4;
  211. flag = 2;
  212. x = (x-1.0)/(x+1.0);
  213. }
  214. z = x * x;
  215. z = z * polevl( z, P, 4 ) / p1evl( z, Q, 5 );
  216. z = x * z + x;
  217. if( flag == 2 )
  218. z += 0.5 * MOREBITS;
  219. else if( flag == 1 )
  220. z += MOREBITS;
  221. y = y + z;
  222. if( sign < 0 )
  223. y = -y;
  224. return(y);
  225. }
  226. /* atan2 */
  227. #ifdef ANSIC
  228. double atan2( y, x )
  229. #else
  230. double atan2( x, y )
  231. #endif
  232. double x, y;
  233. {
  234. double z, w;
  235. short code;
  236. code = 0;
  237. #ifdef NANS
  238. if( isnan(x) )
  239. return(x);
  240. if( isnan(y) )
  241. return(y);
  242. #endif
  243. #ifdef MINUSZERO
  244. if( y == 0.0 )
  245. {
  246. if( signbit(y) )
  247. {
  248. if( x > 0.0 )
  249. z = y;
  250. else if( x < 0.0 )
  251. z = -PI;
  252. else
  253. {
  254. if( signbit(x) )
  255. z = -PI;
  256. else
  257. z = y;
  258. }
  259. }
  260. else /* y is +0 */
  261. {
  262. if( x == 0.0 )
  263. {
  264. if( signbit(x) )
  265. z = PI;
  266. else
  267. z = 0.0;
  268. }
  269. else if( x > 0.0 )
  270. z = 0.0;
  271. else
  272. z = PI;
  273. }
  274. return z;
  275. }
  276. if( x == 0.0 )
  277. {
  278. if( y > 0.0 )
  279. z = PIO2;
  280. else
  281. z = -PIO2;
  282. return z;
  283. }
  284. #endif /* MINUSZERO */
  285. #ifdef INFINITIES
  286. if( x == INFINITY )
  287. {
  288. if( y == INFINITY )
  289. z = 0.25 * PI;
  290. else if( y == -INFINITY )
  291. z = -0.25 * PI;
  292. else if( y < 0.0 )
  293. z = NEGZERO;
  294. else
  295. z = 0.0;
  296. return z;
  297. }
  298. if( x == -INFINITY )
  299. {
  300. if( y == INFINITY )
  301. z = 0.75 * PI;
  302. else if( y <= -INFINITY )
  303. z = -0.75 * PI;
  304. else if( y >= 0.0 )
  305. z = PI;
  306. else
  307. z = -PI;
  308. return z;
  309. }
  310. if( y == INFINITY )
  311. return( PIO2 );
  312. if( y == -INFINITY )
  313. return( -PIO2 );
  314. #endif
  315. if( x < 0.0 )
  316. code = 2;
  317. if( y < 0.0 )
  318. code |= 1;
  319. #ifdef INFINITIES
  320. if( x == 0.0 )
  321. #else
  322. if( fabs(x) <= (fabs(y) / MAXNUM) )
  323. #endif
  324. {
  325. if( code & 1 )
  326. {
  327. #if ANSIC
  328. return( -PIO2 );
  329. #else
  330. return( 3.0*PIO2 );
  331. #endif
  332. }
  333. if( y == 0.0 )
  334. return( 0.0 );
  335. return( PIO2 );
  336. }
  337. if( y == 0.0 )
  338. {
  339. if( code & 2 )
  340. return( PI );
  341. return( 0.0 );
  342. }
  343. switch( code )
  344. {
  345. #if ANSIC
  346. default:
  347. case 0:
  348. case 1: w = 0.0; break;
  349. case 2: w = PI; break;
  350. case 3: w = -PI; break;
  351. #else
  352. default:
  353. case 0: w = 0.0; break;
  354. case 1: w = 2.0 * PI; break;
  355. case 2:
  356. case 3: w = PI; break;
  357. #endif
  358. }
  359. z = w + atan( y/x );
  360. #ifdef MINUSZERO
  361. if( z == 0.0 && y < 0 )
  362. z = NEGZERO;
  363. #endif
  364. return( z );
  365. }