12 #include "cln/float.h"
15 #include "cln/lfloat.h"
17 #include "cln/integer.h"
20 #define MAYBE_INLINE inline
21 #include "cl_LF_zerop.cc"
22 #include "cl_LF_exponent.cc"
26 // sinxbyx is mainly for cl_SF, cl_FF, cl_DF, where we want to avoid underflow.
28 const cl_F sinxbyx_naive (const cl_F& x)
31 // e := Exponent aus (decode-float x), d := (float-digits x)
32 // Bei x=0.0 oder e<=-d/2 liefere 1.0
33 // (denn bei e<=-d/2 ist x^2/6 < x^2/4 < 2^(-d)/4 = 2^(-d-2), also
34 // 1 >= sin(x)/x > 1-x^2/6 > 1-2^(-d-2), also 1 >= (sin(x)/x)^2 > 1-2^(-d-1),
35 // also ist (sin(x)/x)^2, auf d Bits gerundet, gleich 1.0).
36 // Bei e<=-sqrt(d) verwende die Potenzreihe
37 // sin(x)/x = sum(j=0..inf,(-x^2)^j/(2j+1)!):
38 // a:=-x^2, b:=1, i:=1, sum:=0,
39 // while (/= sum (setq sum (+ sum b))) do b:=b*a/((i+1)*(i+2)), i:=i+2.
41 // Sonst setze y := x/2 = (scale-float x -1),
42 // berechne rekursiv z:=(sin(y)/y)^2 und liefere z*(1-y^2*z).
43 // [Die Grenze sqrt(d) ergibt sich so:
44 // Man braucht bei der Potenzreihe mit x=2^-k etwa j Glieder, mit
45 // k*j*ln 2 + j*(ln j - 1) = d, und der Aufwand beträgt etwa 2.8*(j/2)
46 // Multiplikationen von d-Bit-Zahlen. Bei Halbierungen bis x=2^-k ist der
47 // Gesamtaufwand etwa 2*(k+e)+1.4*j(k). Dieses minimieren nach k: Soll sein
48 // -1.4 = d/dk j(k) = (d/dj k(j))^-1 = - j^2/(d+j)*ln 2, also j^2=2(d+j),
49 // grob j=sqrt(2d) und damit k=sqrt(d).]
50 // Aufwand: asymptotisch d^2.5 .
54 var uintL d = float_digits(x);
55 var sintL e = float_exponent(x);
56 if (e <= (-(sintL)d)>>1) // e <= (-d)/2 <==> e <= -ceiling(d/2) ?
57 return cl_float(1,x); // ja -> 1.0 als Ergebnis
59 // Bei e <= -1-limit_slope*floor(sqrt(d)) kann die Potenzreihe
60 // angewandt werden. limit_slope = 1.0 ist sehr schlecht (ca. 30%
61 // zu schlecht). Gute Werte bei N limbs:
69 // Wähle limit_slope = 13/32 = 0.4.
70 var sintL e_limit = -1-floor(isqrt(d)*13,32); // -1-floor(sqrt(d))
72 // e > -1-limit_slope*floor(sqrt(d)) -> muß |x| verkleinern.
73 x = scale_float(x,e_limit-e);
74 // Neuer Exponent = e_limit.
76 var cl_F x2 = square(x); // x^2
77 // Potenzreihe anwenden:
78 var cl_F a = - x2; // a := -x^2
80 var cl_F b = cl_float(1,x); // b := (float 1 x)
81 var cl_F sum = cl_float(0,x); // sum := (float 0 x)
83 var cl_F new_sum = sum + b;
84 if (new_sum == sum) // = sum ?
85 break; // ja -> Potenzreihe abbrechen
87 b = (b*a)/(cl_I)((i+1)*(i+2));
90 var cl_F z = square(sum); // sum^2 als Ergebnis
92 z = z - x2 * square(z);
93 x2 = scale_float(x2,2); // x^2 := x^2*4
98 // Bit complexity (N = length(x)): O(N^(1/2)*M(N)).
100 const cl_LF sinx_naive (const cl_LF& x)
103 // e := Exponent aus (decode-float x), d := (float-digits x)
104 // Bei x=0.0 oder e<=-d/2 liefere x
105 // (denn bei e<=-d/2 ist x^2/6 < x^2/4 < 2^(-d)/4 = 2^(-d-2), also
106 // 1 >= sin(x)/x > 1-x^2/6 > 1-2^(-d-2), also ist sin(x)^2, auf d Bits
107 // gerundet, gleich x).
108 // Bei e<=-sqrt(d) verwende die Potenzreihe
109 // sin(x) = sum(j=0..inf,x*(-x^2)^j/(2j+1)!):
110 // a:=-x^2, b:=x, i:=1, sum:=0,
111 // while (/= sum (setq sum (+ sum b))) do b:=b*a/((i+1)*(i+2)), i:=i+2.
113 // Sonst setze y := x/2 = (scale-float x -1),
114 // berechne rekursiv z:=sin(y)^2 und liefere 4*z*(1-z) = 1-(1-2*z)^2.
115 // [Die Grenze sqrt(d) ergibt sich so:
116 // Man braucht bei der Potenzreihe mit x=2^-k etwa j Glieder, mit
117 // k*j*ln 2 + j*(ln j - 1) = d, und der Aufwand beträgt etwa 2.8*(j/2)
118 // Multiplikationen von d-Bit-Zahlen. Bei Halbierungen bis x=2^-k ist der
119 // Gesamtaufwand etwa 2*(k+e)+1.4*j(k). Dieses minimieren nach k: Soll sein
120 // -1.4 = d/dk j(k) = (d/dj k(j))^-1 = - j^2/(d+j)*ln 2, also j^2=2(d+j),
121 // grob j=sqrt(2d) und damit k=sqrt(d).]
122 // Aufwand: asymptotisch d^2.5 .
126 var uintL actuallen = TheLfloat(x)->len;
127 var uintL d = float_digits(x);
128 var sintL e = float_exponent(x);
129 if (e <= (-(sintL)d)>>1) // e <= (-d)/2 <==> e <= -ceiling(d/2) ?
130 return square(x); // ja -> x^2 als Ergebnis
133 // Bei e <= -1-limit_slope*floor(sqrt(d)) kann die Potenzreihe
134 // angewandt werden. limit_slope = 1.0 ist schlecht (ca. 10% zu
135 // schlecht). Ein guter Wert für naive1 ist limit_slope = 0.6,
136 // für naive3 aber limit_slope = 0.5.
137 var sintL e_limit = -1-floor(isqrt(d),2); // -1-floor(sqrt(d))
139 // e > -1-limit_slope*floor(sqrt(d)) -> muß |x| verkleinern.
140 x = scale_float(x,e_limit-e);
141 ee = e_limit; // Neuer Exponent = e_limit.
143 var cl_LF x2 = square(x); // x^2
144 // Potenzreihe anwenden:
145 var cl_LF powser_value;
146 var cl_LF a = - x2; // a := -x^2
150 // fixed-point representation
151 d = d-ee; // fixed-point representation with d mantissa bits
152 var cl_I b = round1(scale_float(x,d)); // b := x
153 var cl_I sum = 0; // sum := (float 0 x)
157 b = round1(round1(The(cl_LF)(b*a)),(cl_I)((i+1)*(i+2)));
160 powser_value = scale_float(cl_float(sum,x),-d);
161 } else if (actuallen <= 7) { // Break-even-Point before extendsqrt: N<=6
163 // floating-point representation
164 var cl_LF b = x; // b := x
165 var cl_LF sum = cl_float(0,x); // sum := (float 0 x)
167 var cl_LF new_sum = sum + b;
168 if (new_sum == sum) // = sum ?
169 break; // ja -> Potenzreihe abbrechen
171 b = (b*a)/(cl_I)((i+1)*(i+2));
177 // floating-point representation with smooth precision reduction
178 var cl_LF b = x; // b := x
179 var cl_LF eps = scale_float(b,-(sintL)d-10);
180 var cl_LF sum = cl_float(0,x); // sum := (float 0 x)
182 var cl_LF new_sum = sum + LF_to_LF(b,actuallen);
183 if (new_sum == sum) // = sum ?
184 break; // ja -> Potenzreihe abbrechen
186 b = cl_LF_shortenwith(b,eps);
187 b = (b*a)/(cl_I)((i+1)*(i+2));
192 var cl_LF z = square(powser_value); // sin^2 als Ergebnis
193 while (e > e_limit) {
194 z = cl_float(1,x) - square(cl_float(1,x) - scale_float(z,1)); // z := 1-(1-2*z)^2
199 // Bit complexity (N = length(x)): O(N^(1/2)*M(N)).
201 // Timings of the three variants, on an i486 33 MHz, running Linux,
202 // applied to x = sqrt(2)-1 = 0.414...
203 // N naive1 naive2 naive3 ratseries
204 // 4 0.0064 0.0048 0.0049 0.023
205 // 6 0.0081 0.0064 0.0065 0.031
206 // 8 0.0103 0.0085 0.0083 0.038
207 // 10 0.012 0.011 0.010 0.048
208 // 25 0.043 0.047 0.035 0.119
209 // 50 0.15 0.17 0.12 0.37
210 // 100 0.54 0.67 0.44 1.09
211 // 250 3.5 4.4 2.8 5.5
212 // 500 14.7 18.5 11.6 19.4
214 // 2500 315 361 243 261
218 // ==> ratseries faster for N >= 2750.