Old snapshot `BigIntegerLibrary-2005.01.06'; see the ChangeLog file.
[bigint/bigint.git] / sample.cc
CommitLineData
05780f4b
MM
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
b3fe29df 18// Standard libraries
05780f4b
MM
19#include <string>
20#include <iostream>
21
b3fe29df
MM
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
05780f4b
MM
28int main() {
29 try {
30 BigInteger a; // a is 0
31 int b = 535;
32
b3fe29df 33 a = b; // From int to BigInteger...
05780f4b
MM
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
b3fe29df
MM
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.
05780f4b
MM
41 */
42
b3fe29df 43 BigInteger c(a); // Copy a BigInteger.
05780f4b
MM
44
45 BigInteger d(-314159265); // c is -314159265. The `int' literal is converted to a BigInteger.
46
47 // Ahem: that's too big to be an `int' literal (or even a `long' literal)!
48 // Disillusion yourself now -- this won't compile.
49 //BigInteger e(3141592653589793238462643383279);
50
51 std::string s("3141592653589793238462643383279");
52 BigInteger f = easyStringToBI(s);
53 // Ah. The string is converted to a BigInteger, and strings can be as long as you want.
54
55 std::string s2 = easyBItoString(f); // You can convert the other way too.
56
57 std::cout << f << std::endl; // f is stringified and send to std::cout.
58
59 /*
60 * Let's do some math!
61 *
62 * The Big Integer Library provides three kinds of operators:
63 *
64 * (1) Overloaded ``value'' operators: +, -, *, /, %, unary -.
65 * Big-integer code using these operators looks identical to
66 * code using the primitive integer types. The operator takes
67 * one or two BigInteger inputs and returns a BigInteger result,
68 * which can then be assigned to a BigInteger variable or used
69 * in an expression.
70 *
71 * (2) Overloaded assignment operators: +=, -=, *=, /=, %=,
72 * ++, --, flipSign.
73 * Again, these are used on BigIntegers just like on ints.
74 * They take one writable BigInteger that both provides an
75 * operand and receives a result. The first five also take
76 * a second read-only operand.
77 *
78 * (3) ``Put-here'' operations: `add', `subtract', etc.
79 * Use these if and only if you are concerned about performance.
80 * They require fewer BigInteger copy-constructions and assignments
81 * than do operators in (1) or (2). Most take two read-only operands
82 * and save the result in the invoked object `*this', whose previous
83 * value is irrelevant. `divideWithRemainder' is an exception.
84 * <<< NOTE >>>: Put-here operations do not return a value: they don't need to!!
85 */
86
87 BigInteger g(314159), h(265);
88 // All five ``value'' operators
89 std::cout << (g + h) << '\n' << (g - h) << '\n' << (g * h)
90 << '\n' << (g / h) << '\n' << (g % h) << std::endl;
91
92 BigInteger i(5), j(10), k;
93 // These two lines do the same thing: k is set to a BigInteger containing 15.
94 k = i + j;
95 k.add(i, j);
96
97 // Let's do some heavy lifting.
98 std::cout << "Powers of 3" << std::endl;
99 std::cout << "How many do you want?" << std::endl;
100 int maxPower;
101 std::cin >> maxPower;
102
103 BigUnsigned x(1), three(3);
104 for (int power = 0; power <= maxPower; power++) {
105 std::cout << "3^" << power << " = " << x << std::endl;
106 x *= three; // A BigInteger assignment operator
107 }
108
109 std::cout << "There you go. Goodbye." << std::endl;
110
b3fe29df 111 } catch(char const* err) {
05780f4b
MM
112 std::cout << "Sorry, the library threw an exception:\n"
113 << err << std::endl;
114 }
b3fe29df 115
05780f4b
MM
116 return 0;
117}
118
119/*
120* Here is the output of a sample run of this sample program:
121
1223141592653589793238462643383279
123314424
124313894
12583252135
1261185
127134
128Powers of 3
129How many do you want?
1302
1313^0 = 1
1323^1 = 3
1333^2 = 9
134There you go. Goodbye.
135
136*/