1*0a6a1f1dSLionel Sambuc /* $NetBSD: n_atan2.c,v 1.7 2014/10/10 20:58:09 martin Exp $ */
22fe8fb19SBen Gras /*
32fe8fb19SBen Gras * Copyright (c) 1985, 1993
42fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
52fe8fb19SBen Gras *
62fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
72fe8fb19SBen Gras * modification, are permitted provided that the following conditions
82fe8fb19SBen Gras * are met:
92fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
102fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
112fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
152fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
162fe8fb19SBen Gras * without specific prior written permission.
172fe8fb19SBen Gras *
182fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
222fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282fe8fb19SBen Gras * SUCH DAMAGE.
292fe8fb19SBen Gras */
302fe8fb19SBen Gras
312fe8fb19SBen Gras #ifndef lint
322fe8fb19SBen Gras static char sccsid[] = "@(#)atan2.c 8.1 (Berkeley) 6/4/93";
332fe8fb19SBen Gras #endif /* not lint */
342fe8fb19SBen Gras
352fe8fb19SBen Gras /* ATAN2(Y,X)
362fe8fb19SBen Gras * RETURN ARG (X+iY)
372fe8fb19SBen Gras * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
382fe8fb19SBen Gras * CODED IN C BY K.C. NG, 1/8/85;
392fe8fb19SBen Gras * REVISED BY K.C. NG on 2/7/85, 2/13/85, 3/7/85, 3/30/85, 6/29/85.
402fe8fb19SBen Gras *
412fe8fb19SBen Gras * Required system supported functions :
422fe8fb19SBen Gras * copysign(x,y)
432fe8fb19SBen Gras * scalb(x,y)
442fe8fb19SBen Gras * logb(x)
452fe8fb19SBen Gras *
462fe8fb19SBen Gras * Method :
472fe8fb19SBen Gras * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
482fe8fb19SBen Gras * 2. Reduce x to positive by (if x and y are unexceptional):
492fe8fb19SBen Gras * ARG (x+iy) = arctan(y/x) ... if x > 0,
502fe8fb19SBen Gras * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0,
512fe8fb19SBen Gras * 3. According to the integer k=4t+0.25 truncated , t=y/x, the argument
522fe8fb19SBen Gras * is further reduced to one of the following intervals and the
532fe8fb19SBen Gras * arctangent of y/x is evaluated by the corresponding formula:
542fe8fb19SBen Gras *
552fe8fb19SBen Gras * [0,7/16] atan(y/x) = t - t^3*(a1+t^2*(a2+...(a10+t^2*a11)...)
562fe8fb19SBen Gras * [7/16,11/16] atan(y/x) = atan(1/2) + atan( (y-x/2)/(x+y/2) )
572fe8fb19SBen Gras * [11/16.19/16] atan(y/x) = atan( 1 ) + atan( (y-x)/(x+y) )
582fe8fb19SBen Gras * [19/16,39/16] atan(y/x) = atan(3/2) + atan( (y-1.5x)/(x+1.5y) )
592fe8fb19SBen Gras * [39/16,INF] atan(y/x) = atan(INF) + atan( -x/y )
602fe8fb19SBen Gras *
612fe8fb19SBen Gras * Special cases:
622fe8fb19SBen Gras * Notations: atan2(y,x) == ARG (x+iy) == ARG(x,y).
632fe8fb19SBen Gras *
642fe8fb19SBen Gras * ARG( NAN , (anything) ) is NaN;
652fe8fb19SBen Gras * ARG( (anything), NaN ) is NaN;
662fe8fb19SBen Gras * ARG(+(anything but NaN), +-0) is +-0 ;
672fe8fb19SBen Gras * ARG(-(anything but NaN), +-0) is +-PI ;
682fe8fb19SBen Gras * ARG( 0, +-(anything but 0 and NaN) ) is +-PI/2;
692fe8fb19SBen Gras * ARG( +INF,+-(anything but INF and NaN) ) is +-0 ;
702fe8fb19SBen Gras * ARG( -INF,+-(anything but INF and NaN) ) is +-PI;
712fe8fb19SBen Gras * ARG( +INF,+-INF ) is +-PI/4 ;
722fe8fb19SBen Gras * ARG( -INF,+-INF ) is +-3PI/4;
732fe8fb19SBen Gras * ARG( (anything but,0,NaN, and INF),+-INF ) is +-PI/2;
742fe8fb19SBen Gras *
752fe8fb19SBen Gras * Accuracy:
762fe8fb19SBen Gras * atan2(y,x) returns (PI/pi) * the exact ARG (x+iy) nearly rounded,
772fe8fb19SBen Gras * where
782fe8fb19SBen Gras *
792fe8fb19SBen Gras * in decimal:
802fe8fb19SBen Gras * pi = 3.141592653589793 23846264338327 .....
812fe8fb19SBen Gras * 53 bits PI = 3.141592653589793 115997963 ..... ,
822fe8fb19SBen Gras * 56 bits PI = 3.141592653589793 227020265 ..... ,
832fe8fb19SBen Gras *
842fe8fb19SBen Gras * in hexadecimal:
852fe8fb19SBen Gras * pi = 3.243F6A8885A308D313198A2E....
862fe8fb19SBen Gras * 53 bits PI = 3.243F6A8885A30 = 2 * 1.921FB54442D18 error=.276ulps
872fe8fb19SBen Gras * 56 bits PI = 3.243F6A8885A308 = 4 * .C90FDAA22168C2 error=.206ulps
882fe8fb19SBen Gras *
892fe8fb19SBen Gras * In a test run with 356,000 random argument on [-1,1] * [-1,1] on a
902fe8fb19SBen Gras * VAX, the maximum observed error was 1.41 ulps (units of the last place)
912fe8fb19SBen Gras * compared with (PI/pi)*(the exact ARG(x+iy)).
922fe8fb19SBen Gras *
932fe8fb19SBen Gras * Note:
942fe8fb19SBen Gras * We use machine PI (the true pi rounded) in place of the actual
952fe8fb19SBen Gras * value of pi for all the trig and inverse trig functions. In general,
962fe8fb19SBen Gras * if trig is one of sin, cos, tan, then computed trig(y) returns the
972fe8fb19SBen Gras * exact trig(y*pi/PI) nearly rounded; correspondingly, computed arctrig
982fe8fb19SBen Gras * returns the exact arctrig(y)*PI/pi nearly rounded. These guarantee the
992fe8fb19SBen Gras * trig functions have period PI, and trig(arctrig(x)) returns x for
1002fe8fb19SBen Gras * all critical values x.
1012fe8fb19SBen Gras *
1022fe8fb19SBen Gras * Constants:
1032fe8fb19SBen Gras * The hexadecimal values are the intended ones for the following constants.
1042fe8fb19SBen Gras * The decimal values may be used, provided that the compiler will convert
1052fe8fb19SBen Gras * from decimal to binary accurately enough to produce the hexadecimal values
1062fe8fb19SBen Gras * shown.
1072fe8fb19SBen Gras */
1082fe8fb19SBen Gras
1092fe8fb19SBen Gras #define _LIBM_STATIC
1102fe8fb19SBen Gras #include "mathimpl.h"
1112fe8fb19SBen Gras
1122fe8fb19SBen Gras vc(athfhi, 4.6364760900080611433E-1 ,6338,3fed,da7b,2b0d, -1, .ED63382B0DDA7B)
1132fe8fb19SBen Gras vc(athflo, 1.9338828231967579916E-19 ,5005,2164,92c0,9cfe, -62, .E450059CFE92C0)
1142fe8fb19SBen Gras vc(PIo4, 7.8539816339744830676E-1 ,0fda,4049,68c2,a221, 0, .C90FDAA22168C2)
1152fe8fb19SBen Gras vc(at1fhi, 9.8279372324732906796E-1 ,985e,407b,b4d9,940f, 0, .FB985E940FB4D9)
1162fe8fb19SBen Gras vc(at1flo,-3.5540295636764633916E-18 ,1edc,a383,eaea,34d6, -57,-.831EDC34D6EAEA)
1172fe8fb19SBen Gras vc(PIo2, 1.5707963267948966135E0 ,0fda,40c9,68c2,a221, 1, .C90FDAA22168C2)
1182fe8fb19SBen Gras vc(PI, 3.1415926535897932270E0 ,0fda,4149,68c2,a221, 2, .C90FDAA22168C2)
1192fe8fb19SBen Gras vc(a1, 3.3333333333333473730E-1 ,aaaa,3faa,ab75,aaaa, -1, .AAAAAAAAAAAB75)
1202fe8fb19SBen Gras vc(a2, -2.0000000000017730678E-1 ,cccc,bf4c,946e,cccd, -2,-.CCCCCCCCCD946E)
1212fe8fb19SBen Gras vc(a3, 1.4285714286694640301E-1 ,4924,3f12,4262,9274, -2, .92492492744262)
1222fe8fb19SBen Gras vc(a4, -1.1111111135032672795E-1 ,8e38,bee3,6292,ebc6, -3,-.E38E38EBC66292)
1232fe8fb19SBen Gras vc(a5, 9.0909091380563043783E-2 ,2e8b,3eba,d70c,b31b, -3, .BA2E8BB31BD70C)
1242fe8fb19SBen Gras vc(a6, -7.6922954286089459397E-2 ,89c8,be9d,7f18,27c3, -3,-.9D89C827C37F18)
1252fe8fb19SBen Gras vc(a7, 6.6663180891693915586E-2 ,86b4,3e88,9e58,ae37, -3, .8886B4AE379E58)
1262fe8fb19SBen Gras vc(a8, -5.8772703698290408927E-2 ,bba5,be70,a942,8481, -4,-.F0BBA58481A942)
1272fe8fb19SBen Gras vc(a9, 5.2170707402812969804E-2 ,b0f3,3e55,13ab,a1ab, -4, .D5B0F3A1AB13AB)
1282fe8fb19SBen Gras vc(a10, -4.4895863157820361210E-2 ,e4b9,be37,048f,7fd1, -4,-.B7E4B97FD1048F)
1292fe8fb19SBen Gras vc(a11, 3.3006147437343875094E-2 ,3174,3e07,2d87,3cf7, -4, .8731743CF72D87)
1302fe8fb19SBen Gras vc(a12, -1.4614844866464185439E-2 ,731a,bd6f,76d9,2f34, -6,-.EF731A2F3476D9)
1312fe8fb19SBen Gras
1322fe8fb19SBen Gras ic(athfhi, 4.6364760900080609352E-1 , -2, 1.DAC670561BB4F)
1332fe8fb19SBen Gras ic(athflo, 4.6249969567426939759E-18 , -58, 1.5543B8F253271)
1342fe8fb19SBen Gras ic(PIo4, 7.8539816339744827900E-1 , -1, 1.921FB54442D18)
1352fe8fb19SBen Gras ic(at1fhi, 9.8279372324732905408E-1 , -1, 1.F730BD281F69B)
1362fe8fb19SBen Gras ic(at1flo,-2.4407677060164810007E-17 , -56, -1.C23DFEFEAE6B5)
1372fe8fb19SBen Gras ic(PIo2, 1.5707963267948965580E0 , 0, 1.921FB54442D18)
1382fe8fb19SBen Gras ic(PI, 3.1415926535897931160E0 , 1, 1.921FB54442D18)
1392fe8fb19SBen Gras ic(a1, 3.3333333333333942106E-1 , -2, 1.55555555555C3)
1402fe8fb19SBen Gras ic(a2, -1.9999999999979536924E-1 , -3, -1.9999999997CCD)
1412fe8fb19SBen Gras ic(a3, 1.4285714278004377209E-1 , -3, 1.24924921EC1D7)
1422fe8fb19SBen Gras ic(a4, -1.1111110579344973814E-1 , -4, -1.C71C7059AF280)
1432fe8fb19SBen Gras ic(a5, 9.0908906105474668324E-2 , -4, 1.745CE5AA35DB2)
1442fe8fb19SBen Gras ic(a6, -7.6919217767468239799E-2 , -4, -1.3B0FA54BEC400)
1452fe8fb19SBen Gras ic(a7, 6.6614695906082474486E-2 , -4, 1.10DA924597FFF)
1462fe8fb19SBen Gras ic(a8, -5.8358371008508623523E-2 , -5, -1.DE125FDDBD793)
1472fe8fb19SBen Gras ic(a9, 4.9850617156082015213E-2 , -5, 1.9860524BDD807)
1482fe8fb19SBen Gras ic(a10, -3.6700606902093604877E-2 , -5, -1.2CA6C04C6937A)
1492fe8fb19SBen Gras ic(a11, 1.6438029044759730479E-2 , -6, 1.0D52174A1BB54)
1502fe8fb19SBen Gras
1512fe8fb19SBen Gras #ifdef vccast
1522fe8fb19SBen Gras #define athfhi vccast(athfhi)
1532fe8fb19SBen Gras #define athflo vccast(athflo)
1542fe8fb19SBen Gras #define PIo4 vccast(PIo4)
1552fe8fb19SBen Gras #define at1fhi vccast(at1fhi)
1562fe8fb19SBen Gras #define at1flo vccast(at1flo)
1572fe8fb19SBen Gras #define PIo2 vccast(PIo2)
1582fe8fb19SBen Gras #define PI vccast(PI)
1592fe8fb19SBen Gras #define a1 vccast(a1)
1602fe8fb19SBen Gras #define a2 vccast(a2)
1612fe8fb19SBen Gras #define a3 vccast(a3)
1622fe8fb19SBen Gras #define a4 vccast(a4)
1632fe8fb19SBen Gras #define a5 vccast(a5)
1642fe8fb19SBen Gras #define a6 vccast(a6)
1652fe8fb19SBen Gras #define a7 vccast(a7)
1662fe8fb19SBen Gras #define a8 vccast(a8)
1672fe8fb19SBen Gras #define a9 vccast(a9)
1682fe8fb19SBen Gras #define a10 vccast(a10)
1692fe8fb19SBen Gras #define a11 vccast(a11)
1702fe8fb19SBen Gras #define a12 vccast(a12)
1712fe8fb19SBen Gras #endif
1722fe8fb19SBen Gras
173*0a6a1f1dSLionel Sambuc #ifdef __weak_alias
174*0a6a1f1dSLionel Sambuc __weak_alias(_atan2l, atan2);
175*0a6a1f1dSLionel Sambuc #endif
176*0a6a1f1dSLionel Sambuc
1772fe8fb19SBen Gras double
atan2(double y,double x)1782fe8fb19SBen Gras atan2(double y, double x)
1792fe8fb19SBen Gras {
1802fe8fb19SBen Gras static const double zero=0, one=1, small=1.0E-9, big=1.0E18;
1812fe8fb19SBen Gras double t,z,signy,signx,hi,lo;
1822fe8fb19SBen Gras int k,m;
1832fe8fb19SBen Gras
1842fe8fb19SBen Gras #if !defined(__vax__)&&!defined(tahoe)
1852fe8fb19SBen Gras /* if x or y is NAN */
1862fe8fb19SBen Gras if(x!=x) return(x); if(y!=y) return(y);
1872fe8fb19SBen Gras #endif /* !defined(__vax__)&&!defined(tahoe) */
1882fe8fb19SBen Gras
1892fe8fb19SBen Gras /* copy down the sign of y and x */
1902fe8fb19SBen Gras signy = copysign(one,y) ;
1912fe8fb19SBen Gras signx = copysign(one,x) ;
1922fe8fb19SBen Gras
1932fe8fb19SBen Gras /* if x is 1.0, goto begin */
1942fe8fb19SBen Gras if(x==1) { y=copysign(y,one); t=y; if(finite(t)) goto begin;}
1952fe8fb19SBen Gras
1962fe8fb19SBen Gras /* when y = 0 */
1972fe8fb19SBen Gras if(y==zero) return((signx==one)?y:copysign(PI,signy));
1982fe8fb19SBen Gras
1992fe8fb19SBen Gras /* when x = 0 */
2002fe8fb19SBen Gras if(x==zero) return(copysign(PIo2,signy));
2012fe8fb19SBen Gras
2022fe8fb19SBen Gras /* when x is INF */
2032fe8fb19SBen Gras if(!finite(x))
2042fe8fb19SBen Gras if(!finite(y))
2052fe8fb19SBen Gras return(copysign((signx==one)?PIo4:3*PIo4,signy));
2062fe8fb19SBen Gras else
2072fe8fb19SBen Gras return(copysign((signx==one)?zero:PI,signy));
2082fe8fb19SBen Gras
2092fe8fb19SBen Gras /* when y is INF */
2102fe8fb19SBen Gras if(!finite(y)) return(copysign(PIo2,signy));
2112fe8fb19SBen Gras
2122fe8fb19SBen Gras /* compute y/x */
2132fe8fb19SBen Gras x=copysign(x,one);
2142fe8fb19SBen Gras y=copysign(y,one);
2152fe8fb19SBen Gras if((m=(k=logb(y))-logb(x)) > 60) t=big+big;
2162fe8fb19SBen Gras else if(m < -80 ) t=y/x;
2172fe8fb19SBen Gras else { t = y/x ; y = scalb(y,-k); x=scalb(x,-k); }
2182fe8fb19SBen Gras
2192fe8fb19SBen Gras /* begin argument reduction */
2202fe8fb19SBen Gras begin:
2212fe8fb19SBen Gras if (t < 2.4375) {
2222fe8fb19SBen Gras
2232fe8fb19SBen Gras /* truncate 4(t+1/16) to integer for branching */
2242fe8fb19SBen Gras k = 4 * (t+0.0625);
2252fe8fb19SBen Gras switch (k) {
2262fe8fb19SBen Gras
2272fe8fb19SBen Gras /* t is in [0,7/16] */
2282fe8fb19SBen Gras case 0:
2292fe8fb19SBen Gras case 1:
2302fe8fb19SBen Gras if (t < small)
2312fe8fb19SBen Gras { big + small ; /* raise inexact flag */
2322fe8fb19SBen Gras return (copysign((signx>zero)?t:PI-t,signy)); }
2332fe8fb19SBen Gras
2342fe8fb19SBen Gras hi = zero; lo = zero; break;
2352fe8fb19SBen Gras
2362fe8fb19SBen Gras /* t is in [7/16,11/16] */
2372fe8fb19SBen Gras case 2:
2382fe8fb19SBen Gras hi = athfhi; lo = athflo;
2392fe8fb19SBen Gras z = x+x;
2402fe8fb19SBen Gras t = ( (y+y) - x ) / ( z + y ); break;
2412fe8fb19SBen Gras
2422fe8fb19SBen Gras /* t is in [11/16,19/16] */
2432fe8fb19SBen Gras case 3:
2442fe8fb19SBen Gras case 4:
2452fe8fb19SBen Gras hi = PIo4; lo = zero;
2462fe8fb19SBen Gras t = ( y - x ) / ( x + y ); break;
2472fe8fb19SBen Gras
2482fe8fb19SBen Gras /* t is in [19/16,39/16] */
2492fe8fb19SBen Gras default:
2502fe8fb19SBen Gras hi = at1fhi; lo = at1flo;
2512fe8fb19SBen Gras z = y-x; y=y+y+y; t = x+x;
2522fe8fb19SBen Gras t = ( (z+z)-x ) / ( t + y ); break;
2532fe8fb19SBen Gras }
2542fe8fb19SBen Gras }
2552fe8fb19SBen Gras /* end of if (t < 2.4375) */
2562fe8fb19SBen Gras
2572fe8fb19SBen Gras else
2582fe8fb19SBen Gras {
2592fe8fb19SBen Gras hi = PIo2; lo = zero;
2602fe8fb19SBen Gras
2612fe8fb19SBen Gras /* t is in [2.4375, big] */
2622fe8fb19SBen Gras if (t <= big) t = - x / y;
2632fe8fb19SBen Gras
2642fe8fb19SBen Gras /* t is in [big, INF] */
2652fe8fb19SBen Gras else
2662fe8fb19SBen Gras { big+small; /* raise inexact flag */
2672fe8fb19SBen Gras t = zero; }
2682fe8fb19SBen Gras }
2692fe8fb19SBen Gras /* end of argument reduction */
2702fe8fb19SBen Gras
2712fe8fb19SBen Gras /* compute atan(t) for t in [-.4375, .4375] */
2722fe8fb19SBen Gras z = t*t;
2732fe8fb19SBen Gras #if defined(__vax__)||defined(tahoe)
2742fe8fb19SBen Gras z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+
2752fe8fb19SBen Gras z*(a9+z*(a10+z*(a11+z*a12))))))))))));
2762fe8fb19SBen Gras #else /* defined(__vax__)||defined(tahoe) */
2772fe8fb19SBen Gras z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+
2782fe8fb19SBen Gras z*(a9+z*(a10+z*a11)))))))))));
2792fe8fb19SBen Gras #endif /* defined(__vax__)||defined(tahoe) */
2802fe8fb19SBen Gras z = lo - z; z += t; z += hi;
2812fe8fb19SBen Gras
2822fe8fb19SBen Gras return(copysign((signx>zero)?z:PI-z,signy));
2832fe8fb19SBen Gras }
284