]> www.ginac.de Git - cln.git/blob - examples/e.cc
61a733c6e4a492919f66746569837f84963dbed5
[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 <cl_number.h>
31 #include <cl_io.h>
32 #include <cl_integer.h>
33 #include <cl_integer_io.h>
34 #include <cl_float.h>
35 #include <cl_float_io.h>
36 #include <cl_real.h>
37 #include <cl_complex.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <cl_timing.h>
41 #include <math.h>
42
43 void
44 sum_exp1 (uintL a, uintL b, cl_I & first, cl_I & second)
45 {
46   switch (b - a)
47     {
48     case 1:
49       first = second = b;
50       break;
51     case 2:
52         {
53           cl_I s = (a + b) >> 1;
54           second = s * b;
55           first = second + b;
56         }
57       break;
58     default:
59       {
60         cl_I lp, lq, rp, rq, tmp;
61         uintL mid = (a + b) >> 1;
62         sum_exp1 (a, mid, lp, lq);
63         sum_exp1 (mid, b, rp, rq);
64         tmp = lp * rq;
65         first = tmp + rp; 
66         second = lq * rq; 
67       }
68       break;
69     }
70 }
71
72 void
73 const_exp1 (cl_LF & result, uintL dec)
74 {
75   uintL c = (uintL) (dec * log (10));
76   uintL n = dec;
77   uintC actuallen = (uintC)(3.321928094 * dec / intDsize);
78   n = (uintL) ((n + c) / log ((double)n));
79   n = (uintL) ((n + c) / log ((double)n));
80   n = (uintL) ((n + c) / log ((double)n));
81
82   n += 2;
83   actuallen += 2;
84
85   cout << "n = " << n << endl;
86   cout << "actuallen = " << actuallen << endl;
87   cl_I p, q;
88   sum_exp1 (0, n, p, q);
89   cout << "sum_exp1 ends ok" << endl;
90   extern cl_LF cl_I_to_LF(const cl_I&, uintC);
91   result = The(cl_LF)(cl_I_to_LF (p, actuallen) / cl_I_to_LF (q, actuallen));
92   cout << "const_exp1 returns ok" << endl;
93 }
94
95 int
96 main (int argc, char *argv[])
97 {
98   int digits = 100;
99   while (argc >= 3) {
100         if (!strcmp(argv[1],"-n")) {
101             digits = atoi(argv[2]);
102             argc -= 2; argv += 2;
103             continue;
104         }
105         break;
106     }
107     if (argc < 1)
108         exit(1);
109
110   cl_LF c1;
111   long l = digits;
112   cout << "\nCalculating exp1 to " << l << " decimals" << endl;
113   { CL_TIMING;
114     const_exp1 (c1, l);
115   }
116   { CL_TIMING;
117     cout << "@" << endl;
118     cout << c1 << endl;
119     cout << "@" << endl;
120   }
121 }