Boost C++ Libraries

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

This is an old version of boost. Click here for the latest version's documentation home page.
PrevUpHomeNext

Using With MPFR / GMP - a High-Precision Floating-Point Library

The special functions and tools in this library can be used with MPFR (an arbitrary precision number type based on the GMP library), either via the bindings in boost/math/bindings/mpfr.hpp, or via boost/math/bindings/mpreal.hpp.

In order to use these binings you will need to have installed MPFR plus its dependency the GMP library. You will also need one of the two supported C++ wrappers for MPFR: gmpfrxx (or mpfr_class), or mpfr-C++ (mpreal).

Unfortunately neither mpfr_class nor mpreal quite satisfy our conceptual requirements, so there is a very thin set of additional interfaces and some helper traits defined in boost/math/bindings/mpfr.hpp and boost/math/bindings/mpreal.hpp that you should use in place of including 'gmpfrxx.h' or 'mpreal.h' directly. The classes mpfr_class or mpreal are then usable unchanged once this header is included, so for example mpfr_class's performance-enhancing expression templates are preserved and fully supported by this library:

#include <boost/math/bindings/mpfr.hpp>
#include <boost/math/special_functions/gamma.hpp>

int main()
{
   mpfr_class::set_dprec(500); // 500 bit precision
   //
   // Note that the argument to tgamma is an expression template,
   // that's just fine here:
   //
   mpfr_class v = boost::math::tgamma(sqrt(mpfr_class(2)));
   std::cout << std::setprecision(50) << v << std::endl;
}

Alternatively use with mpreal would look like:

#include <boost/math/bindings/mpreal.hpp>
#include <boost/math/special_functions/gamma.hpp>

int main()
{
   mpfr::mpreal::set_precision(500); // 500 bit precision
   mpfr::mpreal v = boost::math::tgamma(sqrt(mpfr::mpreal(2)));
   std::cout << std::setprecision(50) << v << std::endl;
}

For those functions that are based upon the Lanczos approximation, the bindings defines a series of approximations with up to 61 terms and accuracy up to approximately 3e-113. This therefore sets the upper limit for accuracy to the majority of functions defined this library when used with either mpfr_class or mpreal.

There is a concept checking test program for mpfr support here and here.


PrevUpHomeNext