xref: /original-bsd/lib/libm/common/atan2.c (revision 633a6655)
15f692eeaSzliu /*
2*633a6655Sbostic  * Copyright (c) 1985, 1993
3*633a6655Sbostic  *	The Regents of the University of California.  All rights reserved.
45f692eeaSzliu  *
5833fc2a5Sbostic  * %sccs.include.redist.c%
65f692eeaSzliu  */
75f692eeaSzliu 
85f692eeaSzliu #ifndef lint
9*633a6655Sbostic static char sccsid[] = "@(#)atan2.c	8.1 (Berkeley) 06/04/93";
1037736ad6Szliu #endif /* not lint */
115f692eeaSzliu 
125f692eeaSzliu /* ATAN2(Y,X)
135f692eeaSzliu  * RETURN ARG (X+iY)
145f692eeaSzliu  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
155f692eeaSzliu  * CODED IN C BY K.C. NG, 1/8/85;
165f692eeaSzliu  * REVISED BY K.C. NG on 2/7/85, 2/13/85, 3/7/85, 3/30/85, 6/29/85.
175f692eeaSzliu  *
185f692eeaSzliu  * Required system supported functions :
195f692eeaSzliu  *	copysign(x,y)
205f692eeaSzliu  *	scalb(x,y)
215f692eeaSzliu  *	logb(x)
225f692eeaSzliu  *
235f692eeaSzliu  * Method :
245f692eeaSzliu  *	1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
255f692eeaSzliu  *	2. Reduce x to positive by (if x and y are unexceptional):
265f692eeaSzliu  *		ARG (x+iy) = arctan(y/x)   	   ... if x > 0,
275f692eeaSzliu  *		ARG (x+iy) = pi - arctan[y/(-x)]   ... if x < 0,
285f692eeaSzliu  *	3. According to the integer k=4t+0.25 truncated , t=y/x, the argument
295f692eeaSzliu  *	   is further reduced to one of the following intervals and the
305f692eeaSzliu  *	   arctangent of y/x is evaluated by the corresponding formula:
315f692eeaSzliu  *
325f692eeaSzliu  *         [0,7/16]	   atan(y/x) = t - t^3*(a1+t^2*(a2+...(a10+t^2*a11)...)
335f692eeaSzliu  *	   [7/16,11/16]    atan(y/x) = atan(1/2) + atan( (y-x/2)/(x+y/2) )
345f692eeaSzliu  *	   [11/16.19/16]   atan(y/x) = atan( 1 ) + atan( (y-x)/(x+y) )
355f692eeaSzliu  *	   [19/16,39/16]   atan(y/x) = atan(3/2) + atan( (y-1.5x)/(x+1.5y) )
365f692eeaSzliu  *	   [39/16,INF]     atan(y/x) = atan(INF) + atan( -x/y )
375f692eeaSzliu  *
385f692eeaSzliu  * Special cases:
395f692eeaSzliu  * Notations: atan2(y,x) == ARG (x+iy) == ARG(x,y).
405f692eeaSzliu  *
415f692eeaSzliu  *	ARG( NAN , (anything) ) is NaN;
425f692eeaSzliu  *	ARG( (anything), NaN ) is NaN;
435f692eeaSzliu  *	ARG(+(anything but NaN), +-0) is +-0  ;
445f692eeaSzliu  *	ARG(-(anything but NaN), +-0) is +-PI ;
455f692eeaSzliu  *	ARG( 0, +-(anything but 0 and NaN) ) is +-PI/2;
465f692eeaSzliu  *	ARG( +INF,+-(anything but INF and NaN) ) is +-0 ;
475f692eeaSzliu  *	ARG( -INF,+-(anything but INF and NaN) ) is +-PI;
485f692eeaSzliu  *	ARG( +INF,+-INF ) is +-PI/4 ;
495f692eeaSzliu  *	ARG( -INF,+-INF ) is +-3PI/4;
505f692eeaSzliu  *	ARG( (anything but,0,NaN, and INF),+-INF ) is +-PI/2;
515f692eeaSzliu  *
525f692eeaSzliu  * Accuracy:
535f692eeaSzliu  *	atan2(y,x) returns (PI/pi) * the exact ARG (x+iy) nearly rounded,
545f692eeaSzliu  *	where
555f692eeaSzliu  *
565f692eeaSzliu  *	in decimal:
575f692eeaSzliu  *		pi = 3.141592653589793 23846264338327 .....
585f692eeaSzliu  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
595f692eeaSzliu  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
605f692eeaSzliu  *
615f692eeaSzliu  *	in hexadecimal:
625f692eeaSzliu  *		pi = 3.243F6A8885A308D313198A2E....
635f692eeaSzliu  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18	error=.276ulps
645f692eeaSzliu  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2    error=.206ulps
655f692eeaSzliu  *
665f692eeaSzliu  *	In a test run with 356,000 random argument on [-1,1] * [-1,1] on a
675f692eeaSzliu  *	VAX, the maximum observed error was 1.41 ulps (units of the last place)
685f692eeaSzliu  *	compared with (PI/pi)*(the exact ARG(x+iy)).
695f692eeaSzliu  *
705f692eeaSzliu  * Note:
715f692eeaSzliu  *	We use machine PI (the true pi rounded) in place of the actual
725f692eeaSzliu  *	value of pi for all the trig and inverse trig functions. In general,
735f692eeaSzliu  *	if trig is one of sin, cos, tan, then computed trig(y) returns the
745f692eeaSzliu  *	exact trig(y*pi/PI) nearly rounded; correspondingly, computed arctrig
755f692eeaSzliu  *	returns the exact arctrig(y)*PI/pi nearly rounded. These guarantee the
765f692eeaSzliu  *	trig functions have period PI, and trig(arctrig(x)) returns x for
775f692eeaSzliu  *	all critical values x.
785f692eeaSzliu  *
795f692eeaSzliu  * Constants:
805f692eeaSzliu  * The hexadecimal values are the intended ones for the following constants.
815f692eeaSzliu  * The decimal values may be used, provided that the compiler will convert
825f692eeaSzliu  * from decimal to binary accurately enough to produce the hexadecimal values
835f692eeaSzliu  * shown.
845f692eeaSzliu  */
855f692eeaSzliu 
86e81ff4fcSbostic #include "mathimpl.h"
87e81ff4fcSbostic 
88e81ff4fcSbostic vc(athfhi, 4.6364760900080611433E-1  ,6338,3fed,da7b,2b0d,  -1, .ED63382B0DDA7B)
89e81ff4fcSbostic vc(athflo, 1.9338828231967579916E-19 ,5005,2164,92c0,9cfe, -62, .E450059CFE92C0)
90e81ff4fcSbostic vc(PIo4,   7.8539816339744830676E-1  ,0fda,4049,68c2,a221,   0, .C90FDAA22168C2)
91e81ff4fcSbostic vc(at1fhi, 9.8279372324732906796E-1  ,985e,407b,b4d9,940f,   0, .FB985E940FB4D9)
92e81ff4fcSbostic vc(at1flo,-3.5540295636764633916E-18 ,1edc,a383,eaea,34d6, -57,-.831EDC34D6EAEA)
93e81ff4fcSbostic vc(PIo2,   1.5707963267948966135E0   ,0fda,40c9,68c2,a221,   1, .C90FDAA22168C2)
94e81ff4fcSbostic vc(PI,     3.1415926535897932270E0   ,0fda,4149,68c2,a221,   2, .C90FDAA22168C2)
95e81ff4fcSbostic vc(a1,     3.3333333333333473730E-1  ,aaaa,3faa,ab75,aaaa,  -1, .AAAAAAAAAAAB75)
96e81ff4fcSbostic vc(a2,    -2.0000000000017730678E-1  ,cccc,bf4c,946e,cccd,  -2,-.CCCCCCCCCD946E)
97e81ff4fcSbostic vc(a3,     1.4285714286694640301E-1  ,4924,3f12,4262,9274,  -2, .92492492744262)
98e81ff4fcSbostic vc(a4,    -1.1111111135032672795E-1  ,8e38,bee3,6292,ebc6,  -3,-.E38E38EBC66292)
99e81ff4fcSbostic vc(a5,     9.0909091380563043783E-2  ,2e8b,3eba,d70c,b31b,  -3, .BA2E8BB31BD70C)
100e81ff4fcSbostic vc(a6,    -7.6922954286089459397E-2  ,89c8,be9d,7f18,27c3,  -3,-.9D89C827C37F18)
101e81ff4fcSbostic vc(a7,     6.6663180891693915586E-2  ,86b4,3e88,9e58,ae37,  -3, .8886B4AE379E58)
102e81ff4fcSbostic vc(a8,    -5.8772703698290408927E-2  ,bba5,be70,a942,8481,  -4,-.F0BBA58481A942)
103e81ff4fcSbostic vc(a9,     5.2170707402812969804E-2  ,b0f3,3e55,13ab,a1ab,  -4, .D5B0F3A1AB13AB)
104e81ff4fcSbostic vc(a10,   -4.4895863157820361210E-2  ,e4b9,be37,048f,7fd1,  -4,-.B7E4B97FD1048F)
105e81ff4fcSbostic vc(a11,    3.3006147437343875094E-2  ,3174,3e07,2d87,3cf7,  -4, .8731743CF72D87)
106e81ff4fcSbostic vc(a12,   -1.4614844866464185439E-2  ,731a,bd6f,76d9,2f34,  -6,-.EF731A2F3476D9)
107e81ff4fcSbostic 
108e81ff4fcSbostic ic(athfhi, 4.6364760900080609352E-1  ,  -2,  1.DAC670561BB4F)
109e81ff4fcSbostic ic(athflo, 4.6249969567426939759E-18 , -58,  1.5543B8F253271)
110e81ff4fcSbostic ic(PIo4,   7.8539816339744827900E-1  ,  -1,  1.921FB54442D18)
111e81ff4fcSbostic ic(at1fhi, 9.8279372324732905408E-1  ,  -1,  1.F730BD281F69B)
112e81ff4fcSbostic ic(at1flo,-2.4407677060164810007E-17 , -56, -1.C23DFEFEAE6B5)
113e81ff4fcSbostic ic(PIo2,   1.5707963267948965580E0   ,   0,  1.921FB54442D18)
114e81ff4fcSbostic ic(PI,     3.1415926535897931160E0   ,   1,  1.921FB54442D18)
115e81ff4fcSbostic ic(a1,     3.3333333333333942106E-1  ,  -2,  1.55555555555C3)
116e81ff4fcSbostic ic(a2,    -1.9999999999979536924E-1  ,  -3, -1.9999999997CCD)
117e81ff4fcSbostic ic(a3,     1.4285714278004377209E-1  ,  -3,  1.24924921EC1D7)
118e81ff4fcSbostic ic(a4,    -1.1111110579344973814E-1  ,  -4, -1.C71C7059AF280)
119e81ff4fcSbostic ic(a5,     9.0908906105474668324E-2  ,  -4,  1.745CE5AA35DB2)
120e81ff4fcSbostic ic(a6,    -7.6919217767468239799E-2  ,  -4, -1.3B0FA54BEC400)
121e81ff4fcSbostic ic(a7,     6.6614695906082474486E-2  ,  -4,  1.10DA924597FFF)
122e81ff4fcSbostic ic(a8,    -5.8358371008508623523E-2  ,  -5, -1.DE125FDDBD793)
123e81ff4fcSbostic ic(a9,     4.9850617156082015213E-2  ,  -5,  1.9860524BDD807)
124e81ff4fcSbostic ic(a10,   -3.6700606902093604877E-2  ,  -5, -1.2CA6C04C6937A)
125e81ff4fcSbostic ic(a11,    1.6438029044759730479E-2  ,  -6,  1.0D52174A1BB54)
126e81ff4fcSbostic 
127e81ff4fcSbostic #ifdef vccast
128e81ff4fcSbostic #define	athfhi	vccast(athfhi)
129e81ff4fcSbostic #define	athflo	vccast(athflo)
130e81ff4fcSbostic #define	PIo4	vccast(PIo4)
131e81ff4fcSbostic #define	at1fhi	vccast(at1fhi)
132e81ff4fcSbostic #define	at1flo	vccast(at1flo)
133e81ff4fcSbostic #define	PIo2	vccast(PIo2)
134e81ff4fcSbostic #define	PI	vccast(PI)
135e81ff4fcSbostic #define	a1	vccast(a1)
136e81ff4fcSbostic #define	a2	vccast(a2)
137e81ff4fcSbostic #define	a3	vccast(a3)
138e81ff4fcSbostic #define	a4	vccast(a4)
139e81ff4fcSbostic #define	a5	vccast(a5)
140e81ff4fcSbostic #define	a6	vccast(a6)
141e81ff4fcSbostic #define	a7	vccast(a7)
142e81ff4fcSbostic #define	a8	vccast(a8)
143e81ff4fcSbostic #define	a9	vccast(a9)
144e81ff4fcSbostic #define	a10	vccast(a10)
145e81ff4fcSbostic #define	a11	vccast(a11)
146e81ff4fcSbostic #define	a12	vccast(a12)
147e81ff4fcSbostic #endif
1485f692eeaSzliu 
1495f692eeaSzliu double atan2(y,x)
1505f692eeaSzliu double  y,x;
1515f692eeaSzliu {
152e81ff4fcSbostic 	static const double zero=0, one=1, small=1.0E-9, big=1.0E18;
153e81ff4fcSbostic 	double t,z,signy,signx,hi,lo;
154e81ff4fcSbostic 	int k,m;
1555f692eeaSzliu 
15637736ad6Szliu #if !defined(vax)&&!defined(tahoe)
1575f692eeaSzliu     /* if x or y is NAN */
1585f692eeaSzliu 	if(x!=x) return(x); if(y!=y) return(y);
15937736ad6Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
1605f692eeaSzliu 
1615f692eeaSzliu     /* copy down the sign of y and x */
1625f692eeaSzliu 	signy = copysign(one,y) ;
1635f692eeaSzliu 	signx = copysign(one,x) ;
1645f692eeaSzliu 
1655f692eeaSzliu     /* if x is 1.0, goto begin */
1665f692eeaSzliu 	if(x==1) { y=copysign(y,one); t=y; if(finite(t)) goto begin;}
1675f692eeaSzliu 
1685f692eeaSzliu     /* when y = 0 */
1695f692eeaSzliu 	if(y==zero) return((signx==one)?y:copysign(PI,signy));
1705f692eeaSzliu 
1715f692eeaSzliu     /* when x = 0 */
1725f692eeaSzliu 	if(x==zero) return(copysign(PIo2,signy));
1735f692eeaSzliu 
1745f692eeaSzliu     /* when x is INF */
1755f692eeaSzliu 	if(!finite(x))
1765f692eeaSzliu 	    if(!finite(y))
1775f692eeaSzliu 		return(copysign((signx==one)?PIo4:3*PIo4,signy));
1785f692eeaSzliu 	    else
1795f692eeaSzliu 		return(copysign((signx==one)?zero:PI,signy));
1805f692eeaSzliu 
1815f692eeaSzliu     /* when y is INF */
1825f692eeaSzliu 	if(!finite(y)) return(copysign(PIo2,signy));
1835f692eeaSzliu 
1845f692eeaSzliu     /* compute y/x */
1855f692eeaSzliu 	x=copysign(x,one);
1865f692eeaSzliu 	y=copysign(y,one);
1875f692eeaSzliu 	if((m=(k=logb(y))-logb(x)) > 60) t=big+big;
1885f692eeaSzliu 	    else if(m < -80 ) t=y/x;
1895f692eeaSzliu 	    else { t = y/x ; y = scalb(y,-k); x=scalb(x,-k); }
1905f692eeaSzliu 
1915f692eeaSzliu     /* begin argument reduction */
1925f692eeaSzliu begin:
1935f692eeaSzliu 	if (t < 2.4375) {
1945f692eeaSzliu 
1955f692eeaSzliu 	/* truncate 4(t+1/16) to integer for branching */
1965f692eeaSzliu 	    k = 4 * (t+0.0625);
1975f692eeaSzliu 	    switch (k) {
1985f692eeaSzliu 
1995f692eeaSzliu 	    /* t is in [0,7/16] */
2005f692eeaSzliu 	    case 0:
2015f692eeaSzliu 	    case 1:
2025f692eeaSzliu 		if (t < small)
2035f692eeaSzliu 		    { big + small ;  /* raise inexact flag */
2045f692eeaSzliu 		      return (copysign((signx>zero)?t:PI-t,signy)); }
2055f692eeaSzliu 
2065f692eeaSzliu 		hi = zero;  lo = zero;  break;
2075f692eeaSzliu 
2085f692eeaSzliu 	    /* t is in [7/16,11/16] */
2095f692eeaSzliu 	    case 2:
2105f692eeaSzliu 		hi = athfhi; lo = athflo;
2115f692eeaSzliu 		z = x+x;
2125f692eeaSzliu 		t = ( (y+y) - x ) / ( z +  y ); break;
2135f692eeaSzliu 
2145f692eeaSzliu 	    /* t is in [11/16,19/16] */
2155f692eeaSzliu 	    case 3:
2165f692eeaSzliu 	    case 4:
2175f692eeaSzliu 		hi = PIo4; lo = zero;
2185f692eeaSzliu 		t = ( y - x ) / ( x + y ); break;
2195f692eeaSzliu 
2205f692eeaSzliu 	    /* t is in [19/16,39/16] */
2215f692eeaSzliu 	    default:
2225f692eeaSzliu 		hi = at1fhi; lo = at1flo;
2235f692eeaSzliu 		z = y-x; y=y+y+y; t = x+x;
2245f692eeaSzliu 		t = ( (z+z)-x ) / ( t + y ); break;
2255f692eeaSzliu 	    }
2265f692eeaSzliu 	}
2275f692eeaSzliu 	/* end of if (t < 2.4375) */
2285f692eeaSzliu 
2295f692eeaSzliu 	else
2305f692eeaSzliu 	{
2315f692eeaSzliu 	    hi = PIo2; lo = zero;
2325f692eeaSzliu 
2335f692eeaSzliu 	    /* t is in [2.4375, big] */
2345f692eeaSzliu 	    if (t <= big)  t = - x / y;
2355f692eeaSzliu 
2365f692eeaSzliu 	    /* t is in [big, INF] */
2375f692eeaSzliu 	    else
2385f692eeaSzliu 	      { big+small;	/* raise inexact flag */
2395f692eeaSzliu 		t = zero; }
2405f692eeaSzliu 	}
2415f692eeaSzliu     /* end of argument reduction */
2425f692eeaSzliu 
2435f692eeaSzliu     /* compute atan(t) for t in [-.4375, .4375] */
2445f692eeaSzliu 	z = t*t;
24537736ad6Szliu #if defined(vax)||defined(tahoe)
2465f692eeaSzliu 	z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+
2475f692eeaSzliu 			z*(a9+z*(a10+z*(a11+z*a12))))))))))));
24837736ad6Szliu #else	/* defined(vax)||defined(tahoe) */
2495f692eeaSzliu 	z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+
2505f692eeaSzliu 			z*(a9+z*(a10+z*a11)))))))))));
25137736ad6Szliu #endif	/* defined(vax)||defined(tahoe) */
2525f692eeaSzliu 	z = lo - z; z += t; z += hi;
2535f692eeaSzliu 
2545f692eeaSzliu 	return(copysign((signx>zero)?z:PI-z,signy));
2555f692eeaSzliu }
256