Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Using With MPFR or GMP - 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 GNU Multiple Precision Arithmetic Library), either via the bindings in boost/math/bindings/mpfr.hpp, or via boost/math/bindings/mpreal.hpp.

New projects are recommended to use Boost.Multiprecision with GMP/MPFR backend instead.

In order to use these bindings 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