]> www.ginac.de Git - cln.git/blob - examples/contfrac.cc
* Version 1.1.9 released.
[cln.git] / examples / contfrac.cc
1 // Print the continued fraction of a real number.
2
3 // We work with real numbers and integers.
4 #include <cln/real.h>
5 #include <cln/integer.h>
6
7 // We do I/O.
8 #include <cln/io.h>
9 #include <cln/integer_io.h>
10
11 using namespace std;
12 using namespace cln;
13
14 // Our private error handling: return to the main program.
15 #include <csetjmp>
16 jmp_buf restartpoint;
17 namespace cln {
18         void cl_abort (void) { longjmp(restartpoint,1); }
19 }
20
21 int main (int argc, char* argv[])
22 {
23         for (int i = 1; i < argc; i++) {
24                 const char * arg = argv[i];
25                 if (setjmp(restartpoint))
26                         continue;
27                 // Convert argument to its internal representation:
28                 cl_R x = arg;
29                 // Check sign.
30                 if (minusp(x)) {
31                         cout << '-';
32                         x = -x;
33                 }
34                 cout << "[";
35                 const char* separator = "; ";
36                 for (;;) {
37                         // Split x into integral and fractional part.
38                         cl_R_div_t x_split = floor2(x);
39                         cout << x_split.quotient;
40                         x = x_split.remainder;
41                         if (zerop(x))
42                                 break;
43                         cout << separator;
44                         separator = ", ";
45                         // Invert x.
46                         x = recip(x);
47                 }
48                 cout << ']' << endl;
49         }
50 }