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 std::cout << "here 0" << std::endl;
47 BigInteger d(-314159265); // c is -314159265. The `int' literal is converted to a BigInteger.
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);
53 std::cout << "here 1" << std::endl;
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.
59 std::cout << "here 2" << std::endl;
61 std::string s2 = easyBItoString(f); // You can convert the other way too.
63 std::cout << "here 3" << std::endl;
65 std::cout << f << std::endl; // f is stringified and send to std::cout.
67 std::cout << "here 4" << std::endl;
72 * The Big Integer Library provides three kinds of operators:
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
81 * (2) Overloaded assignment operators: +=, -=, *=, /=, %=,
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.
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!!
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;
102 std::cout << "here 5" << std::endl;
104 BigInteger i(5), j(10), k;
105 // These two lines do the same thing: k is set to a BigInteger containing 15.
109 std::cout << "here 6" << std::endl;
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;
115 std::cin >> maxPower;
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
123 std::cout << "There you go. Goodbye." << std::endl;
125 } catch(char const* err) {
126 std::cout << "Sorry, the library threw an exception:\n"
134 * Here is the output of a sample run of this sample program:
136 3141592653589793238462643383279
143 How many do you want?
148 There you go. Goodbye.