- Wrap some text to 80-character lines.
- Sample program: Fix the number of powers to compute so it can run without
user intervention. And increase the base from 3 to 314 to show big numbers
without ridiculously long output.
-+===================================================================+
-Matt McCutchen's Big Integer Library
+ Change Log
-Change Log
-==========
-These entries tell you what was added, fixed, or improved in each version as compared to the previous one. In case you haven't noticed, a version number roughly corresponds to the release date of that version in `YYYY.MM.DD[.N]' format, where `.N' goes `.2', `.3', etc. if there are multiple versions on the same day.
+These entries tell you what was added, fixed, or improved in each version as
+compared to the previous one. In case you haven't noticed, a version number
+roughly corresponds to the release date of that version in `YYYY.MM.DD[.N]'
+format, where `.N' goes `.2', `.3', etc. if there are multiple versions on the
+same day. The topmost version listed is the one you have.
2007.02.13
----------
---------
After months of inactivity, I fixed a bug in the `BigInteger' division routine; thanks to David Allen for reporting the bug. I also added simple routines for decimal output to `std::ostream's, and there is a demo that prints out powers of 3.
-+===================================================================+
+~~~~
-+===================================================================+
-| Big Integer Library |
-| A C++ library that does arithmetic on integers of unlimited size. |
-| Version 2006.05.01 |
-+-------------------------------------------------------------------+
-| By Matt McCutchen |
-| E-mail: hashproduct@gmail.com |
-| My Web site gateway: http://mysite.verizon.net/hashproduct/ |
-| Project Web site: http://www.kepreon.com/~matt/bigint/ |
-+===================================================================+
-
-What the Big Integer Library provides
-=====================================
-This library contains two classes, BigUnsigned and BigInteger, that represent nonnegative integers and integers, respectively, of size limited only by your computer's memory. Their capabilities include these operators:
- Math: + - * / % & | ^ unary-
- Assignment: = += -= *= /= %= &= |= ^= ++ --
- Comparison: == != < <= > >=
-In the works are the << and >> operators, integer algorithms like `gcd', and possibly a working implementation of RSA using the library.
-
-Using the features of the library
-=================================
-The file `sample.cc' explains and demonstrates the most important features of the library. I recommend that you read `sample.cc' and then run the sample program it contains. If you want to do something not shown in `sample.cc' or want more detail, read the actual header and source files, which are extensively commented.
-
-Compiling programs with the library
-===================================
-The library consists of a folder full of header files (`.hh') and source files (`.cc'). `#include' header files and compile with source files as necessary for your own programs. For those who use `make', a `Makefile' is included that compiles the source code to object files (`.o') and compiles the sample program.
-
-Bugs
-====
-The library has been tested by myself and others but is by no means bug-free. The programs you write using the library will be the best test of its correctness. I urge you to report any problems that you find, whether they come in the form of compiling trouble, mathematically inaccurate results, or runtime memory-management bloopers (which, since I use Java, are altogether too common in my C++).
-
-Keep in touch
-=============
-Feel free to e-mail me at `hashproduct@gmail.com' to report bugs or request features. When I fix the bug or add the feature, you will generally be credited by name in the source code and/or the Change Log unless you request otherwise. I am also curious as to what uses you find for the library. New versions of the library will be available at the project Web site.
-
-=====================================================================
\ No newline at end of file
+
+ C++ Big Integer Library
+ (see ChangeLog for version)
+
+ http://www.kepreon.com/~matt/bigint/
+
+ Written and maintained by Matt McCutchen <hashproduct@gmail.com>
+
+You can use this library in a C++ program to do arithmetic on integers of size
+limited only by your computer's memory. The library provides BigUnsigned and
+BigInteger classes that represent nonnegative integers and signed integers,
+respectively. Most of the C++ arithmetic operators are overloaded for these
+classes, so big-integer calculations are as easy as:
+
+ #include "BigIntegerLibrary.hh"
+
+ BigInteger a = 65536;
+ cout << (a * a * a * a * a * a * a * a);
+
+ (prints 340282366920938463463374607431768211456)
+
+The code in `sample.cc' demonstrates the most important features of the library.
+To get started quickly, read the code and explanations in that file and run it.
+If you want more detail or a feature not shown in `sample.cc', consult the
+consult the actual header and source files, which are heavily commented.
+
+Compiling programs that use the library
+---------------------------------------
+The library consists of a folder full of C++ header files (`.hh') and source
+files (`.cc'). `#include' header files and compile with source files as
+necessary for your own programs. A Makefile is included that compiles the
+library source files and the sample program and links them together; you can
+easily customize the Makefile to replace the sample with your own program, or
+you can write your own Makefile.
+
+Bugs and enhancements
+---------------------
+The library has been tested by me and others but is by no means bug-free. If
+you find a bug, please report it to me, whether it comes in the form of
+compiling trouble, a mathematically inaccurate result, or a memory-management
+blooper (since I use Java, these are altogether too common in my C++). I
+generally fix all reported bugs.
+
+You are also welcome to request enhancements, but I am unlikely to do
+substantial amounts of work on enhancements at this point. When I fix a bug you
+report or make an enhancement you request, I will generally credit you by name
+in the source code and/or the Change Log unless you request otherwise. New
+versions of the library will be available at its Web site (above).
+
+Note
+----
+I would be delighted to hear from you if you like this library and/or find a
+good use for it.
+
+Legal
+-----
+I, Matt McCutchen, the sole author of the original Big Integer Library, waive my
+copyright to it, placing it in the public domain. The library comes with
+absolutely no warranty.
+
+~~~~
/*
* Matt McCutchen's Big Integer Library
-*/
-
-/*
-* This sample program demonstrates the most important features of the Big Integer Library.
-* To get started quickly, read the code and explanations below. Then try the program out.
-*
-* 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 most important features of the Big
+* Integer Library
*/
// Standard libraries
int main() {
try {
- std::cout << "=====\nBig Integer Library Demonstration" << std::endl;
-
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.
+ * 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.
*/
BigInteger c(a); // Copy a BigInteger.
- BigInteger d(-314159265); // c is -314159265. The `int' literal is converted to a BigInteger.
+ // d is -314159265. The `int' literal is converted to a
+ // BigInteger.
+ BigInteger d(-314159265);
- // Ahem: that's too big to be an `int' literal (or even a `long' literal)!
- // Disillusion yourself now -- this won't compile.
+ // This won't compile because the number is too big to be an
+ // integer literal.
//BigInteger e(3141592653589793238462643383279);
+ // 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::string s2 = easyBItoString(f); // You can convert the other way too.
+ // You can convert the other way too.
+ std::string s2 = easyBItoString(f);
- std::cout << f << std::endl; // f is stringified and send to std::cout.
+ // f is stringified and send to std::cout.
+ std::cout << f << std::endl;
/*
* Let's do some math!
*
* The Big Integer Library provides lots of overloaded operators
* and corresponding assignment operators. So you can do `a + b'
- * with big integers just as with normal integers. The named
+ * with BigIntegers just as with normal integers. The named
* methods `add', `divideWithRemainder', etc. are more advanced
* ``put-here operations''; see `BigUnsigned.hh' for details.
*/
std::cout << (g + h) << '\n' << (g - h) << '\n' << (g * h)
<< '\n' << (g / h) << '\n' << (g % h) << std::endl;
- std::cout << "=====\nTest code" << std::endl;
+ // 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 << "314^" << power << " = " << x << std::endl;
+ x *= big314; // A BigInteger assignment operator
+ }
/*
* If you want to experiment with the library,
- * put your own test code here.
+ * you can add your own test code here.
*/
-
- /*
- * (End of test code)
- */
-
- // Let's do some heavy lifting.
- std::cout << "=====\nPowers of 3" << std::endl;
- std::cout << "How many do you want?" << std::endl;
- int maxPower;
- std::cin >> maxPower;
-
- BigUnsigned x(1), three(3);
- for (int power = 0; power <= maxPower; power++) {
- std::cout << "3^" << power << " = " << x << std::endl;
- x *= three; // A BigInteger assignment operator
- }
-
- std::cout << "There you go. Goodbye.\n=====" << std::endl;
+ // std::cout << "Beginning of custom test code:\n" << std::endl;
} catch(char const* err) {
- std::cout << "=====\nSorry, the library threw an exception:\n"
+ std::cout << "The library threw an exception:\n"
<< err << std::endl;
}
}
/*
-* Here is the output of a sample run of this sample program:
+Running the 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.
+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
*/