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.

45 lines
1015 B

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