]> www.ginac.de Git - cln.git/blob - examples/pi.cc
Fix floating-point input from decimal string.
[cln.git] / examples / pi.cc
1 // Compute decimal Archimedes' constant Pi to arbitrary accuracy.
2
3 #include <cln/output.h>
4 #include <cln/real.h>
5 #include <cln/real_io.h>
6 #include <cln/version.h>
7 #include <cctype>
8 #include <cstdlib>
9 #include <cstring>
10 #include <sstream>
11
12 using namespace std;
13 using namespace cln;
14
15 static void
16 usage (ostream &os)
17 {
18         os << "Usage: pi [digits]\n";
19         os << "Compute decimal Archimedes' constant Pi to arbitrary accuracy.\n\n";
20         os << "      --help                 display this help and exit\n";
21         os << "      --version              output version information and exit\n";
22         os << "      --bibliography         output recommended readings and exit\n";
23 }
24
25 int
26 main (int argc, char * argv[])
27 {
28         long digits = 100;
29         if (argc > 1) {
30                 if (argc == 2 && !strcmp(argv[1],"--help")) {
31                         usage(cout);
32                         return 0;
33                 }
34                 if (argc == 2 && !strcmp(argv[1],"--version")) {
35                         cout << "pi (CLN "
36                              << version_major << "." << version_minor << "." << version_patchlevel
37                              << ")" << endl;
38                         cout << "Written by Bruno Haible and Richard B. Kreckel." << endl;
39                         cout << endl;
40                         cout << "Copyright (C) 1988-2008 Bruno Haible, 2000-2009 Richard B. Kreckel." << endl;
41                         cout << "This is free software; see the source for copying conditions.  There is NO" << endl;
42                         cout << "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." << endl;
43                         cout << endl;
44                         return 0;
45                 }
46                 if (argc == 2 && !strcmp(argv[1],"--bibliography")) {
47                         cout << "Recommended readings:\n";
48                         cout << "\"Pi\", by Joerg Arndt and Christoph Haenel (1999)\n";
49                         cout << "\"Pi: A Source Book\" by Lennart Berggren, Jonathan Borwein, Peter Borwein (1997)" << endl;
50                         return 0;
51                 }
52                 if (argc == 2 && isdigit(argv[1][0])) {
53                         digits = atol(argv[1]);
54                 } else {
55                         usage(cerr);
56                         return 1;
57                 }
58         }
59
60         if (digits) {
61                 cl_F p = pi(float_format(digits));
62                 // make CLN believe this number has default_float_format to suppress
63                 // exponent marker which would be quite boring for 3.1416...
64                 cl_print_flags cpf;
65                 cpf.default_float_format = float_format(p);
66                 // We cannot print directly because people get dazed and confused
67                 // when they see gratuitous digits (Debian Bug #286266.) And we
68                 // must not "fix" the output routine because print-read consistency
69                 // is at stake. As a workaround, print into a buffer so we can chop
70                 // off characters from its end.
71                 stringstream buf;
72                 print_real(buf, cpf, p);
73                 istreambuf_iterator<char> i = buf.rdbuf();
74                 while (--digits+2>0)
75                         cout << *(i++);
76         }
77         cout << endl;
78
79         return 0;
80 }