]> www.ginac.de Git - cln.git/blob - examples/e.cc
Fix crashes in find_univpoly_ring and related functions
[cln.git] / examples / e.cc
1 /*
2  * The following program was used to compute the first 100,000,000 decimal
3  * digits of e = exp(1), on December 18-20, 1998.
4  * Timings on a Sun UltraSparc-II (296 MHz), running Solaris 2.6, equipped
5  * with 512 MB RAM and 2 GB swap:
6  *
7  * 100 digits:
8  *   computation of e:      real time:     0.002 s, run time:     0.000 s
9  *   conversion to decimal: real time:     0.003 s, run time:     0.000 s
10  * 1000 digits:
11  *   computation of e:      real time:     0.018 s, run time:     0.020 s
12  *   conversion to decimal: real time:     0.028 s, run time:     0.020 s
13  * 10000 digits:
14  *   computation of e:      real time:     0.488 s, run time:     0.480 s
15  *   conversion to decimal: real time:     1.059 s, run time:     1.060 s
16  * 100000 digits:
17  *   computation of e:      real time:     8.139 s, run time:     8.010 s
18  *   conversion to decimal: real time:    16.593 s, run time:    16.540 s
19  * 1000000 digits:
20  *   computation of e:      real time:   122.383 s, run time:   121.020 s
21  *   conversion to decimal: real time:   252.524 s, run time:   250.760 s
22  * 10000000 digits:
23  *   computation of e:      real time:  2152.061 s, run time:  2056.430 s
24  *   conversion to decimal: real time:  3579.670 s, run time:  3388.990 s
25  * 100000000 digits:
26  *   computation of e:      real time: 40061.367 s, run time: 30449.630 s
27  *   conversion to decimal: real time: 54507.003 s, run time: 40063.510 s
28  */
29
30 #include <cln/number.h>
31 #include <cln/io.h>
32 #include <cln/integer.h>
33 #include <cln/integer_io.h>
34 #include <cln/float.h>
35 #include <cln/float_io.h>
36 #include <cln/real.h>
37 #include <cln/complex.h>
38 #include <cstring>
39 #include <cln/timing.h>
40 #include <cmath>
41
42 using namespace std;
43 using namespace cln;
44
45 void
46 sum_exp1 (uintL a, uintL b, cl_I & first, cl_I & second)
47 {
48   switch (b - a)
49     {
50     case 1:
51       first = second = b;
52       break;
53     case 2:
54         {
55           cl_I s = (a + b) >> 1;
56           second = s * b;
57           first = second + b;
58         }
59       break;
60     default:
61       {
62         cl_I lp, lq, rp, rq, tmp;
63         uintL mid = (a + b) >> 1;
64         sum_exp1 (a, mid, lp, lq);
65         sum_exp1 (mid, b, rp, rq);
66         tmp = lp * rq;
67         first = tmp + rp; 
68         second = lq * rq; 
69       }
70       break;
71     }
72 }
73
74 namespace cln {
75   extern cl_LF cl_I_to_LF(const cl_I&, uintC);
76 }
77
78 void
79 const_exp1 (cl_LF & result, uintL dec)
80 {
81   uintL c = (uintL) (dec * ::log (10));
82   uintL n = dec;
83   uintC actuallen = (uintC)(3.321928094 * dec / intDsize);
84   n = (uintL) ((n + c) / ::log ((double)n));
85   n = (uintL) ((n + c) / ::log ((double)n));
86   n = (uintL) ((n + c) / ::log ((double)n));
87
88   n += 2;
89   actuallen += 2;
90
91   cout << "n = " << n << endl;
92   cout << "actuallen = " << actuallen << endl;
93   cl_I p, q;
94   sum_exp1 (0, n, p, q);
95   cout << "sum_exp1 ends ok" << endl;
96   result = The(cl_LF)(cl_I_to_LF (p, actuallen) / cl_I_to_LF (q, actuallen));
97   cout << "const_exp1 returns ok" << endl;
98 }
99
100 int
101 main (int argc, char *argv[])
102 {
103   int digits = 100;
104   while (argc >= 3) {
105         if (!strcmp(argv[1],"-n")) {
106             digits = atoi(argv[2]);
107             argc -= 2; argv += 2;
108             continue;
109         }
110         break;
111     }
112     if (argc < 1)
113         return(1);
114
115   cl_LF c1;
116   long l = digits;
117   cout << "\nCalculating exp1 to " << l << " decimals" << endl;
118   { CL_TIMING;
119     const_exp1 (c1, l);
120   }
121   { CL_TIMING;
122     cout << "@" << endl;
123     cout << c1 << endl;
124     cout << "@" << endl;
125   }
126   return(0);
127 }