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