Boost Libraries is where the people who standardize C++ go to play. What are the advantages of using the C++ boost libraries? In short, if you develop with C++, you should have Boost.
Boost, conveniently, provides easy access to multiprecision arithmetic (or “bignums”) with the Boost.Multiprecision library. Current choices of backend are:
- pure C++ code
- GNU Multiprecision
- LibTom
If you do not want to go through the grief of installing GMP (or deal with the licensing issues), or just don’t want to package anything alongside your application at all, then you can use the first one, which is #include-only. Here is a simple example that does just that:
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
using integer = boost::multiprecision::cpp_int;
integer factorial( unsigned n )
{
integer result = 1;
while (n > 1)
{
result *= n;
n -= 1;
}
return result;
}
int main()
{
std::cout << "30! = " << factorial( 30 ) << "\n";
}
To compile that just make sure that the Boost headers are somewhere in your include path. Executing the resulting program produces:
30! = 265252859812191058636308480000000
for
loop? – Anadiplosis