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

PrevUpHomeNext

Internal Floating-point Promotion Policies

Normally when evaluating a function at say float precision, maximal accuracy is assured by conducting the calculation at double precision internally, and then rounding the result. There are two policies that control whether internal promotion to a higher precision floating-point type takes place, or not:

Policy

Meaning

boost::math::policies::promote_float<B>

Indicates whether float arguments should be promoted to double precision internally: defaults to boost::math::policies::promote_float<true>

boost::math::policies::promote_double<B>

Indicates whether double arguments should be promoted to long double precision internally: defaults to boost::math::policies::promote_double<true>

Examples

Suppose we want tgamma to be evaluated without internal promotion to long double, then we could use:

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

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

// Define a new policy *not* internally promoting RealType to double:
typedef policy<
      promote_double<false>
      > my_policy;

// Call the function, applying the new policy:
double t1 = tgamma(some_value, my_policy());

// Alternatively we could use helper function make_policy,
// and concisely define everything at the call site:
double t2 = tgamma(some_value, make_policy(promote_double<false>()));

Alternatively, suppose we want a distribution to perform calculations without promoting float to double, then we could use:

#include <boost/math/distributions/normal.hpp>
using boost::math::normal_distribution;

using namespace boost::math::policies;

// Define a policy:
typedef policy<
      promote_float<false>
      > my_policy;

// Define the new normal distribution using my_policy:
typedef normal_distribution<float, my_policy> my_norm;

// Get a quantile:
float q = quantile(my_norm(), 0.05f);

PrevUpHomeNext