134127Sbostic /* 224578Szliu * Copyright (c) 1985 Regents of the University of California. 334127Sbostic * All rights reserved. 434127Sbostic * 5*42653Sbostic * %sccs.include.redist.c% 634127Sbostic * 734127Sbostic * All recipients should regard themselves as participants in an ongoing 834127Sbostic * research project and hence should feel obligated to report their 934127Sbostic * experiences (good or bad) with these elementary function codes, using 1034127Sbostic * the sendbug(8) program, to the authors. 1124578Szliu */ 1224578Szliu 1324578Szliu #ifndef lint 14*42653Sbostic static char sccsid[] = "@(#)atan2.c 5.5 (Berkeley) 06/01/90"; 1534127Sbostic #endif /* not lint */ 1624578Szliu 1724578Szliu /* ATAN2(Y,X) 1824578Szliu * RETURN ARG (X+iY) 1924578Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 2024578Szliu * CODED IN C BY K.C. NG, 1/8/85; 2124578Szliu * REVISED BY K.C. NG on 2/7/85, 2/13/85, 3/7/85, 3/30/85, 6/29/85. 2224578Szliu * 2324578Szliu * Required system supported functions : 2424578Szliu * copysign(x,y) 2524578Szliu * scalb(x,y) 2624578Szliu * logb(x) 2724578Szliu * 2824578Szliu * Method : 2924578Szliu * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x). 3024578Szliu * 2. Reduce x to positive by (if x and y are unexceptional): 3124578Szliu * ARG (x+iy) = arctan(y/x) ... if x > 0, 3224578Szliu * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0, 3324578Szliu * 3. According to the integer k=4t+0.25 truncated , t=y/x, the argument 3424578Szliu * is further reduced to one of the following intervals and the 3524578Szliu * arctangent of y/x is evaluated by the corresponding formula: 3624578Szliu * 3724578Szliu * [0,7/16] atan(y/x) = t - t^3*(a1+t^2*(a2+...(a10+t^2*a11)...) 3824578Szliu * [7/16,11/16] atan(y/x) = atan(1/2) + atan( (y-x/2)/(x+y/2) ) 3924578Szliu * [11/16.19/16] atan(y/x) = atan( 1 ) + atan( (y-x)/(x+y) ) 4024578Szliu * [19/16,39/16] atan(y/x) = atan(3/2) + atan( (y-1.5x)/(x+1.5y) ) 4124578Szliu * [39/16,INF] atan(y/x) = atan(INF) + atan( -x/y ) 4224578Szliu * 4324578Szliu * Special cases: 4424578Szliu * Notations: atan2(y,x) == ARG (x+iy) == ARG(x,y). 4524578Szliu * 4624578Szliu * ARG( NAN , (anything) ) is NaN; 4724578Szliu * ARG( (anything), NaN ) is NaN; 4824578Szliu * ARG(+(anything but NaN), +-0) is +-0 ; 4924578Szliu * ARG(-(anything but NaN), +-0) is +-PI ; 5024578Szliu * ARG( 0, +-(anything but 0 and NaN) ) is +-PI/2; 5124578Szliu * ARG( +INF,+-(anything but INF and NaN) ) is +-0 ; 5224578Szliu * ARG( -INF,+-(anything but INF and NaN) ) is +-PI; 5324578Szliu * ARG( +INF,+-INF ) is +-PI/4 ; 5424578Szliu * ARG( -INF,+-INF ) is +-3PI/4; 5524578Szliu * ARG( (anything but,0,NaN, and INF),+-INF ) is +-PI/2; 5624578Szliu * 5724578Szliu * Accuracy: 5824578Szliu * atan2(y,x) returns (PI/pi) * the exact ARG (x+iy) nearly rounded, 5924578Szliu * where 6024578Szliu * 6124578Szliu * in decimal: 6224578Szliu * pi = 3.141592653589793 23846264338327 ..... 6324578Szliu * 53 bits PI = 3.141592653589793 115997963 ..... , 6424578Szliu * 56 bits PI = 3.141592653589793 227020265 ..... , 6524578Szliu * 6624578Szliu * in hexadecimal: 6724578Szliu * pi = 3.243F6A8885A308D313198A2E.... 6824578Szliu * 53 bits PI = 3.243F6A8885A30 = 2 * 1.921FB54442D18 error=.276ulps 6924578Szliu * 56 bits PI = 3.243F6A8885A308 = 4 * .C90FDAA22168C2 error=.206ulps 7024578Szliu * 7124578Szliu * In a test run with 356,000 random argument on [-1,1] * [-1,1] on a 7224578Szliu * VAX, the maximum observed error was 1.41 ulps (units of the last place) 7324578Szliu * compared with (PI/pi)*(the exact ARG(x+iy)). 7424578Szliu * 7524578Szliu * Note: 7624578Szliu * We use machine PI (the true pi rounded) in place of the actual 7724578Szliu * value of pi for all the trig and inverse trig functions. In general, 7824578Szliu * if trig is one of sin, cos, tan, then computed trig(y) returns the 7924578Szliu * exact trig(y*pi/PI) nearly rounded; correspondingly, computed arctrig 8024578Szliu * returns the exact arctrig(y)*PI/pi nearly rounded. These guarantee the 8124578Szliu * trig functions have period PI, and trig(arctrig(x)) returns x for 8224578Szliu * all critical values x. 8324578Szliu * 8424578Szliu * Constants: 8524578Szliu * The hexadecimal values are the intended ones for the following constants. 8624578Szliu * The decimal values may be used, provided that the compiler will convert 8724578Szliu * from decimal to binary accurately enough to produce the hexadecimal values 8824578Szliu * shown. 8924578Szliu */ 9024578Szliu 9135680Sbostic #include "mathimpl.h" 9224578Szliu 9335680Sbostic vc(athfhi, 4.6364760900080611433E-1 ,6338,3fed,da7b,2b0d, -1, .ED63382B0DDA7B) 9435680Sbostic vc(athflo, 1.9338828231967579916E-19 ,5005,2164,92c0,9cfe, -62, .E450059CFE92C0) 9535680Sbostic vc(PIo4, 7.8539816339744830676E-1 ,0fda,4049,68c2,a221, 0, .C90FDAA22168C2) 9635680Sbostic vc(at1fhi, 9.8279372324732906796E-1 ,985e,407b,b4d9,940f, 0, .FB985E940FB4D9) 9735680Sbostic vc(at1flo,-3.5540295636764633916E-18 ,1edc,a383,eaea,34d6, -57,-.831EDC34D6EAEA) 9835680Sbostic vc(PIo2, 1.5707963267948966135E0 ,0fda,40c9,68c2,a221, 1, .C90FDAA22168C2) 9935680Sbostic vc(PI, 3.1415926535897932270E0 ,0fda,4149,68c2,a221, 2, .C90FDAA22168C2) 10035680Sbostic vc(a1, 3.3333333333333473730E-1 ,aaaa,3faa,ab75,aaaa, -1, .AAAAAAAAAAAB75) 10135680Sbostic vc(a2, -2.0000000000017730678E-1 ,cccc,bf4c,946e,cccd, -2,-.CCCCCCCCCD946E) 10235680Sbostic vc(a3, 1.4285714286694640301E-1 ,4924,3f12,4262,9274, -2, .92492492744262) 10335680Sbostic vc(a4, -1.1111111135032672795E-1 ,8e38,bee3,6292,ebc6, -3,-.E38E38EBC66292) 10435680Sbostic vc(a5, 9.0909091380563043783E-2 ,2e8b,3eba,d70c,b31b, -3, .BA2E8BB31BD70C) 10535680Sbostic vc(a6, -7.6922954286089459397E-2 ,89c8,be9d,7f18,27c3, -3,-.9D89C827C37F18) 10635680Sbostic vc(a7, 6.6663180891693915586E-2 ,86b4,3e88,9e58,ae37, -3, .8886B4AE379E58) 10735680Sbostic vc(a8, -5.8772703698290408927E-2 ,bba5,be70,a942,8481, -4,-.F0BBA58481A942) 10835680Sbostic vc(a9, 5.2170707402812969804E-2 ,b0f3,3e55,13ab,a1ab, -4, .D5B0F3A1AB13AB) 10935680Sbostic vc(a10, -4.4895863157820361210E-2 ,e4b9,be37,048f,7fd1, -4,-.B7E4B97FD1048F) 11035680Sbostic vc(a11, 3.3006147437343875094E-2 ,3174,3e07,2d87,3cf7, -4, .8731743CF72D87) 11135680Sbostic vc(a12, -1.4614844866464185439E-2 ,731a,bd6f,76d9,2f34, -6,-.EF731A2F3476D9) 11235680Sbostic 11335680Sbostic ic(athfhi, 4.6364760900080609352E-1 , -2, 1.DAC670561BB4F) 11435680Sbostic ic(athflo, 4.6249969567426939759E-18 , -58, 1.5543B8F253271) 11535680Sbostic ic(PIo4, 7.8539816339744827900E-1 , -1, 1.921FB54442D18) 11635680Sbostic ic(at1fhi, 9.8279372324732905408E-1 , -1, 1.F730BD281F69B) 11735680Sbostic ic(at1flo,-2.4407677060164810007E-17 , -56, -1.C23DFEFEAE6B5) 11835680Sbostic ic(PIo2, 1.5707963267948965580E0 , 0, 1.921FB54442D18) 11935680Sbostic ic(PI, 3.1415926535897931160E0 , 1, 1.921FB54442D18) 12035680Sbostic ic(a1, 3.3333333333333942106E-1 , -2, 1.55555555555C3) 12135680Sbostic ic(a2, -1.9999999999979536924E-1 , -3, -1.9999999997CCD) 12235680Sbostic ic(a3, 1.4285714278004377209E-1 , -3, 1.24924921EC1D7) 12335680Sbostic ic(a4, -1.1111110579344973814E-1 , -4, -1.C71C7059AF280) 12435680Sbostic ic(a5, 9.0908906105474668324E-2 , -4, 1.745CE5AA35DB2) 12535680Sbostic ic(a6, -7.6919217767468239799E-2 , -4, -1.3B0FA54BEC400) 12635680Sbostic ic(a7, 6.6614695906082474486E-2 , -4, 1.10DA924597FFF) 12735680Sbostic ic(a8, -5.8358371008508623523E-2 , -5, -1.DE125FDDBD793) 12835680Sbostic ic(a9, 4.9850617156082015213E-2 , -5, 1.9860524BDD807) 12935680Sbostic ic(a10, -3.6700606902093604877E-2 , -5, -1.2CA6C04C6937A) 13035680Sbostic ic(a11, 1.6438029044759730479E-2 , -6, 1.0D52174A1BB54) 13135680Sbostic 13235680Sbostic #ifdef vccast 13335680Sbostic #define athfhi vccast(athfhi) 13435680Sbostic #define athflo vccast(athflo) 13535680Sbostic #define PIo4 vccast(PIo4) 13635680Sbostic #define at1fhi vccast(at1fhi) 13735680Sbostic #define at1flo vccast(at1flo) 13835680Sbostic #define PIo2 vccast(PIo2) 13935680Sbostic #define PI vccast(PI) 14035680Sbostic #define a1 vccast(a1) 14135680Sbostic #define a2 vccast(a2) 14235680Sbostic #define a3 vccast(a3) 14335680Sbostic #define a4 vccast(a4) 14435680Sbostic #define a5 vccast(a5) 14535680Sbostic #define a6 vccast(a6) 14635680Sbostic #define a7 vccast(a7) 14735680Sbostic #define a8 vccast(a8) 14835680Sbostic #define a9 vccast(a9) 14935680Sbostic #define a10 vccast(a10) 15035680Sbostic #define a11 vccast(a11) 15135680Sbostic #define a12 vccast(a12) 15235680Sbostic #endif 15335680Sbostic 15424578Szliu double atan2(y,x) 15524578Szliu double y,x; 15624578Szliu { 15735680Sbostic static const double zero=0, one=1, small=1.0E-9, big=1.0E18; 15835680Sbostic double t,z,signy,signx,hi,lo; 15935680Sbostic int k,m; 16024578Szliu 16131855Szliu #if !defined(vax)&&!defined(tahoe) 16224578Szliu /* if x or y is NAN */ 16324578Szliu if(x!=x) return(x); if(y!=y) return(y); 16431855Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 16524578Szliu 16624578Szliu /* copy down the sign of y and x */ 16724578Szliu signy = copysign(one,y) ; 16824578Szliu signx = copysign(one,x) ; 16924578Szliu 17024578Szliu /* if x is 1.0, goto begin */ 17124578Szliu if(x==1) { y=copysign(y,one); t=y; if(finite(t)) goto begin;} 17224578Szliu 17324578Szliu /* when y = 0 */ 17424578Szliu if(y==zero) return((signx==one)?y:copysign(PI,signy)); 17524578Szliu 17624578Szliu /* when x = 0 */ 17724578Szliu if(x==zero) return(copysign(PIo2,signy)); 17824578Szliu 17924578Szliu /* when x is INF */ 18024578Szliu if(!finite(x)) 18124578Szliu if(!finite(y)) 18224578Szliu return(copysign((signx==one)?PIo4:3*PIo4,signy)); 18324578Szliu else 18424578Szliu return(copysign((signx==one)?zero:PI,signy)); 18524578Szliu 18624578Szliu /* when y is INF */ 18724578Szliu if(!finite(y)) return(copysign(PIo2,signy)); 18824578Szliu 18924578Szliu /* compute y/x */ 19024578Szliu x=copysign(x,one); 19124578Szliu y=copysign(y,one); 19224578Szliu if((m=(k=logb(y))-logb(x)) > 60) t=big+big; 19324578Szliu else if(m < -80 ) t=y/x; 19424578Szliu else { t = y/x ; y = scalb(y,-k); x=scalb(x,-k); } 19524578Szliu 19624578Szliu /* begin argument reduction */ 19724578Szliu begin: 19824578Szliu if (t < 2.4375) { 19924578Szliu 20024578Szliu /* truncate 4(t+1/16) to integer for branching */ 20124578Szliu k = 4 * (t+0.0625); 20224578Szliu switch (k) { 20324578Szliu 20424578Szliu /* t is in [0,7/16] */ 20524578Szliu case 0: 20624578Szliu case 1: 20724578Szliu if (t < small) 20824578Szliu { big + small ; /* raise inexact flag */ 20924578Szliu return (copysign((signx>zero)?t:PI-t,signy)); } 21024578Szliu 21124578Szliu hi = zero; lo = zero; break; 21224578Szliu 21324578Szliu /* t is in [7/16,11/16] */ 21424578Szliu case 2: 21524578Szliu hi = athfhi; lo = athflo; 21624578Szliu z = x+x; 21724578Szliu t = ( (y+y) - x ) / ( z + y ); break; 21824578Szliu 21924578Szliu /* t is in [11/16,19/16] */ 22024578Szliu case 3: 22124578Szliu case 4: 22224578Szliu hi = PIo4; lo = zero; 22324578Szliu t = ( y - x ) / ( x + y ); break; 22424578Szliu 22524578Szliu /* t is in [19/16,39/16] */ 22624578Szliu default: 22724578Szliu hi = at1fhi; lo = at1flo; 22824578Szliu z = y-x; y=y+y+y; t = x+x; 22924578Szliu t = ( (z+z)-x ) / ( t + y ); break; 23024578Szliu } 23124578Szliu } 23224578Szliu /* end of if (t < 2.4375) */ 23324578Szliu 23424578Szliu else 23524578Szliu { 23624578Szliu hi = PIo2; lo = zero; 23724578Szliu 23824578Szliu /* t is in [2.4375, big] */ 23924578Szliu if (t <= big) t = - x / y; 24024578Szliu 24124578Szliu /* t is in [big, INF] */ 24224578Szliu else 24324578Szliu { big+small; /* raise inexact flag */ 24424578Szliu t = zero; } 24524578Szliu } 24624578Szliu /* end of argument reduction */ 24724578Szliu 24824578Szliu /* compute atan(t) for t in [-.4375, .4375] */ 24924578Szliu z = t*t; 25031855Szliu #if defined(vax)||defined(tahoe) 25124578Szliu z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+ 25224578Szliu z*(a9+z*(a10+z*(a11+z*a12)))))))))))); 25331855Szliu #else /* defined(vax)||defined(tahoe) */ 25424578Szliu z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+ 25524578Szliu z*(a9+z*(a10+z*a11))))))))))); 25631855Szliu #endif /* defined(vax)||defined(tahoe) */ 25724578Szliu z = lo - z; z += t; z += hi; 25824578Szliu 25924578Szliu return(copysign((signx>zero)?z:PI-z,signy)); 26024578Szliu } 261