Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.


#include <boost/multiprecision/gmp.hpp>

namespace boost{ namespace multiprecision{

template <unsigned Digits10>
class gmp_float;

typedef number<gmp_float<50> >    mpf_float_50;
typedef number<gmp_float<100> >   mpf_float_100;
typedef number<gmp_float<500> >   mpf_float_500;
typedef number<gmp_float<1000> >  mpf_float_1000;
typedef number<gmp_float<0> >     mpf_float;

}} // namespaces

The gmp_float back-end is used in conjunction with number : it acts as a thin wrapper around the GMP mpf_t to provide an real-number type that is a drop-in replacement for the native C++ floating-point types, but with much greater precision.

Type gmp_float can be used at fixed precision by specifying a non-zero Digits10 template parameter, or at variable precision by setting the template argument to zero. The typedefs mpf_float_50, mpf_float_100, mpf_float_500, mpf_float_1000 provide arithmetic types at 50, 100, 500 and 1000 decimal digits precision respectively. The typedef mpf_float provides a variable precision type whose precision can be controlled via the numbers member functions.

[Note] Note

This type only provides standard library and numeric_limits support when the precision is fixed at compile time.

As well as the usual conversions from arithmetic and string types, instances of number<mpf_float<N> > are copy constructible and assignable from:

It's also possible to access the underlying mpf_t via the data() member function of gmp_float.

Things you should know when using this type:

GMP example:

#include <boost/multiprecision/gmp.hpp>

using namespace boost::multiprecision;

// Operations at variable precision and limited standard library support:
mpf_float a = 2;
std::cout << mpf_float::default_precision() << std::endl;
std::cout << sqrt(a) << std::endl; // print root-2

// Operations at fixed precision and full standard library support:
mpf_float_100 b = 2;
std::cout << std::numeric_limits<mpf_float_100>::digits << std::endl;
// We can use any C++ std lib function:
std::cout << log(b) << std::endl; // print log(2)
// We can also use any function from Boost.Math:
std::cout << boost::math::tgamma(b) << std::endl;
// These even work when the argument is an expression template:
std::cout << boost::math::tgamma(b * b) << std::endl;

// Access the underlying representation:
mpf_t f;
mpf_set(f, a.backend().data());