12 #include "cl_lfloat.h"
13 #include "cl_LF_tran.h"
15 #include "cl_integer.h"
16 #include "cl_alloca.h"
20 #define floor cln_floor
22 const cl_LF compute_exp1 (uintC len)
24 // Evaluate a sum(0 <= n < N, a(n)/b(n) * (p(0)...p(n))/(q(0)...q(n)))
25 // with appropriate N, and
26 // a(n) = 1, b(n) = 1, p(n) = 1, q(n) = n for n>0.
27 var uintC actuallen = len+1; // 1 Schutz-Digit
28 // How many terms to we need for M bits of precision? N terms suffice,
31 // <== N*(log(N)-1) > M*log(2)
32 // First approximation:
33 // N0 = M will suffice, so put N<=N0.
34 // Second approximation:
35 // N1 = floor(M*log(2)/(log(N0)-1)), slightly too small, so put N>=N1.
36 // Third approximation:
37 // N2 = ceiling(M*log(2)/(log(N1)-1)), slightly too large.
38 // N = N2+2, two more terms for safety.
39 var uintL N0 = intDsize*actuallen;
40 var uintL N1 = (uintL)(0.693147*intDsize*actuallen/(log((double)N0)-1.0));
41 var uintL N2 = (uintL)(0.693148*intDsize*actuallen/(log((double)N1)-1.0))+1;
44 var cl_I* qv = (cl_I*) cl_alloca(N*sizeof(cl_I));
45 var uintL* qsv = (uintL*) cl_alloca(N*sizeof(uintL));
47 for (n = 0; n < N; n++) {
48 init1(cl_I, qv[n]) (n==0 ? 1 : n);
50 var cl_q_series series;
52 series.qsv = (len >= 1000 && len <= 10000 ? qsv : 0); // tiny speedup
53 var cl_LF fsum = eval_rational_series(N,series,actuallen);
54 for (n = 0; n < N; n++) {
57 return shorten(fsum,len); // verkürzen und fertig
59 // Bit complexity (N = len): O(log(N)*M(N)).
61 // Timings of the above algorithm, on an i486 33 MHz, running Linux.
76 const cl_LF cl_exp1 (uintC len)
78 var uintC oldlen = TheLfloat(cl_LF_exp1)->len; // vorhandene Länge
80 return shorten(cl_LF_exp1,len);
84 // TheLfloat(cl_LF_exp1)->len um mindestens einen konstanten Faktor
85 // > 1 wachsen lassen, damit es nicht zu häufig nachberechnet wird:
86 var uintC newlen = len;
87 oldlen += floor(oldlen,2); // oldlen * 3/2
91 // gewünschte > vorhandene Länge -> muß nachberechnen:
92 cl_LF_exp1 = compute_exp1(newlen); // (exp 1)
93 return (len < newlen ? shorten(cl_LF_exp1,len) : cl_LF_exp1);