124578Szliu /* 224578Szliu * Copyright (c) 1985 Regents of the University of California. 324578Szliu * 424578Szliu * Use and reproduction of this software are granted in accordance with 524578Szliu * the terms and conditions specified in the Berkeley Software License 624578Szliu * Agreement (in particular, this entails acknowledgement of the programs' 724578Szliu * source, and inclusion of this notice) with the additional understanding 824578Szliu * that all recipients should regard themselves as participants in an 924578Szliu * ongoing research project and hence should feel obligated to report 1024578Szliu * their experiences (good or bad) with these elementary function codes, 1124578Szliu * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 1224578Szliu */ 1324578Szliu 1424578Szliu #ifndef lint 1524718Selefunt static char sccsid[] = 16*31855Szliu "@(#)atan2.c 1.3 (Berkeley) 8/21/85; 1.6 (ucb.elefunt) 07/13/87"; 17*31855Szliu #endif /* not lint */ 1824578Szliu 1924578Szliu /* ATAN2(Y,X) 2024578Szliu * RETURN ARG (X+iY) 2124578Szliu * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS) 2224578Szliu * CODED IN C BY K.C. NG, 1/8/85; 2324578Szliu * REVISED BY K.C. NG on 2/7/85, 2/13/85, 3/7/85, 3/30/85, 6/29/85. 2424578Szliu * 2524578Szliu * Required system supported functions : 2624578Szliu * copysign(x,y) 2724578Szliu * scalb(x,y) 2824578Szliu * logb(x) 2924578Szliu * 3024578Szliu * Method : 3124578Szliu * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x). 3224578Szliu * 2. Reduce x to positive by (if x and y are unexceptional): 3324578Szliu * ARG (x+iy) = arctan(y/x) ... if x > 0, 3424578Szliu * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0, 3524578Szliu * 3. According to the integer k=4t+0.25 truncated , t=y/x, the argument 3624578Szliu * is further reduced to one of the following intervals and the 3724578Szliu * arctangent of y/x is evaluated by the corresponding formula: 3824578Szliu * 3924578Szliu * [0,7/16] atan(y/x) = t - t^3*(a1+t^2*(a2+...(a10+t^2*a11)...) 4024578Szliu * [7/16,11/16] atan(y/x) = atan(1/2) + atan( (y-x/2)/(x+y/2) ) 4124578Szliu * [11/16.19/16] atan(y/x) = atan( 1 ) + atan( (y-x)/(x+y) ) 4224578Szliu * [19/16,39/16] atan(y/x) = atan(3/2) + atan( (y-1.5x)/(x+1.5y) ) 4324578Szliu * [39/16,INF] atan(y/x) = atan(INF) + atan( -x/y ) 4424578Szliu * 4524578Szliu * Special cases: 4624578Szliu * Notations: atan2(y,x) == ARG (x+iy) == ARG(x,y). 4724578Szliu * 4824578Szliu * ARG( NAN , (anything) ) is NaN; 4924578Szliu * ARG( (anything), NaN ) is NaN; 5024578Szliu * ARG(+(anything but NaN), +-0) is +-0 ; 5124578Szliu * ARG(-(anything but NaN), +-0) is +-PI ; 5224578Szliu * ARG( 0, +-(anything but 0 and NaN) ) is +-PI/2; 5324578Szliu * ARG( +INF,+-(anything but INF and NaN) ) is +-0 ; 5424578Szliu * ARG( -INF,+-(anything but INF and NaN) ) is +-PI; 5524578Szliu * ARG( +INF,+-INF ) is +-PI/4 ; 5624578Szliu * ARG( -INF,+-INF ) is +-3PI/4; 5724578Szliu * ARG( (anything but,0,NaN, and INF),+-INF ) is +-PI/2; 5824578Szliu * 5924578Szliu * Accuracy: 6024578Szliu * atan2(y,x) returns (PI/pi) * the exact ARG (x+iy) nearly rounded, 6124578Szliu * where 6224578Szliu * 6324578Szliu * in decimal: 6424578Szliu * pi = 3.141592653589793 23846264338327 ..... 6524578Szliu * 53 bits PI = 3.141592653589793 115997963 ..... , 6624578Szliu * 56 bits PI = 3.141592653589793 227020265 ..... , 6724578Szliu * 6824578Szliu * in hexadecimal: 6924578Szliu * pi = 3.243F6A8885A308D313198A2E.... 7024578Szliu * 53 bits PI = 3.243F6A8885A30 = 2 * 1.921FB54442D18 error=.276ulps 7124578Szliu * 56 bits PI = 3.243F6A8885A308 = 4 * .C90FDAA22168C2 error=.206ulps 7224578Szliu * 7324578Szliu * In a test run with 356,000 random argument on [-1,1] * [-1,1] on a 7424578Szliu * VAX, the maximum observed error was 1.41 ulps (units of the last place) 7524578Szliu * compared with (PI/pi)*(the exact ARG(x+iy)). 7624578Szliu * 7724578Szliu * Note: 7824578Szliu * We use machine PI (the true pi rounded) in place of the actual 7924578Szliu * value of pi for all the trig and inverse trig functions. In general, 8024578Szliu * if trig is one of sin, cos, tan, then computed trig(y) returns the 8124578Szliu * exact trig(y*pi/PI) nearly rounded; correspondingly, computed arctrig 8224578Szliu * returns the exact arctrig(y)*PI/pi nearly rounded. These guarantee the 8324578Szliu * trig functions have period PI, and trig(arctrig(x)) returns x for 8424578Szliu * all critical values x. 8524578Szliu * 8624578Szliu * Constants: 8724578Szliu * The hexadecimal values are the intended ones for the following constants. 8824578Szliu * The decimal values may be used, provided that the compiler will convert 8924578Szliu * from decimal to binary accurately enough to produce the hexadecimal values 9024578Szliu * shown. 9124578Szliu */ 9224578Szliu 93*31855Szliu #if defined(vax)||defined(tahoe) /* VAX D format */ 94*31855Szliu #ifdef vax 9531822Szliu #define _0x(A,B) 0x/**/A/**/B 96*31855Szliu #else /* vax */ 9731822Szliu #define _0x(A,B) 0x/**/B/**/A 98*31855Szliu #endif /* vax */ 9931822Szliu /*static double */ 10031822Szliu /*athfhi = 4.6364760900080611433E-1 , /*Hex 2^ -1 * .ED63382B0DDA7B */ 10131822Szliu /*athflo = 1.9338828231967579916E-19 , /*Hex 2^-62 * .E450059CFE92C0 */ 10231822Szliu /*PIo4 = 7.8539816339744830676E-1 , /*Hex 2^ 0 * .C90FDAA22168C2 */ 10331822Szliu /*at1fhi = 9.8279372324732906796E-1 , /*Hex 2^ 0 * .FB985E940FB4D9 */ 10431822Szliu /*at1flo = -3.5540295636764633916E-18 , /*Hex 2^-57 * -.831EDC34D6EAEA */ 10531822Szliu /*PIo2 = 1.5707963267948966135E0 , /*Hex 2^ 1 * .C90FDAA22168C2 */ 10631822Szliu /*PI = 3.1415926535897932270E0 , /*Hex 2^ 2 * .C90FDAA22168C2 */ 10731822Szliu /*a1 = 3.3333333333333473730E-1 , /*Hex 2^ -1 * .AAAAAAAAAAAB75 */ 10831822Szliu /*a2 = -2.0000000000017730678E-1 , /*Hex 2^ -2 * -.CCCCCCCCCD946E */ 10931822Szliu /*a3 = 1.4285714286694640301E-1 , /*Hex 2^ -2 * .92492492744262 */ 11031822Szliu /*a4 = -1.1111111135032672795E-1 , /*Hex 2^ -3 * -.E38E38EBC66292 */ 11131822Szliu /*a5 = 9.0909091380563043783E-2 , /*Hex 2^ -3 * .BA2E8BB31BD70C */ 11231822Szliu /*a6 = -7.6922954286089459397E-2 , /*Hex 2^ -3 * -.9D89C827C37F18 */ 11331822Szliu /*a7 = 6.6663180891693915586E-2 , /*Hex 2^ -3 * .8886B4AE379E58 */ 11431822Szliu /*a8 = -5.8772703698290408927E-2 , /*Hex 2^ -4 * -.F0BBA58481A942 */ 11531822Szliu /*a9 = 5.2170707402812969804E-2 , /*Hex 2^ -4 * .D5B0F3A1AB13AB */ 11631822Szliu /*a10 = -4.4895863157820361210E-2 , /*Hex 2^ -4 * -.B7E4B97FD1048F */ 11731822Szliu /*a11 = 3.3006147437343875094E-2 , /*Hex 2^ -4 * .8731743CF72D87 */ 11831822Szliu /*a12 = -1.4614844866464185439E-2 ; /*Hex 2^ -6 * -.EF731A2F3476D9 */ 11931822Szliu static long athfhix[] = { _0x(6338,3fed), _0x(da7b,2b0d)}; 12031822Szliu #define athfhi (*(double *)athfhix) 12131822Szliu static long athflox[] = { _0x(5005,2164), _0x(92c0,9cfe)}; 12231822Szliu #define athflo (*(double *)athflox) 12331822Szliu static long PIo4x[] = { _0x(0fda,4049), _0x(68c2,a221)}; 12431822Szliu #define PIo4 (*(double *)PIo4x) 12531822Szliu static long at1fhix[] = { _0x(985e,407b), _0x(b4d9,940f)}; 12631822Szliu #define at1fhi (*(double *)at1fhix) 12731822Szliu static long at1flox[] = { _0x(1edc,a383), _0x(eaea,34d6)}; 12831822Szliu #define at1flo (*(double *)at1flox) 12931822Szliu static long PIo2x[] = { _0x(0fda,40c9), _0x(68c2,a221)}; 13031822Szliu #define PIo2 (*(double *)PIo2x) 13131822Szliu static long PIx[] = { _0x(0fda,4149), _0x(68c2,a221)}; 13231822Szliu #define PI (*(double *)PIx) 13331822Szliu static long a1x[] = { _0x(aaaa,3faa), _0x(ab75,aaaa)}; 13431822Szliu #define a1 (*(double *)a1x) 13531822Szliu static long a2x[] = { _0x(cccc,bf4c), _0x(946e,cccd)}; 13631822Szliu #define a2 (*(double *)a2x) 13731822Szliu static long a3x[] = { _0x(4924,3f12), _0x(4262,9274)}; 13831822Szliu #define a3 (*(double *)a3x) 13931822Szliu static long a4x[] = { _0x(8e38,bee3), _0x(6292,ebc6)}; 14031822Szliu #define a4 (*(double *)a4x) 14131822Szliu static long a5x[] = { _0x(2e8b,3eba), _0x(d70c,b31b)}; 14231822Szliu #define a5 (*(double *)a5x) 14331822Szliu static long a6x[] = { _0x(89c8,be9d), _0x(7f18,27c3)}; 14431822Szliu #define a6 (*(double *)a6x) 14531822Szliu static long a7x[] = { _0x(86b4,3e88), _0x(9e58,ae37)}; 14631822Szliu #define a7 (*(double *)a7x) 14731822Szliu static long a8x[] = { _0x(bba5,be70), _0x(a942,8481)}; 14831822Szliu #define a8 (*(double *)a8x) 14931822Szliu static long a9x[] = { _0x(b0f3,3e55), _0x(13ab,a1ab)}; 15031822Szliu #define a9 (*(double *)a9x) 15131822Szliu static long a10x[] = { _0x(e4b9,be37), _0x(048f,7fd1)}; 15231822Szliu #define a10 (*(double *)a10x) 15331822Szliu static long a11x[] = { _0x(3174,3e07), _0x(2d87,3cf7)}; 15431822Szliu #define a11 (*(double *)a11x) 15531822Szliu static long a12x[] = { _0x(731a,bd6f), _0x(76d9,2f34)}; 15631822Szliu #define a12 (*(double *)a12x) 157*31855Szliu #else /* defined(vax)||defined(tahoe) */ 15831822Szliu static double 15924578Szliu athfhi = 4.6364760900080609352E-1 , /*Hex 2^ -2 * 1.DAC670561BB4F */ 16024578Szliu athflo = 4.6249969567426939759E-18 , /*Hex 2^-58 * 1.5543B8F253271 */ 16124578Szliu PIo4 = 7.8539816339744827900E-1 , /*Hex 2^ -1 * 1.921FB54442D18 */ 16224578Szliu at1fhi = 9.8279372324732905408E-1 , /*Hex 2^ -1 * 1.F730BD281F69B */ 16324578Szliu at1flo = -2.4407677060164810007E-17 , /*Hex 2^-56 * -1.C23DFEFEAE6B5 */ 16424578Szliu PIo2 = 1.5707963267948965580E0 , /*Hex 2^ 0 * 1.921FB54442D18 */ 16524578Szliu PI = 3.1415926535897931160E0 , /*Hex 2^ 1 * 1.921FB54442D18 */ 16624578Szliu a1 = 3.3333333333333942106E-1 , /*Hex 2^ -2 * 1.55555555555C3 */ 16724578Szliu a2 = -1.9999999999979536924E-1 , /*Hex 2^ -3 * -1.9999999997CCD */ 16824578Szliu a3 = 1.4285714278004377209E-1 , /*Hex 2^ -3 * 1.24924921EC1D7 */ 16924578Szliu a4 = -1.1111110579344973814E-1 , /*Hex 2^ -4 * -1.C71C7059AF280 */ 17024578Szliu a5 = 9.0908906105474668324E-2 , /*Hex 2^ -4 * 1.745CE5AA35DB2 */ 17124578Szliu a6 = -7.6919217767468239799E-2 , /*Hex 2^ -4 * -1.3B0FA54BEC400 */ 17224578Szliu a7 = 6.6614695906082474486E-2 , /*Hex 2^ -4 * 1.10DA924597FFF */ 17324578Szliu a8 = -5.8358371008508623523E-2 , /*Hex 2^ -5 * -1.DE125FDDBD793 */ 17424578Szliu a9 = 4.9850617156082015213E-2 , /*Hex 2^ -5 * 1.9860524BDD807 */ 17524578Szliu a10 = -3.6700606902093604877E-2 , /*Hex 2^ -5 * -1.2CA6C04C6937A */ 17624578Szliu a11 = 1.6438029044759730479E-2 ; /*Hex 2^ -6 * 1.0D52174A1BB54 */ 177*31855Szliu #endif /* defined(vax)||defined(tahoe) */ 17824578Szliu 17924578Szliu double atan2(y,x) 18024578Szliu double y,x; 18124578Szliu { 18224578Szliu static double zero=0, one=1, small=1.0E-9, big=1.0E18; 18324578Szliu double copysign(),logb(),scalb(),t,z,signy,signx,hi,lo; 18424578Szliu int finite(), k,m; 18524578Szliu 186*31855Szliu #if !defined(vax)&&!defined(tahoe) 18724578Szliu /* if x or y is NAN */ 18824578Szliu if(x!=x) return(x); if(y!=y) return(y); 189*31855Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 19024578Szliu 19124578Szliu /* copy down the sign of y and x */ 19224578Szliu signy = copysign(one,y) ; 19324578Szliu signx = copysign(one,x) ; 19424578Szliu 19524578Szliu /* if x is 1.0, goto begin */ 19624578Szliu if(x==1) { y=copysign(y,one); t=y; if(finite(t)) goto begin;} 19724578Szliu 19824578Szliu /* when y = 0 */ 19924578Szliu if(y==zero) return((signx==one)?y:copysign(PI,signy)); 20024578Szliu 20124578Szliu /* when x = 0 */ 20224578Szliu if(x==zero) return(copysign(PIo2,signy)); 20324578Szliu 20424578Szliu /* when x is INF */ 20524578Szliu if(!finite(x)) 20624578Szliu if(!finite(y)) 20724578Szliu return(copysign((signx==one)?PIo4:3*PIo4,signy)); 20824578Szliu else 20924578Szliu return(copysign((signx==one)?zero:PI,signy)); 21024578Szliu 21124578Szliu /* when y is INF */ 21224578Szliu if(!finite(y)) return(copysign(PIo2,signy)); 21324578Szliu 21424578Szliu /* compute y/x */ 21524578Szliu x=copysign(x,one); 21624578Szliu y=copysign(y,one); 21724578Szliu if((m=(k=logb(y))-logb(x)) > 60) t=big+big; 21824578Szliu else if(m < -80 ) t=y/x; 21924578Szliu else { t = y/x ; y = scalb(y,-k); x=scalb(x,-k); } 22024578Szliu 22124578Szliu /* begin argument reduction */ 22224578Szliu begin: 22324578Szliu if (t < 2.4375) { 22424578Szliu 22524578Szliu /* truncate 4(t+1/16) to integer for branching */ 22624578Szliu k = 4 * (t+0.0625); 22724578Szliu switch (k) { 22824578Szliu 22924578Szliu /* t is in [0,7/16] */ 23024578Szliu case 0: 23124578Szliu case 1: 23224578Szliu if (t < small) 23324578Szliu { big + small ; /* raise inexact flag */ 23424578Szliu return (copysign((signx>zero)?t:PI-t,signy)); } 23524578Szliu 23624578Szliu hi = zero; lo = zero; break; 23724578Szliu 23824578Szliu /* t is in [7/16,11/16] */ 23924578Szliu case 2: 24024578Szliu hi = athfhi; lo = athflo; 24124578Szliu z = x+x; 24224578Szliu t = ( (y+y) - x ) / ( z + y ); break; 24324578Szliu 24424578Szliu /* t is in [11/16,19/16] */ 24524578Szliu case 3: 24624578Szliu case 4: 24724578Szliu hi = PIo4; lo = zero; 24824578Szliu t = ( y - x ) / ( x + y ); break; 24924578Szliu 25024578Szliu /* t is in [19/16,39/16] */ 25124578Szliu default: 25224578Szliu hi = at1fhi; lo = at1flo; 25324578Szliu z = y-x; y=y+y+y; t = x+x; 25424578Szliu t = ( (z+z)-x ) / ( t + y ); break; 25524578Szliu } 25624578Szliu } 25724578Szliu /* end of if (t < 2.4375) */ 25824578Szliu 25924578Szliu else 26024578Szliu { 26124578Szliu hi = PIo2; lo = zero; 26224578Szliu 26324578Szliu /* t is in [2.4375, big] */ 26424578Szliu if (t <= big) t = - x / y; 26524578Szliu 26624578Szliu /* t is in [big, INF] */ 26724578Szliu else 26824578Szliu { big+small; /* raise inexact flag */ 26924578Szliu t = zero; } 27024578Szliu } 27124578Szliu /* end of argument reduction */ 27224578Szliu 27324578Szliu /* compute atan(t) for t in [-.4375, .4375] */ 27424578Szliu z = t*t; 275*31855Szliu #if defined(vax)||defined(tahoe) 27624578Szliu z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+ 27724578Szliu z*(a9+z*(a10+z*(a11+z*a12)))))))))))); 278*31855Szliu #else /* defined(vax)||defined(tahoe) */ 27924578Szliu z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+ 28024578Szliu z*(a9+z*(a10+z*a11))))))))))); 281*31855Szliu #endif /* defined(vax)||defined(tahoe) */ 28224578Szliu z = lo - z; z += t; z += hi; 28324578Szliu 28424578Szliu return(copysign((signx>zero)?z:PI-z,signy)); 28524578Szliu } 286