Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

mpfi_float

#include <boost/multiprecision/mpfi.hpp>

namespace boost{ namespace multiprecision{

template <unsigned Digits10>
class mpfi_float_backend;

typedef number<mpfi_float_backend<50> >    mpfi_float_50;
typedef number<mpfi_float_backend<100> >   mpfifloat_100;
typedef number<mpfi_float_backend<500> >   mpfifloat_500;
typedef number<mpfi_float_backend<1000> >  mpfi_float_1000;
typedef number<mpfi_float_backend<0> >     mpfi_float;

}} // namespaces

The mpfi_float_backend type is used in conjunction with number: It acts as a thin wrapper around the MPFI mpfi_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 and implementing interval arithmetic.

Type mpfi_float_backend 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 mpfi_float_50, mpfi_float_100, mpfi_float_500, mpfi_float_1000 provide arithmetic types at 50, 100, 500 and 1000 decimal digits precision respectively. The typedef mpfi_float provides a variable precision type whose precision can be controlled via the numbers member functions.

[Note] Note

This type only provides 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<mpfi_float_backend<N> > are copy constructible and assignable from:

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

Things you should know when using this type:

There are some additional non member functions for working on intervals:

template <unsigned Digits10, expression_template_option ExpressionTemplates>
number<mpfr_float_backend<Digits10>, ExpressionTemplates> lower(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& val);

Returns the lower end of the interval.

template <unsigned Digits10, expression_template_option ExpressionTemplates>
number<mpfr_float_backend<Digits10>, ExpressionTemplates> upper(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& val);

Returns the upper end of the interval.

template <unsigned Digits10, expression_template_option ExpressionTemplates>
number<mpfr_float_backend<Digits10>, ExpressionTemplates> median(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& val);

Returns the mid point of the interval.

template <unsigned Digits10, expression_template_option ExpressionTemplates>
number<mpfr_float_backend<Digits10>, ExpressionTemplates> width(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& val);

Returns the absolute width of the interval.

template <unsigned Digits10, expression_template_option ExpressionTemplates>
number<mpfi_float_backend<Digits10>, ExpressionTemplates> intersect(
  const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a,
  const number<mpfi_float_backend<Digits10>, ExpressionTemplates>&  b);

Returns the interval which is the intersection of the a and b. Returns an unspecified empty interval if there is no such intersection.

template <unsigned Digits10, expression_template_option ExpressionTemplates>
number<mpfi_float_backend<Digits10>, ExpressionTemplates> hull(
  const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a,
  const number<mpfi_float_backend<Digits10>, ExpressionTemplates>&  b);

Returns the interval which is the union of a and b.

template <unsigned Digits10, expression_template_option ExpressionTemplates>
bool overlap(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a,
             const number<mpfi_float_backend<Digits10>, ExpressionTemplates>&  b);

Returns true only if the intervals a and b overlap.

template <unsigned Digits10, expression_template_option ExpressionTemplates1, expression_template_option ExpressionTemplates2>
bool in(const number<mpfr_float_backend<Digits10>, ExpressionTemplates1>& a,
        const number<mpfi_float_backend<Digits10>, ExpressionTemplates2>&  b);

Returns true only if point a is contained within the interval b.

template <unsigned Digits10, expression_template_option ExpressionTemplates>
bool zero_in(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a);

Returns true only if the interval a contains the value zero.

template <unsigned Digits10, expression_template_option ExpressionTemplates>
bool subset(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a,
            const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& b);

Returns true only if a is a subset of b.

template <unsigned Digits10, expression_template_option ExpressionTemplates>
bool proper_subset(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a,
                   const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& b);

Returns true only if a is a proper subset of b.

template <unsigned Digits10, expression_template_option ExpressionTemplates>
bool empty(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a);

Returns true only if a is an empty interval, equivalent to upper(a) < lower(a).

template <unsigned Digits10, expression_template_option ExpressionTemplates>
bool singleton(const number<mpfi_float_backend<Digits10>, ExpressionTemplates>& a);

Returns true if lower(a) == upper(a).

MPFI example:
#include <boost/multiprecision/mpfi.hpp>
#include <boost/math/special_functions/gamma.hpp>
#include <iostream>

int main()
{
   using namespace boost::multiprecision;

   // Operations at variable precision and no numeric_limits support:
   mpfi_float a = 2;
   mpfi_float::default_precision(1000);
   std::cout << mpfi_float::default_precision() << std::endl;
   std::cout << sqrt(a) << std::endl; // print root-2

   // Operations at fixed precision and full numeric_limits support:
   mpfi_float_100 b = 2;
   std::cout << std::numeric_limits<mpfi_float_100>::digits << std::endl;
   // We can use any C++ std lib function:
   std::cout << log(b) << std::endl; // print log(2)

   // Access the underlying data:
   mpfi_t r;
   mpfi_init(r);
   mpfi_set(r, b.backend().data());

   // Construct some explicit intervals and perform set operations:
   mpfi_float_50 i1(1, 2), i2(1.5, 2.5);
   std::cout << intersect(i1, i2) << std::endl;
   std::cout << hull(i1, i2) << std::endl;
   std::cout << overlap(i1, i2) << std::endl;
   std::cout << subset(i1, i2) << std::endl;
   mpfi_clear(r);
   return 0;
}

PrevUpHomeNext