1
0

atan2.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* origin: FreeBSD /usr/src/lib/msun/src/e_atan2.c */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunSoft, a Sun Microsystems, Inc. business.
  7. * Permission to use, copy, modify, and distribute this
  8. * software is freely granted, provided that this notice
  9. * is preserved.
  10. * ====================================================
  11. *
  12. */
  13. /* atan2(y,x)
  14. * Method :
  15. * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
  16. * 2. Reduce x to positive by (if x and y are unexceptional):
  17. * ARG (x+iy) = arctan(y/x) ... if x > 0,
  18. * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0,
  19. *
  20. * Special cases:
  21. *
  22. * ATAN2((anything), NaN ) is NaN;
  23. * ATAN2(NAN , (anything) ) is NaN;
  24. * ATAN2(+-0, +(anything but NaN)) is +-0 ;
  25. * ATAN2(+-0, -(anything but NaN)) is +-pi ;
  26. * ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2;
  27. * ATAN2(+-(anything but INF and NaN), +INF) is +-0 ;
  28. * ATAN2(+-(anything but INF and NaN), -INF) is +-pi;
  29. * ATAN2(+-INF,+INF ) is +-pi/4 ;
  30. * ATAN2(+-INF,-INF ) is +-3pi/4;
  31. * ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;
  32. *
  33. * Constants:
  34. * The hexadecimal values are the intended ones for the following
  35. * constants. The decimal values may be used, provided that the
  36. * compiler will convert from decimal to binary accurately enough
  37. * to produce the hexadecimal values shown.
  38. */
  39. #include "libm.h"
  40. static const double
  41. pi = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */
  42. pi_lo = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */
  43. double atan2(double y, double x)
  44. {
  45. double z;
  46. uint32_t m,lx,ly,ix,iy;
  47. if (isnan(x) || isnan(y))
  48. return x+y;
  49. EXTRACT_WORDS(ix, lx, x);
  50. EXTRACT_WORDS(iy, ly, y);
  51. if ((ix-0x3ff00000 | lx) == 0) /* x = 1.0 */
  52. return atan(y);
  53. m = ((iy>>31)&1) | ((ix>>30)&2); /* 2*sign(x)+sign(y) */
  54. ix = ix & 0x7fffffff;
  55. iy = iy & 0x7fffffff;
  56. /* when y = 0 */
  57. if ((iy|ly) == 0) {
  58. switch(m) {
  59. case 0:
  60. case 1: return y; /* atan(+-0,+anything)=+-0 */
  61. case 2: return pi; /* atan(+0,-anything) = pi */
  62. case 3: return -pi; /* atan(-0,-anything) =-pi */
  63. }
  64. }
  65. /* when x = 0 */
  66. if ((ix|lx) == 0)
  67. return m&1 ? -pi/2 : pi/2;
  68. /* when x is INF */
  69. if (ix == 0x7ff00000) {
  70. if (iy == 0x7ff00000) {
  71. switch(m) {
  72. case 0: return pi/4; /* atan(+INF,+INF) */
  73. case 1: return -pi/4; /* atan(-INF,+INF) */
  74. case 2: return 3*pi/4; /* atan(+INF,-INF) */
  75. case 3: return -3*pi/4; /* atan(-INF,-INF) */
  76. }
  77. } else {
  78. switch(m) {
  79. case 0: return 0.0; /* atan(+...,+INF) */
  80. case 1: return -0.0; /* atan(-...,+INF) */
  81. case 2: return pi; /* atan(+...,-INF) */
  82. case 3: return -pi; /* atan(-...,-INF) */
  83. }
  84. }
  85. }
  86. /* |y/x| > 0x1p64 */
  87. if (ix+(64<<20) < iy || iy == 0x7ff00000)
  88. return m&1 ? -pi/2 : pi/2;
  89. /* z = atan(|y/x|) without spurious underflow */
  90. if ((m&2) && iy+(64<<20) < ix) /* |y/x| < 0x1p-64, x<0 */
  91. z = 0;
  92. else
  93. z = atan(fabs(y/x));
  94. switch (m) {
  95. case 0: return z; /* atan(+,+) */
  96. case 1: return -z; /* atan(-,+) */
  97. case 2: return pi - (z-pi_lo); /* atan(+,-) */
  98. default: /* case 3 */
  99. return (z-pi_lo) - pi; /* atan(-,-) */
  100. }
  101. }