You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.0 KiB

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