n_atan2.c revision 1.6.74.1 1 1.6.74.1 martin /* $NetBSD: n_atan2.c,v 1.6.74.1 2014/10/13 19:34:58 martin Exp $ */
2 1.1 ragge /*
3 1.1 ragge * Copyright (c) 1985, 1993
4 1.1 ragge * The Regents of the University of California. All rights reserved.
5 1.1 ragge *
6 1.1 ragge * Redistribution and use in source and binary forms, with or without
7 1.1 ragge * modification, are permitted provided that the following conditions
8 1.1 ragge * are met:
9 1.1 ragge * 1. Redistributions of source code must retain the above copyright
10 1.1 ragge * notice, this list of conditions and the following disclaimer.
11 1.1 ragge * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 ragge * notice, this list of conditions and the following disclaimer in the
13 1.1 ragge * documentation and/or other materials provided with the distribution.
14 1.6 agc * 3. Neither the name of the University nor the names of its contributors
15 1.1 ragge * may be used to endorse or promote products derived from this software
16 1.1 ragge * without specific prior written permission.
17 1.1 ragge *
18 1.1 ragge * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 1.1 ragge * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.1 ragge * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 ragge * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 1.1 ragge * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 ragge * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.1 ragge * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 ragge * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 ragge * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 ragge * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 ragge * SUCH DAMAGE.
29 1.1 ragge */
30 1.1 ragge
31 1.1 ragge #ifndef lint
32 1.1 ragge static char sccsid[] = "@(#)atan2.c 8.1 (Berkeley) 6/4/93";
33 1.1 ragge #endif /* not lint */
34 1.1 ragge
35 1.1 ragge /* ATAN2(Y,X)
36 1.1 ragge * RETURN ARG (X+iY)
37 1.1 ragge * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
38 1.4 simonb * CODED IN C BY K.C. NG, 1/8/85;
39 1.1 ragge * REVISED BY K.C. NG on 2/7/85, 2/13/85, 3/7/85, 3/30/85, 6/29/85.
40 1.1 ragge *
41 1.1 ragge * Required system supported functions :
42 1.1 ragge * copysign(x,y)
43 1.1 ragge * scalb(x,y)
44 1.1 ragge * logb(x)
45 1.4 simonb *
46 1.1 ragge * Method :
47 1.1 ragge * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x).
48 1.4 simonb * 2. Reduce x to positive by (if x and y are unexceptional):
49 1.1 ragge * ARG (x+iy) = arctan(y/x) ... if x > 0,
50 1.1 ragge * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0,
51 1.4 simonb * 3. According to the integer k=4t+0.25 truncated , t=y/x, the argument
52 1.4 simonb * is further reduced to one of the following intervals and the
53 1.1 ragge * arctangent of y/x is evaluated by the corresponding formula:
54 1.1 ragge *
55 1.1 ragge * [0,7/16] atan(y/x) = t - t^3*(a1+t^2*(a2+...(a10+t^2*a11)...)
56 1.1 ragge * [7/16,11/16] atan(y/x) = atan(1/2) + atan( (y-x/2)/(x+y/2) )
57 1.1 ragge * [11/16.19/16] atan(y/x) = atan( 1 ) + atan( (y-x)/(x+y) )
58 1.1 ragge * [19/16,39/16] atan(y/x) = atan(3/2) + atan( (y-1.5x)/(x+1.5y) )
59 1.1 ragge * [39/16,INF] atan(y/x) = atan(INF) + atan( -x/y )
60 1.1 ragge *
61 1.1 ragge * Special cases:
62 1.1 ragge * Notations: atan2(y,x) == ARG (x+iy) == ARG(x,y).
63 1.1 ragge *
64 1.1 ragge * ARG( NAN , (anything) ) is NaN;
65 1.1 ragge * ARG( (anything), NaN ) is NaN;
66 1.1 ragge * ARG(+(anything but NaN), +-0) is +-0 ;
67 1.1 ragge * ARG(-(anything but NaN), +-0) is +-PI ;
68 1.1 ragge * ARG( 0, +-(anything but 0 and NaN) ) is +-PI/2;
69 1.1 ragge * ARG( +INF,+-(anything but INF and NaN) ) is +-0 ;
70 1.1 ragge * ARG( -INF,+-(anything but INF and NaN) ) is +-PI;
71 1.1 ragge * ARG( +INF,+-INF ) is +-PI/4 ;
72 1.1 ragge * ARG( -INF,+-INF ) is +-3PI/4;
73 1.1 ragge * ARG( (anything but,0,NaN, and INF),+-INF ) is +-PI/2;
74 1.1 ragge *
75 1.1 ragge * Accuracy:
76 1.4 simonb * atan2(y,x) returns (PI/pi) * the exact ARG (x+iy) nearly rounded,
77 1.1 ragge * where
78 1.1 ragge *
79 1.1 ragge * in decimal:
80 1.4 simonb * pi = 3.141592653589793 23846264338327 .....
81 1.1 ragge * 53 bits PI = 3.141592653589793 115997963 ..... ,
82 1.4 simonb * 56 bits PI = 3.141592653589793 227020265 ..... ,
83 1.1 ragge *
84 1.1 ragge * in hexadecimal:
85 1.1 ragge * pi = 3.243F6A8885A308D313198A2E....
86 1.1 ragge * 53 bits PI = 3.243F6A8885A30 = 2 * 1.921FB54442D18 error=.276ulps
87 1.1 ragge * 56 bits PI = 3.243F6A8885A308 = 4 * .C90FDAA22168C2 error=.206ulps
88 1.4 simonb *
89 1.1 ragge * In a test run with 356,000 random argument on [-1,1] * [-1,1] on a
90 1.1 ragge * VAX, the maximum observed error was 1.41 ulps (units of the last place)
91 1.1 ragge * compared with (PI/pi)*(the exact ARG(x+iy)).
92 1.1 ragge *
93 1.1 ragge * Note:
94 1.1 ragge * We use machine PI (the true pi rounded) in place of the actual
95 1.4 simonb * value of pi for all the trig and inverse trig functions. In general,
96 1.4 simonb * if trig is one of sin, cos, tan, then computed trig(y) returns the
97 1.4 simonb * exact trig(y*pi/PI) nearly rounded; correspondingly, computed arctrig
98 1.4 simonb * returns the exact arctrig(y)*PI/pi nearly rounded. These guarantee the
99 1.1 ragge * trig functions have period PI, and trig(arctrig(x)) returns x for
100 1.1 ragge * all critical values x.
101 1.4 simonb *
102 1.1 ragge * Constants:
103 1.1 ragge * The hexadecimal values are the intended ones for the following constants.
104 1.1 ragge * The decimal values may be used, provided that the compiler will convert
105 1.1 ragge * from decimal to binary accurately enough to produce the hexadecimal values
106 1.1 ragge * shown.
107 1.1 ragge */
108 1.1 ragge
109 1.5 matt #define _LIBM_STATIC
110 1.1 ragge #include "mathimpl.h"
111 1.1 ragge
112 1.1 ragge vc(athfhi, 4.6364760900080611433E-1 ,6338,3fed,da7b,2b0d, -1, .ED63382B0DDA7B)
113 1.1 ragge vc(athflo, 1.9338828231967579916E-19 ,5005,2164,92c0,9cfe, -62, .E450059CFE92C0)
114 1.1 ragge vc(PIo4, 7.8539816339744830676E-1 ,0fda,4049,68c2,a221, 0, .C90FDAA22168C2)
115 1.1 ragge vc(at1fhi, 9.8279372324732906796E-1 ,985e,407b,b4d9,940f, 0, .FB985E940FB4D9)
116 1.1 ragge vc(at1flo,-3.5540295636764633916E-18 ,1edc,a383,eaea,34d6, -57,-.831EDC34D6EAEA)
117 1.1 ragge vc(PIo2, 1.5707963267948966135E0 ,0fda,40c9,68c2,a221, 1, .C90FDAA22168C2)
118 1.1 ragge vc(PI, 3.1415926535897932270E0 ,0fda,4149,68c2,a221, 2, .C90FDAA22168C2)
119 1.1 ragge vc(a1, 3.3333333333333473730E-1 ,aaaa,3faa,ab75,aaaa, -1, .AAAAAAAAAAAB75)
120 1.1 ragge vc(a2, -2.0000000000017730678E-1 ,cccc,bf4c,946e,cccd, -2,-.CCCCCCCCCD946E)
121 1.1 ragge vc(a3, 1.4285714286694640301E-1 ,4924,3f12,4262,9274, -2, .92492492744262)
122 1.1 ragge vc(a4, -1.1111111135032672795E-1 ,8e38,bee3,6292,ebc6, -3,-.E38E38EBC66292)
123 1.1 ragge vc(a5, 9.0909091380563043783E-2 ,2e8b,3eba,d70c,b31b, -3, .BA2E8BB31BD70C)
124 1.1 ragge vc(a6, -7.6922954286089459397E-2 ,89c8,be9d,7f18,27c3, -3,-.9D89C827C37F18)
125 1.1 ragge vc(a7, 6.6663180891693915586E-2 ,86b4,3e88,9e58,ae37, -3, .8886B4AE379E58)
126 1.1 ragge vc(a8, -5.8772703698290408927E-2 ,bba5,be70,a942,8481, -4,-.F0BBA58481A942)
127 1.1 ragge vc(a9, 5.2170707402812969804E-2 ,b0f3,3e55,13ab,a1ab, -4, .D5B0F3A1AB13AB)
128 1.1 ragge vc(a10, -4.4895863157820361210E-2 ,e4b9,be37,048f,7fd1, -4,-.B7E4B97FD1048F)
129 1.1 ragge vc(a11, 3.3006147437343875094E-2 ,3174,3e07,2d87,3cf7, -4, .8731743CF72D87)
130 1.1 ragge vc(a12, -1.4614844866464185439E-2 ,731a,bd6f,76d9,2f34, -6,-.EF731A2F3476D9)
131 1.1 ragge
132 1.1 ragge ic(athfhi, 4.6364760900080609352E-1 , -2, 1.DAC670561BB4F)
133 1.1 ragge ic(athflo, 4.6249969567426939759E-18 , -58, 1.5543B8F253271)
134 1.1 ragge ic(PIo4, 7.8539816339744827900E-1 , -1, 1.921FB54442D18)
135 1.1 ragge ic(at1fhi, 9.8279372324732905408E-1 , -1, 1.F730BD281F69B)
136 1.1 ragge ic(at1flo,-2.4407677060164810007E-17 , -56, -1.C23DFEFEAE6B5)
137 1.1 ragge ic(PIo2, 1.5707963267948965580E0 , 0, 1.921FB54442D18)
138 1.1 ragge ic(PI, 3.1415926535897931160E0 , 1, 1.921FB54442D18)
139 1.1 ragge ic(a1, 3.3333333333333942106E-1 , -2, 1.55555555555C3)
140 1.1 ragge ic(a2, -1.9999999999979536924E-1 , -3, -1.9999999997CCD)
141 1.1 ragge ic(a3, 1.4285714278004377209E-1 , -3, 1.24924921EC1D7)
142 1.1 ragge ic(a4, -1.1111110579344973814E-1 , -4, -1.C71C7059AF280)
143 1.1 ragge ic(a5, 9.0908906105474668324E-2 , -4, 1.745CE5AA35DB2)
144 1.1 ragge ic(a6, -7.6919217767468239799E-2 , -4, -1.3B0FA54BEC400)
145 1.1 ragge ic(a7, 6.6614695906082474486E-2 , -4, 1.10DA924597FFF)
146 1.1 ragge ic(a8, -5.8358371008508623523E-2 , -5, -1.DE125FDDBD793)
147 1.1 ragge ic(a9, 4.9850617156082015213E-2 , -5, 1.9860524BDD807)
148 1.1 ragge ic(a10, -3.6700606902093604877E-2 , -5, -1.2CA6C04C6937A)
149 1.1 ragge ic(a11, 1.6438029044759730479E-2 , -6, 1.0D52174A1BB54)
150 1.1 ragge
151 1.1 ragge #ifdef vccast
152 1.1 ragge #define athfhi vccast(athfhi)
153 1.1 ragge #define athflo vccast(athflo)
154 1.1 ragge #define PIo4 vccast(PIo4)
155 1.1 ragge #define at1fhi vccast(at1fhi)
156 1.1 ragge #define at1flo vccast(at1flo)
157 1.1 ragge #define PIo2 vccast(PIo2)
158 1.1 ragge #define PI vccast(PI)
159 1.1 ragge #define a1 vccast(a1)
160 1.1 ragge #define a2 vccast(a2)
161 1.1 ragge #define a3 vccast(a3)
162 1.1 ragge #define a4 vccast(a4)
163 1.1 ragge #define a5 vccast(a5)
164 1.1 ragge #define a6 vccast(a6)
165 1.1 ragge #define a7 vccast(a7)
166 1.1 ragge #define a8 vccast(a8)
167 1.1 ragge #define a9 vccast(a9)
168 1.1 ragge #define a10 vccast(a10)
169 1.1 ragge #define a11 vccast(a11)
170 1.1 ragge #define a12 vccast(a12)
171 1.1 ragge #endif
172 1.1 ragge
173 1.6.74.1 martin #ifdef __weak_alias
174 1.6.74.1 martin __weak_alias(_atan2l, atan2);
175 1.6.74.1 martin #endif
176 1.6.74.1 martin
177 1.5 matt double
178 1.5 matt atan2(double y, double x)
179 1.4 simonb {
180 1.1 ragge static const double zero=0, one=1, small=1.0E-9, big=1.0E18;
181 1.1 ragge double t,z,signy,signx,hi,lo;
182 1.1 ragge int k,m;
183 1.1 ragge
184 1.2 matt #if !defined(__vax__)&&!defined(tahoe)
185 1.1 ragge /* if x or y is NAN */
186 1.1 ragge if(x!=x) return(x); if(y!=y) return(y);
187 1.2 matt #endif /* !defined(__vax__)&&!defined(tahoe) */
188 1.1 ragge
189 1.1 ragge /* copy down the sign of y and x */
190 1.4 simonb signy = copysign(one,y) ;
191 1.4 simonb signx = copysign(one,x) ;
192 1.1 ragge
193 1.1 ragge /* if x is 1.0, goto begin */
194 1.1 ragge if(x==1) { y=copysign(y,one); t=y; if(finite(t)) goto begin;}
195 1.1 ragge
196 1.1 ragge /* when y = 0 */
197 1.1 ragge if(y==zero) return((signx==one)?y:copysign(PI,signy));
198 1.1 ragge
199 1.1 ragge /* when x = 0 */
200 1.1 ragge if(x==zero) return(copysign(PIo2,signy));
201 1.4 simonb
202 1.1 ragge /* when x is INF */
203 1.1 ragge if(!finite(x))
204 1.4 simonb if(!finite(y))
205 1.1 ragge return(copysign((signx==one)?PIo4:3*PIo4,signy));
206 1.1 ragge else
207 1.1 ragge return(copysign((signx==one)?zero:PI,signy));
208 1.1 ragge
209 1.1 ragge /* when y is INF */
210 1.1 ragge if(!finite(y)) return(copysign(PIo2,signy));
211 1.1 ragge
212 1.1 ragge /* compute y/x */
213 1.4 simonb x=copysign(x,one);
214 1.4 simonb y=copysign(y,one);
215 1.4 simonb if((m=(k=logb(y))-logb(x)) > 60) t=big+big;
216 1.1 ragge else if(m < -80 ) t=y/x;
217 1.1 ragge else { t = y/x ; y = scalb(y,-k); x=scalb(x,-k); }
218 1.1 ragge
219 1.1 ragge /* begin argument reduction */
220 1.1 ragge begin:
221 1.4 simonb if (t < 2.4375) {
222 1.1 ragge
223 1.1 ragge /* truncate 4(t+1/16) to integer for branching */
224 1.1 ragge k = 4 * (t+0.0625);
225 1.1 ragge switch (k) {
226 1.1 ragge
227 1.1 ragge /* t is in [0,7/16] */
228 1.4 simonb case 0:
229 1.1 ragge case 1:
230 1.4 simonb if (t < small)
231 1.1 ragge { big + small ; /* raise inexact flag */
232 1.1 ragge return (copysign((signx>zero)?t:PI-t,signy)); }
233 1.1 ragge
234 1.1 ragge hi = zero; lo = zero; break;
235 1.1 ragge
236 1.1 ragge /* t is in [7/16,11/16] */
237 1.4 simonb case 2:
238 1.1 ragge hi = athfhi; lo = athflo;
239 1.1 ragge z = x+x;
240 1.1 ragge t = ( (y+y) - x ) / ( z + y ); break;
241 1.1 ragge
242 1.1 ragge /* t is in [11/16,19/16] */
243 1.4 simonb case 3:
244 1.1 ragge case 4:
245 1.1 ragge hi = PIo4; lo = zero;
246 1.1 ragge t = ( y - x ) / ( x + y ); break;
247 1.1 ragge
248 1.1 ragge /* t is in [19/16,39/16] */
249 1.4 simonb default:
250 1.1 ragge hi = at1fhi; lo = at1flo;
251 1.1 ragge z = y-x; y=y+y+y; t = x+x;
252 1.1 ragge t = ( (z+z)-x ) / ( t + y ); break;
253 1.1 ragge }
254 1.1 ragge }
255 1.1 ragge /* end of if (t < 2.4375) */
256 1.1 ragge
257 1.4 simonb else
258 1.1 ragge {
259 1.1 ragge hi = PIo2; lo = zero;
260 1.1 ragge
261 1.1 ragge /* t is in [2.4375, big] */
262 1.1 ragge if (t <= big) t = - x / y;
263 1.1 ragge
264 1.1 ragge /* t is in [big, INF] */
265 1.4 simonb else
266 1.1 ragge { big+small; /* raise inexact flag */
267 1.1 ragge t = zero; }
268 1.1 ragge }
269 1.1 ragge /* end of argument reduction */
270 1.1 ragge
271 1.1 ragge /* compute atan(t) for t in [-.4375, .4375] */
272 1.1 ragge z = t*t;
273 1.3 ragge #if defined(__vax__)||defined(tahoe)
274 1.1 ragge z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+
275 1.1 ragge z*(a9+z*(a10+z*(a11+z*a12))))))))))));
276 1.3 ragge #else /* defined(__vax__)||defined(tahoe) */
277 1.1 ragge z = t*(z*(a1+z*(a2+z*(a3+z*(a4+z*(a5+z*(a6+z*(a7+z*(a8+
278 1.1 ragge z*(a9+z*(a10+z*a11)))))))))));
279 1.3 ragge #endif /* defined(__vax__)||defined(tahoe) */
280 1.1 ragge z = lo - z; z += t; z += hi;
281 1.1 ragge
282 1.1 ragge return(copysign((signx>zero)?z:PI-z,signy));
283 1.1 ragge }
284