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 the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

Use in non-template code

When using the math constants at your chosen fixed precision in non-template code, you can simply add a using namespace declaration, for example, using namespace boost::math::double_constants, to make the constants of the correct precision for your code visible in the current scope, and then use each constant as a simple variable - sans brackets:

#include <boost/math/constants/constants.hpp>

double area(double r)
{
   using namespace boost::math::double_constants;
   return pi * r * r;
}

Had our function been written as taking a float rather than a double, we could have written instead:

#include <boost/math/constants/constants.hpp>

float area(float r)
{
   using namespace boost::math::float_constants;
   return pi * r * r;
}

Likewise, constants that are suitable for use at long double precision are available in the namespace boost::math::long_double_constants.

You can see the full list of available constants at math_toolkit.constants.

Some examples of using constants are at constants_eg1.


PrevUpHomeNext