15 #include "cl_lfloat.h"
17 #include "cl_integer.h"
20 #define MAYBE_INLINE inline
21 #include "cl_LF_zerop.cc"
22 #include "cl_LF_minusp.cc"
23 #include "cl_LF_exponent.cc"
25 // cl_F atanx_naive (const cl_F& x)
26 // cl_LF atanx_naive (const cl_LF& x)
29 // e := Exponent aus (decode-float x), d := (float-digits x)
30 // Bei x=0.0 oder e<=-d/2 liefere x
31 // (denn bei e<=-d/2 ist x^2/3 < x^2/2 < 2^(-d)/2 = 2^(-d-1), also
32 // 1 >= atan(x)/x > 1-x^2/3 > 1-2^(-d-1),
33 // also ist atan(x)/x, auf d Bits gerundet, gleich 1.0).
34 // Bei e<=-sqrt(d) verwende die Potenzreihe
35 // atan(x)/x = sum(j=0..inf,(-x^2)^j/(2j+1)):
36 // a:=-x^2, b:=1, i:=1, sum:=0,
37 // while (/= sum (setq sum (+ sum (/ b i)))) do i:=i+2, b:=b*a.
39 // Sonst setze y := x/(1+sqrt(1+x^2)), berechne rekursiv z:=atan(y)
40 // und liefere 2*z = (scale-float z 1).
41 // Diese Rekursion wird entrekursiviert. Statt k mal hintereinander
42 // x := x/(1+sqrt(1+x^2)) zu bilden, arbeitet man lieber mit den Kehrwerten,
43 // setzt also x := 1/|x|, dann k mal x := x+sqrt(x^2+1), dann x := +- 1/x.
44 // Aufwand: asymptotisch d^2.5 .
46 static const cl_LF atanx_naive (const cl_LF& x)
50 var uintL actuallen = TheLfloat(x)->len;
51 var uintL d = float_digits(x);
52 var sintL e = float_exponent(x);
53 if (e <= (sintL)(-d)>>1) // e <= -d/2 <==> e <= -ceiling(d/2)
54 return x; // ja -> x als Ergebnis
55 var uintL k = 0; // Rekursionszähler k:=0
56 // Bei e <= -1-limit_slope*floor(sqrt(d)) kann die Potenzreihe
57 // angewandt werden. limit_slope = 1.0 ist schlecht (ca. 20% zu
58 // schlecht). Ein guter Wert ist:
59 // Für naive1: limit_scope = 0.5.
60 // Für naive2: limit_scope = 0.375 (ca. 0.5 für kleine len, 0.35 für
62 var uintL sqrt_d = floor(isqrt(d)*3,8); // limit_slope*floor(sqrt(d))
64 if (e >= (sintL)(-sqrt_d)) {
65 // e > -1-limit_slope*floor(sqrt(d)) -> muß |x| verkleinern.
66 var sintL e_limit = 1+sqrt_d; // 1+limit_slope*floor(sqrt(d))
67 xx = recip(abs(xx)); // 1/|x|
69 // nächstes x nach der Formel x := x+sqrt(x^2 + 1) berechnen:
70 xx = sqrt(square(xx) + cl_float(1,xx)) + xx;
72 } until (float_exponent(xx) > e_limit);
73 // Schleifenende mit Exponent(x) > 1+limit_slope*floor(sqrt(d)),
74 // also x >= 2^(1+limit_slope*floor(sqrt(d))),
75 // also 1/x <= 2^(-1-limit_slope*floor(sqrt(d))).
76 // Nun kann die Potenzreihe auf 1/x angewandt werden.
79 xx = - xx; // Vorzeichen wieder rein
81 // Potenzreihe anwenden:
83 var cl_LF a = - square(xx); // a = - x^2
84 var cl_LF b = cl_float(1,xx); // b := (float 1 x)
85 var cl_LF sum = cl_float(0,xx); // sum := (float 0 x)
88 // floating-point representation
90 var cl_LF new_sum = sum + b / (cl_I)i; // (+ sum (/ b i))
91 if (new_sum == sum) // = sum ?
92 break; // ja -> Potenzreihe abbrechen
99 // floating-point representation with smooth precision reduction
100 var cl_LF eps = scale_float(b,-(sintL)d-10);
102 var cl_LF new_sum = sum + LF_to_LF(b/(cl_I)i,actuallen); // (+ sum (/ b i))
103 if (new_sum == sum) // = sum ?
104 break; // ja -> Potenzreihe abbrechen
106 b = cl_LF_shortenwith(b,eps);
111 var cl_LF erg = sum*xx; // sum*x als Ergebnis
112 return scale_float(erg,k); // wegen Rekursion noch mal 2^k
114 // Bit complexity (N = length(x)): O(N^(1/2)*M(N)).
116 static const cl_F atanx_naive (const cl_F& x)
120 var uintL d = float_digits(x);
121 var sintL e = float_exponent(x);
122 if (e <= (sintL)(-d)>>1) // e <= -d/2 <==> e <= -ceiling(d/2)
123 return x; // ja -> x als Ergebnis
124 var uintL k = 0; // Rekursionszähler k:=0
125 var uintL sqrt_d = floor(isqrt(d),2); // limit_slope*floor(sqrt(d))
126 // Bei e <= -1-limit_slope*floor(sqrt(d)) kann die Potenzreihe
127 // angewandt werden. limit_slope = 1.0 ist schlecht (ca. 20% zu
128 // schlecht). Ein guter Wert ist limit_scope = 0.5.
130 if (e >= (sintL)(-sqrt_d)) {
131 // e > -1-limit_slope*floor(sqrt(d)) -> muß |x| verkleinern.
132 var sintL e_limit = 1+sqrt_d; // 1+limit_slope*floor(sqrt(d))
133 xx = recip(abs(xx)); // 1/|x|
135 // nächstes x nach der Formel x := x+sqrt(x^2 + 1) berechnen:
136 xx = sqrt(square(xx) + cl_float(1,xx)) + xx;
138 } until (float_exponent(xx) > e_limit);
139 // Schleifenende mit Exponent(x) > 1+limit_slope*floor(sqrt(d)),
140 // also x >= 2^(1+limit_slope*floor(sqrt(d))),
141 // also 1/x <= 2^(-1-limit_slope*floor(sqrt(d))).
142 // Nun kann die Potenzreihe auf 1/x angewandt werden.
145 xx = - xx; // Vorzeichen wieder rein
147 // Potenzreihe anwenden:
149 var cl_F a = - square(xx); // a = - x^2
150 var cl_F b = cl_float(1,xx); // b := (float 1 x)
151 var cl_F sum = cl_float(0,xx); // sum := (float 0 x)
153 var cl_F new_sum = sum + b / (cl_I)i; // (+ sum (/ b i))
154 if (new_sum == sum) // = sum ?
155 break; // ja -> Potenzreihe abbrechen
160 var cl_F erg = sum*xx; // sum*x als Ergebnis
161 return scale_float(erg,k); // wegen Rekursion noch mal 2^k
163 // Bit complexity (N = length(x)): O(N^(1/2)*M(N)).
165 static const cl_LF atanx_ratseries (const cl_LF& t)
168 // Based on the same ideas as lnx_ratseries.
169 // e := exponent of (decode-float t), d := (float-digits t).
170 // If t=0.0 or e<=-d/2, return t.
171 // (x,y) := (1/sqrt(1+t^2),t/sqrt(1+t^2)), z := 0.
173 // [(x+i*y)*exp(i*z) is invariant, x>0, sqrt(x^2+y^2)=1]
174 // e := exponent of (decode-float y), d := (float-digits y).
175 // If y=0.0 or e<=-d/2, return z+y
176 // (because if e<=-d/2 then |y|^3/6 < 2^(-d)/2*|y|, and since
177 // asin(y) = y+y^3/6+..., asin(y) rounded to d bits is = y).
178 // Choose approximation z' of angle(x+i*y):
179 // If |y| >= 1/2, set z' = 1/2 * sign(y).
180 // If |y| < 2^-n with n maximal, set
181 // z' = truncate(y*2^(2n))/2^(2n).
182 // Set z := z + z' and x+i*y := (x+i*y)*exp(-i*z').
183 var uintC len = TheLfloat(t)->len;
184 var uintL d = intDsize*(uintL)len;
185 if (zerop(t) || (float_exponent(t) <= (sintL)(-d)>>1))
187 var cl_LF x = recip(sqrt(cl_I_to_LF(1,len) + square(t)));
189 var cl_LF z = cl_I_to_LF(0,len);
191 if (zerop(y) || (float_exponent(y) <= (sintL)(-d)>>1))
193 var cl_idecoded_float y_ = integer_decode_float(y);
194 // y = (-1)^sign * 2^exponent * mantissa
195 var uintL lm = integer_length(y_.mantissa);
196 var uintL me = cl_I_to_UL(- y_.exponent);
199 var cl_boolean last_step = cl_false;
200 if (lm >= me) { // |y| >= 1/2 ?
201 p = y_.sign; // 1 or -1
204 var uintL n = me - lm; // |y| < 2^-n with n maximal
205 // Set p to the first n bits of |y|:
207 p = y_.mantissa >> (lm - n);
213 if (minusp(y_.sign)) { p = -p; }
214 // If 2*n >= lm = intDsize*len, then within our
215 // precision exp(-i*z')=1-i*z' (because |z'^2| < 2^-lm),
216 // and we know a priori that the iteration will stop
217 // after the next big multiplication. This saves one
218 // big multiplication at the end.
222 z = z + scale_float(cl_I_to_LF(p,len),-(sintL)lq);
225 var cl_LF_cos_sin_t cis_z = cl_cossin_aux(-p,lq,len);
226 var cl_LF new_x = x*cis_z.cos - y*cis_z.sin;
227 var cl_LF new_y = x*cis_z.sin + y*cis_z.cos;
228 x = new_x; y = new_y;
232 // Bit complexity (N = length(x)): O(log(N)^2*M(N)).
234 // Timings of the above algorithms, on an i486 33 MHz, running Linux,
235 // applied to x = sqrt(2)-1 = 0.414...
236 // N naive1 naive2 ratseries
237 // 10 0.013 0.013 0.043
238 // 25 0.062 0.048 0.122
240 // 100 1.06 0.70 1.07
242 // 500 34.7 23.2 20.0
244 // ==> ratseries faster for N >= 325.
246 const cl_F atanx (const cl_F& x)
249 DeclareType(cl_LF,x);
250 if (TheLfloat(x)->len >= 325)
251 return cl_float(atanx_ratseries(extend(x,TheLfloat(x)->len+1)),x);
253 return atanx_naive(x);
255 return atanx_naive(x);
257 // Bit complexity (N = length(x)): O(log(N)^2*M(N)).