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.

65 lines
1.9 KiB

  1. // Compute decimal Archimedes' constant Pi to arbitrary accuracy.
  2. #include <cln/output.h>
  3. #include <cln/real.h>
  4. #include <cln/real_io.h>
  5. #include <ctype.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. using namespace std;
  9. using namespace cln;
  10. static void
  11. usage (ostream &os)
  12. {
  13. os << "Usage: pi [digits]\n";
  14. os << "Compute decimal Archimedes' constant Pi to arbitrary accuracy.\n\n";
  15. os << " --help display this help and exit\n";
  16. os << " --version output version information and exit\n";
  17. os << " --bibliography output recommended readings and exit\n";
  18. }
  19. int
  20. main (int argc, char * argv[])
  21. {
  22. int digits = 100;
  23. if (argc > 1) {
  24. if (argc == 2 && !strcmp(argv[1],"--help")) {
  25. usage(cout);
  26. return 0;
  27. }
  28. if (argc == 2 && !strcmp(argv[1],"--version")) {
  29. cout << "pi (cln) " << CL_VERSION_MAJOR << "." << CL_VERSION_MINOR << endl;
  30. cout << "Written by Bruno Haible." << endl;
  31. cout << endl;
  32. cout << "Copyright (C) 1998-2001 Bruno Haible." << endl;
  33. cout << "This is free software; see the source for copying conditions. There is NO" << endl;
  34. cout << "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." << endl;
  35. cout << endl;
  36. return 0;
  37. }
  38. if (argc == 2 && !strcmp(argv[1],"--bibliography")) {
  39. cout << "Recommended readings:\n";
  40. cout << "\"Pi\", by Joerg Arndt and Christoph Haenel (1999)\n";
  41. cout << "\"Pi: A Source Book\" by Lennart Berggren, Jonathan Borwein, Peter Borwein (1997)" << endl;
  42. return 0;
  43. }
  44. if (argc == 2 && isdigit(argv[1][0])) {
  45. digits = atoi(argv[1]);
  46. } else {
  47. usage(cerr);
  48. return 1;
  49. }
  50. }
  51. cl_F p = pi(float_format(digits));
  52. // make CLN believe this number has default_float_format to suppress
  53. // exponent marker which would be quite boring for 3.1416...
  54. cl_print_flags cpf;
  55. cpf.default_float_format = float_format(p);
  56. print_real(cout, cpf, p);
  57. cout << endl;
  58. return 0;
  59. }