atan.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. floating-point arctangent
  11. atan returns the value of the arctangent of its
  12. argument in the range [-pi/2,pi/2].
  13. atan2 returns the arctangent of arg1/arg2
  14. in the range [-pi,pi].
  15. there are no error returns.
  16. coefficients are #5077 from Hart & Cheney. (19.56D)
  17. */
  18. #include <u.h>
  19. #include <libc.h>
  20. #define sq2p1 2.414213562373095048802e0
  21. #define sq2m1 .414213562373095048802e0
  22. #define p4 .161536412982230228262e2
  23. #define p3 .26842548195503973794141e3
  24. #define p2 .11530293515404850115428136e4
  25. #define p1 .178040631643319697105464587e4
  26. #define p0 .89678597403663861959987488e3
  27. #define q4 .5895697050844462222791e2
  28. #define q3 .536265374031215315104235e3
  29. #define q2 .16667838148816337184521798e4
  30. #define q1 .207933497444540981287275926e4
  31. #define q0 .89678597403663861962481162e3
  32. /*
  33. xatan evaluates a series valid in the
  34. range [-0.414...,+0.414...]. (tan(pi/8))
  35. */
  36. static
  37. double
  38. xatan(double arg)
  39. {
  40. double argsq, value;
  41. argsq = arg*arg;
  42. value = ((((p4*argsq + p3)*argsq + p2)*argsq + p1)*argsq + p0);
  43. value = value/(((((argsq + q4)*argsq + q3)*argsq + q2)*argsq + q1)*argsq + q0);
  44. return value*arg;
  45. }
  46. /*
  47. satan reduces its argument (known to be positive)
  48. to the range [0,0.414...] and calls xatan.
  49. */
  50. static
  51. double
  52. satan(double arg)
  53. {
  54. if(arg < sq2m1)
  55. return xatan(arg);
  56. if(arg > sq2p1)
  57. return PIO2 - xatan(1/arg);
  58. return PIO2/2 + xatan((arg-1)/(arg+1));
  59. }
  60. /*
  61. atan makes its argument positive and
  62. calls the inner routine satan.
  63. */
  64. double
  65. atan(double arg)
  66. {
  67. if(arg > 0)
  68. return satan(arg);
  69. return -satan(-arg);
  70. }