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