Old snapshot `BigIntegerLibrary-2005.01.11.devel'; see the ChangeLog file.
[bigint/bigint.git] / sample.cc
1 /*
2 * Matt McCutchen's Big Integer Library
3 * http://mysite.verizon.net/mccutchen/bigint/
4 */
5
6 /*
7 * This sample file demonstrates the most important features of the Big Integer Library.
8 *
9 * To get started quickly with the library, imitate the code in `main' below.
10 *
11 * If you want more detail or more speed or can't find a feature here,
12 * look in the appropriate source file.  This file shows only the more ``user-friendly'' features;
13 * the other features are messier but worth learning eventually.
14 *
15 * GO FORTH and play with many-digit numbers!  (c.f. The TeXbook.)
16 */
17
18 // Standard libraries
19 #include <string>
20 #include <iostream>
21
22 // For the BigInteger class itself.
23 #include "BigInteger.hh"
24
25 // For the 4 routines `easy BI/BU <=> string' and `iostream' integration.
26 #include "BigIntegerUtils.hh"
27
28 int main() {
29         try {
30                 BigInteger a; // a is 0
31                 int b = 535;
32                 
33                 a = b; // From int to BigInteger...
34                 b = a; // ...and back, no casts required!
35                 /*
36                 * If a were too big for an int you'd get a runtime exception.  The Big Integer Library
37                 * throws C-strings (that is, `const char *'s) when something goes wrong.  It's a good
38                 * idea to catch them; the `try/catch' construct wrapping all this code is an example
39                 * of how to do this.  Some C++ compilers need a special command-line option to compile
40                 * code that uses exceptions.
41                 */
42                 
43                 BigInteger c(a); // Copy a BigInteger.
44                 
45                 std::cout << "here 0" << std::endl;
46                 
47                 BigInteger d(-314159265); // c is -314159265.  The `int' literal is converted to a BigInteger.
48                 
49                 // Ahem: that's too big to be an `int' literal (or even a `long' literal)!
50                 // Disillusion yourself now -- this won't compile.
51                 //BigInteger e(3141592653589793238462643383279);
52                 
53                 std::cout << "here 1" << std::endl;
54                 
55                 std::string s("3141592653589793238462643383279");
56                 BigInteger f = easyStringToBI(s);
57                 // Ah.  The string is converted to a BigInteger, and strings can be as long as you want.
58                 
59                 std::cout << "here 2" << std::endl;
60                 
61                 std::string s2 = easyBItoString(f); // You can convert the other way too.
62                 
63                 std::cout << "here 3" << std::endl;
64                 
65                 std::cout << f << std::endl; // f is stringified and send to std::cout.
66                 
67                 std::cout << "here 4" << std::endl;
68                 
69                 /*
70                 * Let's do some math!
71                 *
72                 * The Big Integer Library provides three kinds of operators:
73                 *
74                 * (1) Overloaded ``value'' operators: +, -, *, /, %, unary -.
75                 * Big-integer code using these operators looks identical to
76                 * code using the primitive integer types.  The operator takes
77                 * one or two BigInteger inputs and returns a BigInteger result,
78                 * which can then be assigned to a BigInteger variable or used
79                 * in an expression.
80                 *
81                 * (2) Overloaded assignment operators: +=, -=, *=, /=, %=,
82                 *     ++, --, flipSign.
83                 * Again, these are used on BigIntegers just like on ints.
84                 * They take one writable BigInteger that both provides an
85                 * operand and receives a result.  The first five also take
86                 * a second read-only operand.
87                 *
88                 * (3) ``Put-here'' operations: `add', `subtract', etc.
89                 * Use these if and only if you are concerned about performance.
90                 * They require fewer BigInteger copy-constructions and assignments
91                 * than do operators in (1) or (2).  Most take two read-only operands
92                 * and save the result in the invoked object `*this', whose previous
93                 * value is irrelevant.  `divideWithRemainder' is an exception.
94                 * <<< NOTE >>>: Put-here operations do not return a value: they don't need to!!
95                 */
96                 
97                 BigInteger g(314159), h(265);
98                 // All five ``value'' operators
99                 std::cout << (g + h) << '\n' << (g - h) << '\n' << (g * h)
100                         << '\n' << (g / h) << '\n' << (g % h) << std::endl;
101                 
102                 std::cout << "here 5" << std::endl;
103                 
104                 BigInteger i(5), j(10), k;
105                 // These two lines do the same thing: k is set to a BigInteger containing 15.
106                 k = i + j;
107                 k.add(i, j);
108                 
109                 std::cout << "here 6" << std::endl;
110                 
111                 // Let's do some heavy lifting.
112                 std::cout << "Powers of 3" << std::endl;
113                 std::cout << "How many do you want?" << std::endl;
114                 int maxPower;
115                 std::cin >> maxPower;
116                 
117                 BigUnsigned x(1), three(3);
118                 for (int power = 0; power <= maxPower; power++) {
119                         std::cout << "3^" << power << " = " << x << std::endl;
120                         x *= three; // A BigInteger assignment operator
121                 }
122                 
123                 std::cout << "There you go.  Goodbye." << std::endl;
124                 
125         } catch(char const* err) {
126                 std::cout << "Sorry, the library threw an exception:\n"
127                         << err << std::endl;
128         }
129         
130         return 0;
131 }
132
133 /*
134 * Here is the output of a sample run of this sample program:
135
136 3141592653589793238462643383279
137 314424
138 313894
139 83252135
140 1185
141 134
142 Powers of 3
143 How many do you want?
144 2
145 3^0 = 1
146 3^1 = 3
147 3^2 = 9
148 There you go.  Goodbye.
149
150 */