]> www.ginac.de Git - cln.git/blob - examples/contfrac.cc
* src/base/digitseq/cl_asm.h: Test if (intDsize==32) for MIPS and HPPA,
[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 cln;
12
13 // Our private error handling: return to the main program.
14 #include <setjmp.h>
15 jmp_buf restartpoint;
16 namespace cln {
17         void cl_abort (void) { longjmp(restartpoint,1); }
18 }
19
20 int main (int argc, char* argv[])
21 {
22         for (int i = 1; i < argc; i++) {
23                 const char * arg = argv[i];
24                 if (setjmp(restartpoint))
25                         continue;
26                 // Convert argument to its internal representation:
27                 cl_R x = arg;
28                 // Check sign.
29                 if (minusp(x)) {
30                         stdout << '-';
31                         x = -x;
32                 }
33                 fprint(stdout, "[");
34                 const char* separator = "; ";
35                 for (;;) {
36                         // Split x into integral and fractional part.
37                         cl_R_div_t x_split = floor2(x);
38                         stdout << x_split.quotient;
39                         x = x_split.remainder;
40                         if (zerop(x))
41                                 break;
42                         stdout << separator;
43                         separator = ", ";
44                         // Invert x.
45                         x = recip(x);
46                 }
47                 stdout << ']' << std::endl;
48         }
49 }