atan.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. *
  5. * Author: Ceriel J.H. Jacobs
  6. */
  7. /* $Id$ */
  8. #include <float.h>
  9. #include <math.h>
  10. #include <errno.h>
  11. #include "localmath.h"
  12. double
  13. atan(double x)
  14. {
  15. /* Algorithm and coefficients from:
  16. "Software manual for the elementary functions"
  17. by W.J. Cody and W. Waite, Prentice-Hall, 1980
  18. */
  19. static double p[] = {
  20. -0.13688768894191926929e+2,
  21. -0.20505855195861651981e+2,
  22. -0.84946240351320683534e+1,
  23. -0.83758299368150059274e+0
  24. };
  25. static double q[] = {
  26. 0.41066306682575781263e+2,
  27. 0.86157349597130242515e+2,
  28. 0.59578436142597344465e+2,
  29. 0.15024001160028576121e+2,
  30. 1.0
  31. };
  32. static double a[] = {
  33. 0.0,
  34. 0.52359877559829887307710723554658381, /* pi/6 */
  35. M_PI_2,
  36. 1.04719755119659774615421446109316763 /* pi/3 */
  37. };
  38. int neg = x < 0;
  39. int n;
  40. double g;
  41. if (__IsNan(x)) {
  42. errno = EDOM;
  43. return x;
  44. }
  45. if (neg) {
  46. x = -x;
  47. }
  48. if (x > 1.0) {
  49. x = 1.0/x;
  50. n = 2;
  51. }
  52. else n = 0;
  53. if (x > 0.26794919243112270647) { /* 2-sqtr(3) */
  54. n = n + 1;
  55. x = (((0.73205080756887729353*x-0.5)-0.5)+x)/
  56. (1.73205080756887729353+x);
  57. }
  58. /* ??? avoid underflow ??? */
  59. g = x * x;
  60. x += x * g * POLYNOM3(g, p) / POLYNOM4(g, q);
  61. if (n > 1) x = -x;
  62. x += a[n];
  63. return neg ? -x : x;
  64. }