]> www.ginac.de Git - cln.git/blob - src/float/transcendental/cl_F_atanx.cc
Initial revision
[cln.git] / src / float / transcendental / cl_F_atanx.cc
1 // atanx().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_F_tran.h"
8
9
10 // Implementation.
11
12 #include "cl_float.h"
13 #include "cl_low.h"
14 #include "cl_F.h"
15 #include "cl_lfloat.h"
16 #include "cl_LF.h"
17 #include "cl_integer.h"
18
19 #undef MAYBE_INLINE
20 #define MAYBE_INLINE inline
21 #include "cl_LF_zerop.cc"
22 #include "cl_LF_minusp.cc"
23 #include "cl_LF_exponent.cc"
24
25 // cl_F atanx_naive (const cl_F& x)
26 // cl_LF atanx_naive (const cl_LF& x)
27 //
28 // Methode:
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.
38 //   Ergebnis x*sum.
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 .
45
46 static const cl_LF atanx_naive (const cl_LF& x)
47 {
48         if (zerop(x))
49                 return 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
61         // große len).
62         var uintL sqrt_d = floor(isqrt(d)*3,8); // limit_slope*floor(sqrt(d))
63         var cl_LF xx = x;
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|
68                 do {
69                   // nächstes x nach der Formel x := x+sqrt(x^2 + 1) berechnen:
70                   xx = sqrt(square(xx) + cl_float(1,xx)) + xx;
71                   k = k+1;
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.
77                 xx = recip(xx);
78                 if (minusp(x))
79                         xx = - xx; // Vorzeichen wieder rein
80         }
81         // Potenzreihe anwenden:
82         var int i = 1;
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)
86         if (0) {
87                 // naive1:
88                 // floating-point representation
89                 loop {
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
93                         sum = new_sum;
94                         b = b*a;
95                         i = i+2;
96                 }
97         } else {
98                 // naive2:
99                 // floating-point representation with smooth precision reduction
100                 var cl_LF eps = scale_float(b,-(sintL)d-10);
101                 loop {
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
105                         sum = new_sum;
106                         b = cl_LF_shortenwith(b,eps);
107                         b = b*a;
108                         i = i+2;
109                 }
110         }
111         var cl_LF erg = sum*xx; // sum*x als Ergebnis
112         return scale_float(erg,k); // wegen Rekursion noch mal 2^k
113 }
114 // Bit complexity (N = length(x)): O(N^(1/2)*M(N)).
115
116 static const cl_F atanx_naive (const cl_F& x)
117 {
118         if (zerop(x))
119                 return 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.
129         var cl_F xx = x;
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|
134                 do {
135                   // nächstes x nach der Formel x := x+sqrt(x^2 + 1) berechnen:
136                   xx = sqrt(square(xx) + cl_float(1,xx)) + xx;
137                   k = k+1;
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.
143                 xx = recip(xx);
144                 if (minusp(x))
145                         xx = - xx; // Vorzeichen wieder rein
146         }
147         // Potenzreihe anwenden:
148         var int i = 1;
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)
152         loop {
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
156                 sum = new_sum;
157                 b = b*a;
158                 i = i+2;
159         }
160         var cl_F erg = sum*xx; // sum*x als Ergebnis
161         return scale_float(erg,k); // wegen Rekursion noch mal 2^k
162 }
163 // Bit complexity (N = length(x)): O(N^(1/2)*M(N)).
164
165 static const cl_LF atanx_ratseries (const cl_LF& t)
166 {
167         // Method:
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.
172         // Loop
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))
186                 return t;
187         var cl_LF x = recip(sqrt(cl_I_to_LF(1,len) + square(t)));
188         var cl_LF y = t*x;
189         var cl_LF z = cl_I_to_LF(0,len);
190         loop {
191                 if (zerop(y) || (float_exponent(y) <= (sintL)(-d)>>1))
192                         break;
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);
197                 var cl_I p;
198                 var uintL lq;
199                 var cl_boolean last_step = cl_false;
200                 if (lm >= me) { // |y| >= 1/2 ?
201                         p = y_.sign; // 1 or -1
202                         lq = 1;
203                 } else {
204                         var uintL n = me - lm; // |y| < 2^-n with n maximal
205                         // Set p to the first n bits of |y|:
206                         if (lm > n) {
207                                 p = y_.mantissa >> (lm - n);
208                                 lq = 2*n;
209                         } else {
210                                 p = y_.mantissa;
211                                 lq = lm + n;
212                         }
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.
219                         if (2*n >= lm)
220                                 last_step = cl_true;
221                 }
222                 z = z + scale_float(cl_I_to_LF(p,len),-(sintL)lq);
223                 if (last_step)
224                         break;
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;
229         }
230         return z+y;
231 }
232 // Bit complexity (N = length(x)): O(log(N)^2*M(N)).
233
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
239 //   50     0.25    0.17    0.34
240 //  100     1.06    0.70    1.07
241 //  250     7.5     5.0     5.6
242 //  500    34.7    23.2    20.0
243 // 1000   167     112      65
244 // ==> ratseries faster for N >= 325.
245
246 const cl_F atanx (const cl_F& x)
247 {
248         if (longfloatp(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);
252                 else
253                         return atanx_naive(x);
254         } else
255                 return atanx_naive(x);
256 }
257 // Bit complexity (N = length(x)): O(log(N)^2*M(N)).
258