]> www.ginac.de Git - cln.git/blob - examples/contfrac.cc
Enable modifying operators by default.
[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 int main (int argc, char* argv[])
15 {
16         for (int i = 1; i < argc; i++) {
17                 const char * arg = argv[i];
18                 try {
19                         // Convert argument to its internal representation:
20                         cl_R x = arg;
21                         // Check sign.
22                         if (minusp(x)) {
23                                 cout << '-';
24                                 x = -x;
25                         }
26                         cout << "[";
27                         const char* separator = "; ";
28                         for (;;) {
29                                 // Split x into integral and fractional part.
30                                 cl_R_div_t x_split = floor2(x);
31                                 cout << x_split.quotient;
32                                 x = x_split.remainder;
33                                 if (zerop(x))
34                                         break;
35                                 cout << separator;
36                                 separator = ", ";
37                                 // Invert x.
38                                 x = recip(x);
39                         }
40                         cout << ']' << endl;
41                 } catch ( const runtime_exception& ) {}
42         }
43 }