SputArithmetic is a portable library written in C++ for doing multiple-precision arithmetic. The underlying theory and design can be found among the multiple-precision articles.
Note that the library was created mainly to support/test the theory of those articles. It has been made open source in the hope that people may find it useful. The library has a free and permissive license and it should be very easy to use because of its portability and the fact that it is header based only (depends on some Boost libraries for access to portable type traits, shared pointers, and random number generators, but they are header based also). If you look for a thoroughly tested and highly optimized multiple-precision library, you should probably look at the GNU Multiple Precision Arithmetic Library instead.
The project is hosted at SourceForge. Bug reports, suggestions for improvements, patches, and other comments are very welcome.
Download
The current release is version 0.1, obtainable from the project files page. You can also check out the development branch by browsing the source or by doing a Subversion checkout as
svn co https://sputarithmetic.svn.sourceforge.net/svnroot/sputarithmetic/trunk sputarithmetic
A Quick Introduction
The data type for non-negative integers is called NonNegativeInteger. It is a generic type and needs an unsigned integer as type parameter. A good choice is to use unsigned int:
#include "nonnegative_integer.hpp"; typedef NonNegativeInteger<unsigned int> NNI;
This type can now be used as a usual integer in many contexts. A simple example (assuming the include and typedef from above):
NNI a = 2, b; NNI c = a; b = a + NNI(5); c *= b; std::cout << "c: " << c << std::endl;
A combined division quotient and remainder method is provided so you can do things like
NNI u(18473), v(546); std::pair<NNI, NNI> qr = NNI::divide(u, v); std::cout << u << " = " << qr.first << "*" << v << " + " << qr.second << std::endl;
Apart from addition, subtraction, multiplication, and division, the NonNegativeInteger data type also supports, among others, comparison and binary operations.
The file nni_utils.hpp contains some convenience methods that can be used together with the NonNegativeInteger data type. The most useful is probably the string_to_nni method, which builds a non-negative integer from a string, e.g.,
NNI n = string_to_nni<NNI>("600851475143");
std::cout << "Number: " << n << std::endl;