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

Precision Policies

There are two equivalent policies that effect the working precision used to calculate results, these policies both default to 0 - meaning calculate to the maximum precision available in the type being used - but can be set to other values to cause lower levels of precision to be used.

namespace boost{ namespace math{ namespace policies{

template <int N>
digits10;

template <int N>
digits2;

}}} // namespaces

As you would expect, digits10 specifies the number of decimal digits to use, and digits2 the number of binary digits. Internally, whichever is used, the precision is always converted to binary digits.

These policies are specified at compile-time, because many of the special functions use compile-time-dispatch to select which approximation to use based on the precision requested and the numeric type being used.

For example we could calculate tgamma to approximately 5 decimal digits using:

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

using namespace boost::math;
using namespace boost::math::policies;

typedef policy<digits10<5> > pol;

double t = tgamma(12, pol());

Or again using make_policy:

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

using namespace boost::math;
using namespace boost::math::policies;

double t = tgamma(12, policy<digits10<5> >());

And for a quantile of a distribution to approximately 25-bit precision:

#include <boost/math/distributions/normal.hpp>

using namespace boost::math;
using namespace boost::math::policies;

double q = quantile(
      normal_distribution<double, policy<digits2<25> > >(), 
      0.05);


PrevUpHomeNext