xref: /csrg-svn/lib/libm/common/atan2.c (revision 31789)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  *
4  * Use and reproduction of this software are granted  in  accordance  with
5  * the terms and conditions specified in  the  Berkeley  Software  License
6  * Agreement (in particular, this entails acknowledgement of the programs'
7  * source, and inclusion of this notice) with the additional understanding
8  * that  all  recipients  should regard themselves as participants  in  an
9  * ongoing  research  project and hence should  feel  obligated  to report
10  * their  experiences (good or bad) with these elementary function  codes,
11  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
12  */
13 
14 #ifndef lint
15 static char sccsid[] =
16 "@(#)atan2.c	1.3 (Berkeley) 8/21/85; 1.3 (ucb.elefunt) 07/07/87";
17 #endif not lint
18 
19 /* ATAN2(Y,X)
20  * RETURN ARG (X+iY)
21  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
22  * CODED IN C BY K.C. NG, 1/8/85;
23  * REVISED BY K.C. NG on 2/7/85, 2/13/85, 3/7/85, 3/30/85, 6/29/85.
24  *
25  * Required system supported functions :
26  *	copysign(x,y)
27  *	scalb(x,y)
28  *	logb(x)
29  *
30  * Method :
31  *	1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
32  *	2. Reduce x to positive by (if x and y are unexceptional):
33  *		ARG (x+iy) = arctan(y/x)   	   ... if x > 0,
34  *		ARG (x+iy) = pi - arctan[y/(-x)]   ... if x < 0,
35  *	3. According to the integer k=4t+0.25 truncated , t=y/x, the argument
36  *	   is further reduced to one of the following intervals and the
37  *	   arctangent of y/x is evaluated by the corresponding formula:
38  *
39  *         [0,7/16]	   atan(y/x) = t - t^3*(a1+t^2*(a2+...(a10+t^2*a11)...)
40  *	   [7/16,11/16]    atan(y/x) = atan(1/2) + atan( (y-x/2)/(x+y/2) )
41  *	   [11/16.19/16]   atan(y/x) = atan( 1 ) + atan( (y-x)/(x+y) )
42  *	   [19/16,39/16]   atan(y/x) = atan(3/2) + atan( (y-1.5x)/(x+1.5y) )
43  *	   [39/16,INF]     atan(y/x) = atan(INF) + atan( -x/y )
44  *
45  * Special cases:
46  * Notations: atan2(y,x) == ARG (x+iy) == ARG(x,y).
47  *
48  *	ARG( NAN , (anything) ) is NaN;
49  *	ARG( (anything), NaN ) is NaN;
50  *	ARG(+(anything but NaN), +-0) is +-0  ;
51  *	ARG(-(anything but NaN), +-0) is +-PI ;
52  *	ARG( 0, +-(anything but 0 and NaN) ) is +-PI/2;
53  *	ARG( +INF,+-(anything but INF and NaN) ) is +-0 ;
54  *	ARG( -INF,+-(anything but INF and NaN) ) is +-PI;
55  *	ARG( +INF,+-INF ) is +-PI/4 ;
56  *	ARG( -INF,+-INF ) is +-3PI/4;
57  *	ARG( (anything but,0,NaN, and INF),+-INF ) is +-PI/2;
58  *
59  * Accuracy:
60  *	atan2(y,x) returns (PI/pi) * the exact ARG (x+iy) nearly rounded,
61  *	where
62  *
63  *	in decimal:
64  *		pi = 3.141592653589793 23846264338327 .....
65  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
66  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
67  *
68  *	in hexadecimal:
69  *		pi = 3.243F6A8885A308D313198A2E....
70  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18	error=.276ulps
71  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2    error=.206ulps
72  *
73  *	In a test run with 356,000 random argument on [-1,1] * [-1,1] on a
74  *	VAX, the maximum observed error was 1.41 ulps (units of the last place)
75  *	compared with (PI/pi)*(the exact ARG(x+iy)).
76  *
77  * Note:
78  *	We use machine PI (the true pi rounded) in place of the actual
79  *	value of pi for all the trig and inverse trig functions. In general,
80  *	if trig is one of sin, cos, tan, then computed trig(y) returns the
81  *	exact trig(y*pi/PI) nearly rounded; correspondingly, computed arctrig
82  *	returns the exact arctrig(y)*PI/pi nearly rounded. These guarantee the
83  *	trig functions have period PI, and trig(arctrig(x)) returns x for
84  *	all critical values x.
85  *
86  * Constants:
87  * The hexadecimal values are the intended ones for the following constants.
88  * The decimal values may be used, provided that the compiler will convert
89  * from decimal to binary accurately enough to produce the hexadecimal values
90  * shown.
91  */
92 
93 static double
94 #if (defined(VAX)||defined(TAHOE)) 	/* VAX D format */
95 athfhi =  4.6364760900080611433E-1    , /*Hex  2^ -1   *  .ED63382B0DDA7B */
96 athflo =  1.9338828231967579916E-19   , /*Hex  2^-62   *  .E450059CFE92C0 */
97 PIo4   =  7.8539816339744830676E-1    , /*Hex  2^  0   *  .C90FDAA22168C2 */
98 at1fhi =  9.8279372324732906796E-1    , /*Hex  2^  0   *  .FB985E940FB4D9 */
99 at1flo = -3.5540295636764633916E-18   , /*Hex  2^-57   * -.831EDC34D6EAEA */
100 PIo2   =  1.5707963267948966135E0     , /*Hex  2^  1   *  .C90FDAA22168C2 */
101 PI     =  3.1415926535897932270E0     , /*Hex  2^  2   *  .C90FDAA22168C2 */
102 a1     =  3.3333333333333473730E-1    , /*Hex  2^ -1   *  .AAAAAAAAAAAB75 */
103 a2     = -2.0000000000017730678E-1    , /*Hex  2^ -2   * -.CCCCCCCCCD946E */
104 a3     =  1.4285714286694640301E-1    , /*Hex  2^ -2   *  .92492492744262 */
105 a4     = -1.1111111135032672795E-1    , /*Hex  2^ -3   * -.E38E38EBC66292 */
106 a5     =  9.0909091380563043783E-2    , /*Hex  2^ -3   *  .BA2E8BB31BD70C */
107 a6     = -7.6922954286089459397E-2    , /*Hex  2^ -3   * -.9D89C827C37F18 */
108 a7     =  6.6663180891693915586E-2    , /*Hex  2^ -3   *  .8886B4AE379E58 */
109 a8     = -5.8772703698290408927E-2    , /*Hex  2^ -4   * -.F0BBA58481A942 */
110 a9     =  5.2170707402812969804E-2    , /*Hex  2^ -4   *  .D5B0F3A1AB13AB */
111 a10    = -4.4895863157820361210E-2    , /*Hex  2^ -4   * -.B7E4B97FD1048F */
112 a11    =  3.3006147437343875094E-2    , /*Hex  2^ -4   *  .8731743CF72D87 */
113 a12    = -1.4614844866464185439E-2    ; /*Hex  2^ -6   * -.EF731A2F3476D9 */
114 #else 	/* IEEE double */
115 athfhi =  4.6364760900080609352E-1    , /*Hex  2^ -2   *  1.DAC670561BB4F */
116 athflo =  4.6249969567426939759E-18   , /*Hex  2^-58   *  1.5543B8F253271 */
117 PIo4   =  7.8539816339744827900E-1    , /*Hex  2^ -1   *  1.921FB54442D18 */
118 at1fhi =  9.8279372324732905408E-1    , /*Hex  2^ -1   *  1.F730BD281F69B */
119 at1flo = -2.4407677060164810007E-17   , /*Hex  2^-56   * -1.C23DFEFEAE6B5 */
120 PIo2   =  1.5707963267948965580E0     , /*Hex  2^  0   *  1.921FB54442D18 */
121 PI     =  3.1415926535897931160E0     , /*Hex  2^  1   *  1.921FB54442D18 */
122 a1     =  3.3333333333333942106E-1    , /*Hex  2^ -2   *  1.55555555555C3 */
123 a2     = -1.9999999999979536924E-1    , /*Hex  2^ -3   * -1.9999999997CCD */
124 a3     =  1.4285714278004377209E-1    , /*Hex  2^ -3   *  1.24924921EC1D7 */
125 a4     = -1.1111110579344973814E-1    , /*Hex  2^ -4   * -1.C71C7059AF280 */
126 a5     =  9.0908906105474668324E-2    , /*Hex  2^ -4   *  1.745CE5AA35DB2 */
127 a6     = -7.6919217767468239799E-2    , /*Hex  2^ -4   * -1.3B0FA54BEC400 */
128 a7     =  6.6614695906082474486E-2    , /*Hex  2^ -4   *  1.10DA924597FFF */
129 a8     = -5.8358371008508623523E-2    , /*Hex  2^ -5   * -1.DE125FDDBD793 */
130 a9     =  4.9850617156082015213E-2    , /*Hex  2^ -5   *  1.9860524BDD807 */
131 a10    = -3.6700606902093604877E-2    , /*Hex  2^ -5   * -1.2CA6C04C6937A */
132 a11    =  1.6438029044759730479E-2    ; /*Hex  2^ -6   *  1.0D52174A1BB54 */
133 #endif
134 
135 double atan2(y,x)
136 double  y,x;
137 {
138 	static double zero=0, one=1, small=1.0E-9, big=1.0E18;
139 	double copysign(),logb(),scalb(),t,z,signy,signx,hi,lo;
140 	int finite(), k,m;
141 
142     /* if x or y is NAN */
143 	if(x!=x) return(x); if(y!=y) return(y);
144 
145     /* copy down the sign of y and x */
146 	signy = copysign(one,y) ;
147 	signx = copysign(one,x) ;
148 
149     /* if x is 1.0, goto begin */
150 	if(x==1) { y=copysign(y,one); t=y; if(finite(t)) goto begin;}
151 
152     /* when y = 0 */
153 	if(y==zero) return((signx==one)?y:copysign(PI,signy));
154 
155     /* when x = 0 */
156 	if(x==zero) return(copysign(PIo2,signy));
157 
158     /* when x is INF */
159 	if(!finite(x))
160 	    if(!finite(y))
161 		return(copysign((signx==one)?PIo4:3*PIo4,signy));
162 	    else
163 		return(copysign((signx==one)?zero:PI,signy));
164 
165     /* when y is INF */
166 	if(!finite(y)) return(copysign(PIo2,signy));
167 
168 
169     /* compute y/x */
170 	x=copysign(x,one);
171 	y=copysign(y,one);
172 	if((m=(k=logb(y))-logb(x)) > 60) t=big+big;
173 	    else if(m < -80 ) t=y/x;
174 	    else { t = y/x ; y = scalb(y,-k); x=scalb(x,-k); }
175 
176     /* begin argument reduction */
177 begin:
178 	if (t < 2.4375) {
179 
180 	/* truncate 4(t+1/16) to integer for branching */
181 	    k = 4 * (t+0.0625);
182 	    switch (k) {
183 
184 	    /* t is in [0,7/16] */
185 	    case 0:
186 	    case 1:
187 		if (t < small)
188 		    { big + small ;  /* raise inexact flag */
189 		      return (copysign((signx>zero)?t:PI-t,signy)); }
190 
191 		hi = zero;  lo = zero;  break;
192 
193 	    /* t is in [7/16,11/16] */
194 	    case 2:
195 		hi = athfhi; lo = athflo;
196 		z = x+x;
197 		t = ( (y+y) - x ) / ( z +  y ); break;
198 
199 	    /* t is in [11/16,19/16] */
200 	    case 3:
201 	    case 4:
202 		hi = PIo4; lo = zero;
203 		t = ( y - x ) / ( x + y ); break;
204 
205 	    /* t is in [19/16,39/16] */
206 	    default:
207 		hi = at1fhi; lo = at1flo;
208 		z = y-x; y=y+y+y; t = x+x;
209 		t = ( (z+z)-x ) / ( t + y ); break;
210 	    }
211 	}
212 	/* end of if (t < 2.4375) */
213 
214 	else
215 	{
216 	    hi = PIo2; lo = zero;
217 
218 	    /* t is in [2.4375, big] */
219 	    if (t <= big)  t = - x / y;
220 
221 	    /* t is in [big, INF] */
222 	    else
223 	      { big+small;	/* raise inexact flag */
224 		t = zero; }
225 	}
226     /* end of argument reduction */
227 
228     /* compute atan(t) for t in [-.4375, .4375] */
229 	z = t*t;
230 #if (defined(VAX)||defined(TAHOE))
231 	z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+
232 			z*(a9+z*(a10+z*(a11+z*a12))))))))))));
233 #else	/* IEEE double */
234 	z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+
235 			z*(a9+z*(a10+z*a11)))))))))));
236 #endif
237 	z = lo - z; z += t; z += hi;
238 
239 	return(copysign((signx>zero)?z:PI-z,signy));
240 }
241