atan.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 "mconf.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, -1.615753718733365076637E1,
  78. -7.500855792314704667340E1, -1.228866684490136173410E2,
  79. -6.485021904942025371773E1,
  80. };
  81. static double Q[5] = {
  82. /* 1.000000000000000000000E0, */
  83. 2.485846490142306297962E1, 1.650270098316988542046E2,
  84. 4.328810604912902668951E2, 4.853903996359136964868E2,
  85. 1.945506571482613964425E2,
  86. };
  87. /* tan( 3*pi/8 ) */
  88. static double T3P8 = 2.41421356237309504880;
  89. #endif
  90. #ifdef DEC
  91. static short P[20] = {
  92. 0140140, 0001775, 0007671, 0026242, 0141201, 0041242, 0155534,
  93. 0001715, 0141626, 0002141, 0132100, 0011625, 0141765, 0142771,
  94. 0064055, 0150453, 0141601, 0131517, 0164507, 0062164,
  95. };
  96. static short Q[20] = {
  97. /* 0040200,0000000,0000000,0000000, */
  98. 0041306, 0157042, 0154243, 0000742, 0042045, 0003352, 0016707,
  99. 0150452, 0042330, 0070306, 0113425, 0170730, 0042362, 0130770,
  100. 0116602, 0047520, 0042102, 0106367, 0156753, 0013541,
  101. };
  102. /* tan( 3*pi/8 ) = 2.41421356237309504880 */
  103. static unsigned short T3P8A[] = {
  104. 040432,
  105. 0101171,
  106. 0114774,
  107. 0167462,
  108. };
  109. #define T3P8 *(double *)T3P8A
  110. #endif
  111. #ifdef IBMPC
  112. static short P[20] = {
  113. 0x2594, 0xa1f7, 0x007f, 0xbfec, 0x807a, 0x5b6b, 0x2854,
  114. 0xc030, 0x0273, 0x3688, 0xc08c, 0xc052, 0xba25, 0x2d05,
  115. 0xb8bf, 0xc05e, 0xec8e, 0xfd28, 0x3669, 0xc050,
  116. };
  117. static short Q[20] = {
  118. /* 0x0000,0x0000,0x0000,0x3ff0, */
  119. 0x603c, 0x5b14, 0xdbc4, 0x4038, 0xfa25, 0x43b8, 0xa0dd,
  120. 0x4064, 0xbe3b, 0xd2e2, 0x0e18, 0x407b, 0x49ea, 0x13b0,
  121. 0x563f, 0x407e, 0x62ec, 0xfbbd, 0x519e, 0x4068,
  122. };
  123. /* tan( 3*pi/8 ) = 2.41421356237309504880 */
  124. static unsigned short T3P8A[] = {0x9de6, 0x333f, 0x504f, 0x4003};
  125. #define T3P8 *(double *)T3P8A
  126. #endif
  127. #ifdef MIEEE
  128. static short P[20] = {
  129. 0xbfec, 0x007f, 0xa1f7, 0x2594, 0xc030, 0x2854, 0x5b6b,
  130. 0x807a, 0xc052, 0xc08c, 0x3688, 0x0273, 0xc05e, 0xb8bf,
  131. 0x2d05, 0xba25, 0xc050, 0x3669, 0xfd28, 0xec8e,
  132. };
  133. static short Q[20] = {
  134. /* 0x3ff0,0x0000,0x0000,0x0000, */
  135. 0x4038, 0xdbc4, 0x5b14, 0x603c, 0x4064, 0xa0dd, 0x43b8,
  136. 0xfa25, 0x407b, 0x0e18, 0xd2e2, 0xbe3b, 0x407e, 0x563f,
  137. 0x13b0, 0x49ea, 0x4068, 0x519e, 0xfbbd, 0x62ec,
  138. };
  139. /* tan( 3*pi/8 ) = 2.41421356237309504880 */
  140. static unsigned short T3P8A[] = {0x4003, 0x504f, 0x333f, 0x9de6};
  141. #define T3P8 *(double *)T3P8A
  142. #endif
  143. #ifdef ANSIPROT
  144. extern double polevl(double, void *, int);
  145. extern double p1evl(double, void *, int);
  146. extern double atan(double);
  147. extern double fabs(double);
  148. extern int signbit(double);
  149. extern int isnan(double);
  150. #else
  151. double polevl(), p1evl(), atan(), fabs();
  152. int signbit(), isnan();
  153. #endif
  154. extern double PI, PIO2, PIO4, INFINITY, NEGZERO, MAXNUM;
  155. /* pi/2 = PIO2 + MOREBITS. */
  156. #ifdef DEC
  157. #define MOREBITS 5.721188726109831840122E-18
  158. #else
  159. #define MOREBITS 6.123233995736765886130E-17
  160. #endif
  161. double atan(x) double x;
  162. {
  163. double y, z;
  164. short sign, flag;
  165. #ifdef MINUSZERO
  166. if (x == 0.0)
  167. return (x);
  168. #endif
  169. #ifdef INFINITIES
  170. if (x == INFINITY)
  171. return (PIO2);
  172. if (x == -INFINITY)
  173. return (-PIO2);
  174. #endif
  175. /* make argument positive and save the sign */
  176. sign = 1;
  177. if (x < 0.0) {
  178. sign = -1;
  179. x = -x;
  180. }
  181. /* range reduction */
  182. flag = 0;
  183. if (x > T3P8) {
  184. y = PIO2;
  185. flag = 1;
  186. x = -(1.0 / x);
  187. } else if (x <= 0.66) {
  188. y = 0.0;
  189. } else {
  190. y = PIO4;
  191. flag = 2;
  192. x = (x - 1.0) / (x + 1.0);
  193. }
  194. z = x * x;
  195. z = z * polevl(z, P, 4) / p1evl(z, Q, 5);
  196. z = x * z + x;
  197. if (flag == 2)
  198. z += 0.5 * MOREBITS;
  199. else if (flag == 1)
  200. z += MOREBITS;
  201. y = y + z;
  202. if (sign < 0)
  203. y = -y;
  204. return (y);
  205. }
  206. /* atan2 */
  207. #ifdef ANSIC
  208. double atan2(y, x)
  209. #else
  210. double atan2(x, y)
  211. #endif
  212. double x,
  213. y;
  214. {
  215. double z, w;
  216. short code;
  217. code = 0;
  218. #ifdef NANS
  219. if (isnan(x))
  220. return (x);
  221. if (isnan(y))
  222. return (y);
  223. #endif
  224. #ifdef MINUSZERO
  225. if (y == 0.0) {
  226. if (signbit(y)) {
  227. if (x > 0.0)
  228. z = y;
  229. else if (x < 0.0)
  230. z = -PI;
  231. else {
  232. if (signbit(x))
  233. z = -PI;
  234. else
  235. z = y;
  236. }
  237. } else /* y is +0 */
  238. {
  239. if (x == 0.0) {
  240. if (signbit(x))
  241. z = PI;
  242. else
  243. z = 0.0;
  244. } else if (x > 0.0)
  245. z = 0.0;
  246. else
  247. z = PI;
  248. }
  249. return z;
  250. }
  251. if (x == 0.0) {
  252. if (y > 0.0)
  253. z = PIO2;
  254. else
  255. z = -PIO2;
  256. return z;
  257. }
  258. #endif /* MINUSZERO */
  259. #ifdef INFINITIES
  260. if (x == INFINITY) {
  261. if (y == INFINITY)
  262. z = 0.25 * PI;
  263. else if (y == -INFINITY)
  264. z = -0.25 * PI;
  265. else if (y < 0.0)
  266. z = NEGZERO;
  267. else
  268. z = 0.0;
  269. return z;
  270. }
  271. if (x == -INFINITY) {
  272. if (y == INFINITY)
  273. z = 0.75 * PI;
  274. else if (y <= -INFINITY)
  275. z = -0.75 * PI;
  276. else if (y >= 0.0)
  277. z = PI;
  278. else
  279. z = -PI;
  280. return z;
  281. }
  282. if (y == INFINITY)
  283. return (PIO2);
  284. if (y == -INFINITY)
  285. return (-PIO2);
  286. #endif
  287. if (x < 0.0)
  288. code = 2;
  289. if (y < 0.0)
  290. code |= 1;
  291. #ifdef INFINITIES
  292. if (x == 0.0)
  293. #else
  294. if (fabs(x) <= (fabs(y) / MAXNUM))
  295. #endif
  296. {
  297. if (code & 1) {
  298. #if ANSIC
  299. return (-PIO2);
  300. #else
  301. return (3.0 * PIO2);
  302. #endif
  303. }
  304. if (y == 0.0)
  305. return (0.0);
  306. return (PIO2);
  307. }
  308. if (y == 0.0) {
  309. if (code & 2)
  310. return (PI);
  311. return (0.0);
  312. }
  313. switch (code) {
  314. #if ANSIC
  315. default:
  316. case 0:
  317. case 1:
  318. w = 0.0;
  319. break;
  320. case 2:
  321. w = PI;
  322. break;
  323. case 3:
  324. w = -PI;
  325. break;
  326. #else
  327. default:
  328. case 0:
  329. w = 0.0;
  330. break;
  331. case 1:
  332. w = 2.0 * PI;
  333. break;
  334. case 2:
  335. case 3:
  336. w = PI;
  337. break;
  338. #endif
  339. }
  340. z = w + atan(y / x);
  341. #ifdef MINUSZERO
  342. if (z == 0.0 && y < 0)
  343. z = NEGZERO;
  344. #endif
  345. return (z);
  346. }