xref: /original-bsd/lib/libm/common/atan2.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1985, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)atan2.c	8.1 (Berkeley) 06/04/93";
10 #endif /* not lint */
11 
12 /* ATAN2(Y,X)
13  * RETURN ARG (X+iY)
14  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
15  * CODED IN C BY K.C. NG, 1/8/85;
16  * REVISED BY K.C. NG on 2/7/85, 2/13/85, 3/7/85, 3/30/85, 6/29/85.
17  *
18  * Required system supported functions :
19  *	copysign(x,y)
20  *	scalb(x,y)
21  *	logb(x)
22  *
23  * Method :
24  *	1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
25  *	2. Reduce x to positive by (if x and y are unexceptional):
26  *		ARG (x+iy) = arctan(y/x)   	   ... if x > 0,
27  *		ARG (x+iy) = pi - arctan[y/(-x)]   ... if x < 0,
28  *	3. According to the integer k=4t+0.25 truncated , t=y/x, the argument
29  *	   is further reduced to one of the following intervals and the
30  *	   arctangent of y/x is evaluated by the corresponding formula:
31  *
32  *         [0,7/16]	   atan(y/x) = t - t^3*(a1+t^2*(a2+...(a10+t^2*a11)...)
33  *	   [7/16,11/16]    atan(y/x) = atan(1/2) + atan( (y-x/2)/(x+y/2) )
34  *	   [11/16.19/16]   atan(y/x) = atan( 1 ) + atan( (y-x)/(x+y) )
35  *	   [19/16,39/16]   atan(y/x) = atan(3/2) + atan( (y-1.5x)/(x+1.5y) )
36  *	   [39/16,INF]     atan(y/x) = atan(INF) + atan( -x/y )
37  *
38  * Special cases:
39  * Notations: atan2(y,x) == ARG (x+iy) == ARG(x,y).
40  *
41  *	ARG( NAN , (anything) ) is NaN;
42  *	ARG( (anything), NaN ) is NaN;
43  *	ARG(+(anything but NaN), +-0) is +-0  ;
44  *	ARG(-(anything but NaN), +-0) is +-PI ;
45  *	ARG( 0, +-(anything but 0 and NaN) ) is +-PI/2;
46  *	ARG( +INF,+-(anything but INF and NaN) ) is +-0 ;
47  *	ARG( -INF,+-(anything but INF and NaN) ) is +-PI;
48  *	ARG( +INF,+-INF ) is +-PI/4 ;
49  *	ARG( -INF,+-INF ) is +-3PI/4;
50  *	ARG( (anything but,0,NaN, and INF),+-INF ) is +-PI/2;
51  *
52  * Accuracy:
53  *	atan2(y,x) returns (PI/pi) * the exact ARG (x+iy) nearly rounded,
54  *	where
55  *
56  *	in decimal:
57  *		pi = 3.141592653589793 23846264338327 .....
58  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
59  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
60  *
61  *	in hexadecimal:
62  *		pi = 3.243F6A8885A308D313198A2E....
63  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18	error=.276ulps
64  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2    error=.206ulps
65  *
66  *	In a test run with 356,000 random argument on [-1,1] * [-1,1] on a
67  *	VAX, the maximum observed error was 1.41 ulps (units of the last place)
68  *	compared with (PI/pi)*(the exact ARG(x+iy)).
69  *
70  * Note:
71  *	We use machine PI (the true pi rounded) in place of the actual
72  *	value of pi for all the trig and inverse trig functions. In general,
73  *	if trig is one of sin, cos, tan, then computed trig(y) returns the
74  *	exact trig(y*pi/PI) nearly rounded; correspondingly, computed arctrig
75  *	returns the exact arctrig(y)*PI/pi nearly rounded. These guarantee the
76  *	trig functions have period PI, and trig(arctrig(x)) returns x for
77  *	all critical values x.
78  *
79  * Constants:
80  * The hexadecimal values are the intended ones for the following constants.
81  * The decimal values may be used, provided that the compiler will convert
82  * from decimal to binary accurately enough to produce the hexadecimal values
83  * shown.
84  */
85 
86 #include "mathimpl.h"
87 
88 vc(athfhi, 4.6364760900080611433E-1  ,6338,3fed,da7b,2b0d,  -1, .ED63382B0DDA7B)
89 vc(athflo, 1.9338828231967579916E-19 ,5005,2164,92c0,9cfe, -62, .E450059CFE92C0)
90 vc(PIo4,   7.8539816339744830676E-1  ,0fda,4049,68c2,a221,   0, .C90FDAA22168C2)
91 vc(at1fhi, 9.8279372324732906796E-1  ,985e,407b,b4d9,940f,   0, .FB985E940FB4D9)
92 vc(at1flo,-3.5540295636764633916E-18 ,1edc,a383,eaea,34d6, -57,-.831EDC34D6EAEA)
93 vc(PIo2,   1.5707963267948966135E0   ,0fda,40c9,68c2,a221,   1, .C90FDAA22168C2)
94 vc(PI,     3.1415926535897932270E0   ,0fda,4149,68c2,a221,   2, .C90FDAA22168C2)
95 vc(a1,     3.3333333333333473730E-1  ,aaaa,3faa,ab75,aaaa,  -1, .AAAAAAAAAAAB75)
96 vc(a2,    -2.0000000000017730678E-1  ,cccc,bf4c,946e,cccd,  -2,-.CCCCCCCCCD946E)
97 vc(a3,     1.4285714286694640301E-1  ,4924,3f12,4262,9274,  -2, .92492492744262)
98 vc(a4,    -1.1111111135032672795E-1  ,8e38,bee3,6292,ebc6,  -3,-.E38E38EBC66292)
99 vc(a5,     9.0909091380563043783E-2  ,2e8b,3eba,d70c,b31b,  -3, .BA2E8BB31BD70C)
100 vc(a6,    -7.6922954286089459397E-2  ,89c8,be9d,7f18,27c3,  -3,-.9D89C827C37F18)
101 vc(a7,     6.6663180891693915586E-2  ,86b4,3e88,9e58,ae37,  -3, .8886B4AE379E58)
102 vc(a8,    -5.8772703698290408927E-2  ,bba5,be70,a942,8481,  -4,-.F0BBA58481A942)
103 vc(a9,     5.2170707402812969804E-2  ,b0f3,3e55,13ab,a1ab,  -4, .D5B0F3A1AB13AB)
104 vc(a10,   -4.4895863157820361210E-2  ,e4b9,be37,048f,7fd1,  -4,-.B7E4B97FD1048F)
105 vc(a11,    3.3006147437343875094E-2  ,3174,3e07,2d87,3cf7,  -4, .8731743CF72D87)
106 vc(a12,   -1.4614844866464185439E-2  ,731a,bd6f,76d9,2f34,  -6,-.EF731A2F3476D9)
107 
108 ic(athfhi, 4.6364760900080609352E-1  ,  -2,  1.DAC670561BB4F)
109 ic(athflo, 4.6249969567426939759E-18 , -58,  1.5543B8F253271)
110 ic(PIo4,   7.8539816339744827900E-1  ,  -1,  1.921FB54442D18)
111 ic(at1fhi, 9.8279372324732905408E-1  ,  -1,  1.F730BD281F69B)
112 ic(at1flo,-2.4407677060164810007E-17 , -56, -1.C23DFEFEAE6B5)
113 ic(PIo2,   1.5707963267948965580E0   ,   0,  1.921FB54442D18)
114 ic(PI,     3.1415926535897931160E0   ,   1,  1.921FB54442D18)
115 ic(a1,     3.3333333333333942106E-1  ,  -2,  1.55555555555C3)
116 ic(a2,    -1.9999999999979536924E-1  ,  -3, -1.9999999997CCD)
117 ic(a3,     1.4285714278004377209E-1  ,  -3,  1.24924921EC1D7)
118 ic(a4,    -1.1111110579344973814E-1  ,  -4, -1.C71C7059AF280)
119 ic(a5,     9.0908906105474668324E-2  ,  -4,  1.745CE5AA35DB2)
120 ic(a6,    -7.6919217767468239799E-2  ,  -4, -1.3B0FA54BEC400)
121 ic(a7,     6.6614695906082474486E-2  ,  -4,  1.10DA924597FFF)
122 ic(a8,    -5.8358371008508623523E-2  ,  -5, -1.DE125FDDBD793)
123 ic(a9,     4.9850617156082015213E-2  ,  -5,  1.9860524BDD807)
124 ic(a10,   -3.6700606902093604877E-2  ,  -5, -1.2CA6C04C6937A)
125 ic(a11,    1.6438029044759730479E-2  ,  -6,  1.0D52174A1BB54)
126 
127 #ifdef vccast
128 #define	athfhi	vccast(athfhi)
129 #define	athflo	vccast(athflo)
130 #define	PIo4	vccast(PIo4)
131 #define	at1fhi	vccast(at1fhi)
132 #define	at1flo	vccast(at1flo)
133 #define	PIo2	vccast(PIo2)
134 #define	PI	vccast(PI)
135 #define	a1	vccast(a1)
136 #define	a2	vccast(a2)
137 #define	a3	vccast(a3)
138 #define	a4	vccast(a4)
139 #define	a5	vccast(a5)
140 #define	a6	vccast(a6)
141 #define	a7	vccast(a7)
142 #define	a8	vccast(a8)
143 #define	a9	vccast(a9)
144 #define	a10	vccast(a10)
145 #define	a11	vccast(a11)
146 #define	a12	vccast(a12)
147 #endif
148 
149 double atan2(y,x)
150 double  y,x;
151 {
152 	static const double zero=0, one=1, small=1.0E-9, big=1.0E18;
153 	double t,z,signy,signx,hi,lo;
154 	int k,m;
155 
156 #if !defined(vax)&&!defined(tahoe)
157     /* if x or y is NAN */
158 	if(x!=x) return(x); if(y!=y) return(y);
159 #endif	/* !defined(vax)&&!defined(tahoe) */
160 
161     /* copy down the sign of y and x */
162 	signy = copysign(one,y) ;
163 	signx = copysign(one,x) ;
164 
165     /* if x is 1.0, goto begin */
166 	if(x==1) { y=copysign(y,one); t=y; if(finite(t)) goto begin;}
167 
168     /* when y = 0 */
169 	if(y==zero) return((signx==one)?y:copysign(PI,signy));
170 
171     /* when x = 0 */
172 	if(x==zero) return(copysign(PIo2,signy));
173 
174     /* when x is INF */
175 	if(!finite(x))
176 	    if(!finite(y))
177 		return(copysign((signx==one)?PIo4:3*PIo4,signy));
178 	    else
179 		return(copysign((signx==one)?zero:PI,signy));
180 
181     /* when y is INF */
182 	if(!finite(y)) return(copysign(PIo2,signy));
183 
184     /* compute y/x */
185 	x=copysign(x,one);
186 	y=copysign(y,one);
187 	if((m=(k=logb(y))-logb(x)) > 60) t=big+big;
188 	    else if(m < -80 ) t=y/x;
189 	    else { t = y/x ; y = scalb(y,-k); x=scalb(x,-k); }
190 
191     /* begin argument reduction */
192 begin:
193 	if (t < 2.4375) {
194 
195 	/* truncate 4(t+1/16) to integer for branching */
196 	    k = 4 * (t+0.0625);
197 	    switch (k) {
198 
199 	    /* t is in [0,7/16] */
200 	    case 0:
201 	    case 1:
202 		if (t < small)
203 		    { big + small ;  /* raise inexact flag */
204 		      return (copysign((signx>zero)?t:PI-t,signy)); }
205 
206 		hi = zero;  lo = zero;  break;
207 
208 	    /* t is in [7/16,11/16] */
209 	    case 2:
210 		hi = athfhi; lo = athflo;
211 		z = x+x;
212 		t = ( (y+y) - x ) / ( z +  y ); break;
213 
214 	    /* t is in [11/16,19/16] */
215 	    case 3:
216 	    case 4:
217 		hi = PIo4; lo = zero;
218 		t = ( y - x ) / ( x + y ); break;
219 
220 	    /* t is in [19/16,39/16] */
221 	    default:
222 		hi = at1fhi; lo = at1flo;
223 		z = y-x; y=y+y+y; t = x+x;
224 		t = ( (z+z)-x ) / ( t + y ); break;
225 	    }
226 	}
227 	/* end of if (t < 2.4375) */
228 
229 	else
230 	{
231 	    hi = PIo2; lo = zero;
232 
233 	    /* t is in [2.4375, big] */
234 	    if (t <= big)  t = - x / y;
235 
236 	    /* t is in [big, INF] */
237 	    else
238 	      { big+small;	/* raise inexact flag */
239 		t = zero; }
240 	}
241     /* end of argument reduction */
242 
243     /* compute atan(t) for t in [-.4375, .4375] */
244 	z = t*t;
245 #if defined(vax)||defined(tahoe)
246 	z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+
247 			z*(a9+z*(a10+z*(a11+z*a12))))))))))));
248 #else	/* defined(vax)||defined(tahoe) */
249 	z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+
250 			z*(a9+z*(a10+z*a11)))))))))));
251 #endif	/* defined(vax)||defined(tahoe) */
252 	z = lo - z; z += t; z += hi;
253 
254 	return(copysign((signx>zero)?z:PI-z,signy));
255 }
256