Best arbitrary precision library for C++?

Soldato
Joined
15 Feb 2011
Posts
10,235
Location
Slough
I've started to use project euler as a source of short and interesting programming projects. While the simpler tasks are easily doable with the standard variable types some of the more complex problems will need longer integers (and probably longer decimals) than C++ can provide. I could create my own library to deal with arbitrarily long numbers but there are plenty that have been developed already. If anyone could give me recommendations as to which is the best of those available, that would be great.
 
Associate
Joined
29 Sep 2005
Posts
818
Location
St Neots / Dublin
I've used gmpxx, the C++ binding to GNU GMP for the project Euler solutions which I've done in C++, which has worked well.

If you use "#include <gmpxx.h>" and link with "-lgmp -lgmpxx" then you can use the mpz_class type for integers, mpq_class for rationals, and mpf_class for floats. They have the usual operators defined so you can usually just replace your standard types with them and it'll just work.
 
Soldato
OP
Joined
15 Feb 2011
Posts
10,235
Location
Slough
Possibly a stupid question but, since I cant seem to find any way of installing this without compiling it all myself, are there any alternatives to gmp which don't need to be compiled by me? After the nightmare that was compiling openCV and some of its other libraries I don't want to have to go through that again just to do some programming projects.

Also, I should have mentioned in the original post, but I'm running on windows 7 and hoping to use visual studios for the programming. 99% of all the installation instructions are about linux/unix for gmp.
 
Associate
Joined
29 Sep 2005
Posts
818
Location
St Neots / Dublin
On Windows probably the best supported is the Boost Multi Precision Library, which is part of Boost library. Installers for Windows are available on sourceforge : http://sourceforge.net/projects/boost/files/boost-binaries/1.59.0/

That'll give you the boost::multiprecision namespace which has cpp_int / cpp_rational / cpp_bin_float / cpp_dec_float.

It works similarly to the gmpxx library, operators are overloaded as you'd expect, etc.

Documentation here : http://www.boost.org/doc/libs/1_57_0/libs/multiprecision/doc/html/index.html
 
Back
Top Bottom