1 /* origin: FreeBSD /usr/src/lib/msun/src/e_atan2.c */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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
10 * ====================================================
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,
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;
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.
43 pi
= 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */
44 pi_lo
= 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */
46 double atan2(double y
, double x
)
49 uint32_t m
,lx
,ly
,ix
,iy
;
51 if (isnan(x
) || isnan(y
))
53 EXTRACT_WORDS(ix
, lx
, x
);
54 EXTRACT_WORDS(iy
, ly
, y
);
55 if ((ix
-0x3ff00000 | lx
) == 0) /* x = 1.0 */
57 m
= ((iy
>>31)&1) | ((ix
>>30)&2); /* 2*sign(x)+sign(y) */
65 case 1: return y
; /* atan(+-0,+anything)=+-0 */
66 case 2: return pi
; /* atan(+0,-anything) = pi */
67 case 3: return -pi
; /* atan(-0,-anything) =-pi */
72 return m
&1 ? -pi
/2 : pi
/2;
74 if (ix
== 0x7ff00000) {
75 if (iy
== 0x7ff00000) {
77 case 0: return pi
/4; /* atan(+INF,+INF) */
78 case 1: return -pi
/4; /* atan(-INF,+INF) */
79 case 2: return 3*pi
/4; /* atan(+INF,-INF) */
80 case 3: return -3*pi
/4; /* atan(-INF,-INF) */
84 case 0: return 0.0; /* atan(+...,+INF) */
85 case 1: return -0.0; /* atan(-...,+INF) */
86 case 2: return pi
; /* atan(+...,-INF) */
87 case 3: return -pi
; /* atan(-...,-INF) */
92 if (ix
+(64<<20) < iy
|| iy
== 0x7ff00000)
93 return m
&1 ? -pi
/2 : pi
/2;
95 /* z = atan(|y/x|) without spurious underflow */
96 if ((m
&2) && iy
+(64<<20) < ix
) /* |y/x| < 0x1p-64, x<0 */
101 case 0: return z
; /* atan(+,+) */
102 case 1: return -z
; /* atan(-,+) */
103 case 2: return pi
- (z
-pi_lo
); /* atan(+,-) */
104 default: /* case 3 */
105 return (z
-pi_lo
) - pi
; /* atan(-,-) */