2 * Matt McCutchen's Big Integer Library
3 * http://mysite.verizon.net/mccutchen/bigint/
7 * This sample file demonstrates the most important features of the Big Integer Library.
9 * To get started quickly with the library, imitate the code in `main' below.
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.
15 * GO FORTH and play with many-digit numbers! (c.f. The TeXbook.)
22 // For the BigInteger class itself.
23 #include "BigInteger.hh"
25 // For the 4 routines `easy BI/BU <=> string' and `iostream' integration.
26 #include "BigIntegerUtils.hh"
30 BigInteger a; // a is 0
33 a = b; // From int to BigInteger...
34 b = a; // ...and back, no casts required!
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.
43 BigInteger c(a); // Copy a BigInteger.
45 BigInteger d(-314159265); // c is -314159265. The `int' literal is converted to a BigInteger.
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);
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.
55 std::string s2 = easyBItoString(f); // You can convert the other way too.
57 std::cout << f << std::endl; // f is stringified and send to std::cout.
62 * The Big Integer Library provides three kinds of operators:
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
71 * (2) Overloaded assignment operators: +=, -=, *=, /=, %=,
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.
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!!
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;
92 BigInteger i(5), j(10), k;
93 // These two lines do the same thing: k is set to a BigInteger containing 15.
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;
101 std::cin >> maxPower;
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
109 std::cout << "There you go. Goodbye." << std::endl;
111 } catch(char const* err) {
112 std::cout << "Sorry, the library threw an exception:\n"
120 * Here is the output of a sample run of this sample program:
122 3141592653589793238462643383279
129 How many do you want?
134 There you go. Goodbye.