/usr/include/boost/math/constants
Edit: /usr/include/boost/math/constants/calculate_constants.hpp (33626B)
// Copyright John Maddock 2010, 2012.
// Copyright Paul A. Bristow 2011, 2012.
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_MATH_CALCULATE_CONSTANTS_CONSTANTS_INCLUDED
#define BOOST_MATH_CALCULATE_CONSTANTS_CONSTANTS_INCLUDED
#include
namespace boost{ namespace math{ namespace constants{ namespace detail{
template
template
inline T constant_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return ldexp(acos(T(0)), 1);
/*
// Although this code works well, it's usually more accurate to just call acos
// and access the number types own representation of PI which is usually calculated
// at slightly higher precision...
T result;
T a = 1;
T b;
T A(a);
T B = 0.5f;
T D = 0.25f;
T lim;
lim = boost::math::tools::epsilon();
unsigned k = 1;
do
{
result = A + B;
result = ldexp(result, -2);
b = sqrt(B);
a += b;
a = ldexp(a, -1);
A = a * a;
B = A - result;
B = ldexp(B, 1);
result = A - B;
bool neg = boost::math::sign(result) < 0;
if(neg)
result = -result;
if(result <= lim)
break;
if(neg)
result = -result;
result = ldexp(result, k - 1);
D -= result;
++k;
lim = ldexp(lim, 1);
}
while(true);
result = B / D;
return result;
*/
}
template
template
inline T constant_two_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
return 2 * pi > >();
}
template // 2 / pi
template
inline T constant_two_div_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
return 2 / pi > >();
}
template // sqrt(2/pi)
template
inline T constant_root_two_div_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return sqrt((2 / pi > >()));
}
template
template
inline T constant_one_div_two_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
return 1 / two_pi > >();
}
template
template
inline T constant_root_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return sqrt(pi > >());
}
template
template
inline T constant_root_half_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return sqrt(pi > >() / 2);
}
template
template
inline T constant_root_two_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return sqrt(two_pi > >());
}
template
template
inline T constant_log_root_two_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return log(root_two_pi > >());
}
template
template
inline T constant_root_ln_four::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return sqrt(log(static_cast(4)));
}
template
template
inline T constant_e::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
//
// Although we can clearly calculate this from first principles, this hooks into
// T's own notion of e, which hopefully will more accurate than one calculated to
// a few epsilon:
//
BOOST_MATH_STD_USING
return exp(static_cast(1));
}
template
template
inline T constant_half::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
return static_cast(1) / static_cast(2);
}
template
template
inline T constant_euler::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
//
// This is the method described in:
// "Some New Algorithms for High-Precision Computation of Euler's Constant"
// Richard P Brent and Edwin M McMillan.
// Mathematics of Computation, Volume 34, Number 149, Jan 1980, pages 305-312.
// See equation 17 with p = 2.
//
T n = 3 + (M ? (std::min)(M, tools::digits()) : tools::digits()) / 4;
T lim = M ? ldexp(T(1), 1 - (std::min)(M, tools::digits())) : tools::epsilon();
T lnn = log(n);
T term = 1;
T N = -lnn;
T D = 1;
T Hk = 0;
T one = 1;
for(unsigned k = 1;; ++k)
{
term *= n * n;
term /= k * k;
Hk += one / k;
N += term * (Hk - lnn);
D += term;
if(term < D * lim)
break;
}
return N / D;
}
template
template
inline T constant_euler_sqr::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return euler > >()
* euler > >();
}
template
template
inline T constant_one_div_euler::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return static_cast(1)
/ euler > >();
}
template
template
inline T constant_root_two::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return sqrt(static_cast(2));
}
template
template
inline T constant_root_three::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return sqrt(static_cast(3));
}
template
template
inline T constant_half_root_two::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return sqrt(static_cast(2)) / 2;
}
template
template
inline T constant_ln_two::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
//
// Although there are good ways to calculate this from scratch, this hooks into
// T's own notion of log(2) which will hopefully be accurate to the full precision
// of T:
//
BOOST_MATH_STD_USING
return log(static_cast(2));
}
template
template
inline T constant_ln_ten::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return log(static_cast(10));
}
template
template
inline T constant_ln_ln_two::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return log(log(static_cast(2)));
}
template
template
inline T constant_third::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return static_cast(1) / static_cast(3);
}
template
template
inline T constant_twothirds::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return static_cast(2) / static_cast(3);
}
template
template
inline T constant_two_thirds::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return static_cast(2) / static_cast(3);
}
template
template
inline T constant_three_quarters::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return static_cast(3) / static_cast(4);
}
template
template
inline T constant_sixth::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return static_cast(1) / static_cast(6);
}
// Pi and related constants.
template
template
inline T constant_pi_minus_three::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
return pi > >() - static_cast(3);
}
template
template
inline T constant_four_minus_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
return static_cast(4) - pi > >();
}
//template
//template
//inline T constant_pow23_four_minus_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
//{
// BOOST_MATH_STD_USING
// return pow(four_minus_pi > >(), static_cast(1.5));
//}
template
template
inline T constant_exp_minus_half::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return exp(static_cast(-0.5));
}
template
template
inline T constant_exp_minus_one::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return exp(static_cast(-1.));
}
template
template
inline T constant_one_div_root_two::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
return static_cast(1) / root_two > >();
}
template
template
inline T constant_one_div_root_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
return static_cast(1) / root_pi > >();
}
template
template
inline T constant_one_div_root_two_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
return static_cast(1) / root_two_pi > >();
}
template
template
inline T constant_root_one_div_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return sqrt(static_cast(1) / pi > >());
}
template
template
inline T constant_four_thirds_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return pi > >() * static_cast(4) / static_cast(3);
}
template
template
inline T constant_half_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return pi > >() / static_cast(2);
}
template
template
inline T constant_third_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return pi > >() / static_cast(3);
}
template
template
inline T constant_sixth_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return pi > >() / static_cast(6);
}
template
template
inline T constant_two_thirds_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return pi > >() * static_cast(2) / static_cast(3);
}
template
template
inline T constant_three_quarters_pi::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return pi > >() * static_cast(3) / static_cast(4);
}
template
template
inline T constant_pi_pow_e::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return pow(pi > >(), e > >()); //
}
template
template
inline T constant_pi_sqr::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return pi > >()
* pi > >() ; //
}
template
template
inline T constant_pi_sqr_div_six::compute(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC((boost::integral_constant)))
{
BOOST_MATH_STD_USING
return pi > >()
* pi > >()
/ static_cast(6); //
}
template
template