...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
#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 number
s
member functions.
Note | |
---|---|
This type only provides standard library and |
As well as the usual conversions from arithmetic and string types, instances
of number<mpf_float<N> >
are copy constructible and assignable
from:
mpf_t
, mpz_t
,
mpq_t
.
number
wrappers
around those types: number<mpf_float<M> >
,
number<gmp_int>
,
number<gmp_rational>
.
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_float
s
have the value zero (this is the GMP
library's default behavior).
number
on this backend
move aware.
std::runtime_error
being thrown if the string can not be interpreted as a valid floating-point
number.
std::overflow_error
being thrown.
#include <boost/multiprecision/gmp.hpp> #include <boost/math/special_functions/gamma.hpp> #include <iostream> int main() { using namespace boost::multiprecision; // Operations at variable precision and limited standard library support: mpf_float a = 2; mpf_float::default_precision(1000); 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_init(f); mpf_set(f, a.backend().data()); mpf_clear(f); return 0; }