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