-/*
-* Matt McCutchen's Big Integer Library
-* http://mysite.verizon.net/mccutchen/bigint/
-*/
-
-/*
-* This sample file demonstrates the most important features of the Big Integer Library.
-*
-* To get started quickly with the library, imitate the code in `main' below.
-*
-* If you want more detail or more speed or can't find a feature here,
-* look in the appropriate source file. This file shows only the more ``user-friendly'' features;
-* the other features are messier but worth learning eventually.
-*
-* GO FORTH and play with many-digit numbers! (c.f. The TeXbook.)
-*/
+// Sample program demonstrating the use of the Big Integer Library.
// Standard libraries
#include <string>
#include <iostream>
-// For the BigInteger class itself.
-#include "BigInteger.hh"
-
-// For the 4 routines `easy BI/BU <=> string' and `iostream' integration.
-#include "BigIntegerUtils.hh"
+// `BigIntegerLibrary.hh' includes all of the library headers.
+#include "BigIntegerLibrary.hh"
int main() {
+ /* The library throws `const char *' error messages when things go
+ * wrong. It's a good idea to catch them using a `try' block like this
+ * one. Your C++ compiler might need a command-line option to compile
+ * code that uses exceptions. */
try {
BigInteger a; // a is 0
int b = 535;
-
- a = b; // From int to BigInteger...
- b = a; // ...and back, no casts required!
- /*
- * If a were too big for an int you'd get a runtime exception. The Big Integer Library
- * throws C-strings (that is, `const char *'s) when something goes wrong. It's a good
- * idea to catch them; the `try/catch' construct wrapping all this code is an example
- * of how to do this. Some C++ compilers need a special command-line option to compile
- * code that uses exceptions.
- */
-
+
+ /* Any primitive integer can be converted implicitly to a
+ * BigInteger. */
+ a = b;
+
+ /* The reverse conversion requires a method call (implicit
+ * conversions were previously supported but caused trouble).
+ * If a were too big for an int, the library would throw an
+ * exception. */
+ b = a.toInt();
+
BigInteger c(a); // Copy a BigInteger.
-
- std::cout << "here 0" << std::endl;
-
- BigInteger d(-314159265); // c is -314159265. The `int' literal is converted to a BigInteger.
-
- // Ahem: that's too big to be an `int' literal (or even a `long' literal)!
- // Disillusion yourself now -- this won't compile.
+
+ // The int literal is converted to a BigInteger.
+ BigInteger d(-314159265);
+
+ /* This won't compile (at least on 32-bit machines) because the
+ * number is too big to be a primitive integer literal, and
+ * there's no such thing as a BigInteger literal. */
//BigInteger e(3141592653589793238462643383279);
-
- std::cout << "here 1" << std::endl;
-
+
+ // Instead you can convert the number from a string.
std::string s("3141592653589793238462643383279");
- BigInteger f = easyStringToBI(s);
- // Ah. The string is converted to a BigInteger, and strings can be as long as you want.
-
- std::cout << "here 2" << std::endl;
-
- std::string s2 = easyBItoString(f); // You can convert the other way too.
-
- std::cout << "here 3" << std::endl;
-
- std::cout << f << std::endl; // f is stringified and send to std::cout.
-
- std::cout << "here 4" << std::endl;
-
- /*
- * Let's do some math!
- *
- * The Big Integer Library provides three kinds of operators:
- *
- * (1) Overloaded ``value'' operators: +, -, *, /, %, unary -.
- * Big-integer code using these operators looks identical to
- * code using the primitive integer types. The operator takes
- * one or two BigInteger inputs and returns a BigInteger result,
- * which can then be assigned to a BigInteger variable or used
- * in an expression.
- *
- * (2) Overloaded assignment operators: +=, -=, *=, /=, %=,
- * ++, --, flipSign.
- * Again, these are used on BigIntegers just like on ints.
- * They take one writable BigInteger that both provides an
- * operand and receives a result. The first five also take
- * a second read-only operand.
- *
- * (3) ``Put-here'' operations: `add', `subtract', etc.
- * Use these if and only if you are concerned about performance.
- * They require fewer BigInteger copy-constructions and assignments
- * than do operators in (1) or (2). Most take two read-only operands
- * and save the result in the invoked object `*this', whose previous
- * value is irrelevant. `divideWithRemainder' is an exception.
- * <<< NOTE >>>: Put-here operations do not return a value: they don't need to!!
- */
-
+ BigInteger f = stringToBigInteger(s);
+
+ // You can convert the other way too.
+ std::string s2 = bigIntegerToString(f);
+
+ // f is implicitly stringified and sent to std::cout.
+ std::cout << f << std::endl;
+
+ /* Let's do some math! The library overloads most of the
+ * mathematical operators (including assignment operators) to
+ * work on BigIntegers. There are also ``copy-less''
+ * operations; see `BigUnsigned.hh' for details. */
+
+ // Arithmetic operators
BigInteger g(314159), h(265);
- // All five ``value'' operators
- std::cout << (g + h) << '\n' << (g - h) << '\n' << (g * h)
- << '\n' << (g / h) << '\n' << (g % h) << std::endl;
-
- std::cout << "here 5" << std::endl;
-
- BigInteger i(5), j(10), k;
- // These two lines do the same thing: k is set to a BigInteger containing 15.
- k = i + j;
- k.add(i, j);
-
- std::cout << "here 6" << std::endl;
-
- // Let's do some heavy lifting.
- std::cout << "Powers of 3" << std::endl;
- std::cout << "How many do you want?" << std::endl;
- int maxPower;
- std::cin >> maxPower;
-
- BigUnsigned x(1), three(3);
+ std::cout << (g + h) << '\n'
+ << (g - h) << '\n'
+ << (g * h) << '\n'
+ << (g / h) << '\n'
+ << (g % h) << std::endl;
+
+ // Bitwise operators
+ BigUnsigned i(0xFF0000FF), j(0x0000FFFF);
+ // The library's << operator recognizes base flags.
+ std::cout.flags(std::ios::hex | std::ios::showbase);
+ std::cout << (i & j) << '\n'
+ << (i | j) << '\n'
+ << (i ^ j) << '\n'
+ // Shift distances are ordinary unsigned ints.
+ << (j << 21) << '\n'
+ << (j >> 10) << '\n';
+ std::cout.flags(std::ios::dec);
+
+ // Let's do some heavy lifting and calculate powers of 314.
+ int maxPower = 10;
+ BigUnsigned x(1), big314(314);
for (int power = 0; power <= maxPower; power++) {
- std::cout << "3^" << power << " = " << x << std::endl;
- x *= three; // A BigInteger assignment operator
+ std::cout << "314^" << power << " = " << x << std::endl;
+ x *= big314; // A BigInteger assignment operator
}
-
- std::cout << "There you go. Goodbye." << std::endl;
-
+
+ // Some big-integer algorithms (albeit on small integers).
+ std::cout << gcd(BigUnsigned(60), 72) << '\n'
+ << modinv(BigUnsigned(7), 11) << '\n'
+ << modexp(BigUnsigned(314), 159, 2653) << std::endl;
+
+ // Add your own code here to experiment with the library.
} catch(char const* err) {
- std::cout << "Sorry, the library threw an exception:\n"
+ std::cout << "The library threw an exception:\n"
<< err << std::endl;
}
-
+
return 0;
}
/*
-* Here is the output of a sample run of this sample program:
+The original sample program produces this output:
3141592653589793238462643383279
314424
83252135
1185
134
-Powers of 3
-How many do you want?
-2
-3^0 = 1
-3^1 = 3
-3^2 = 9
-There you go. Goodbye.
+0xFF
+0xFF00FFFF
+0xFF00FF00
+0x1FFFE00000
+0x3F
+314^0 = 1
+314^1 = 314
+314^2 = 98596
+314^3 = 30959144
+314^4 = 9721171216
+314^5 = 3052447761824
+314^6 = 958468597212736
+314^7 = 300959139524799104
+314^8 = 94501169810786918656
+314^9 = 29673367320587092457984
+314^10 = 9317437338664347031806976
+12
+8
+1931
*/