Boost logo

Boost :

From: Andy Little (andy_at_[hidden])
Date: 2004-01-20 10:30:11


Hi,

    Have been looking through math-constants in order to take advantage of
the PI functions in function_constants.hpp.

Two thoughts have occurred.

1) the type of constant could be parameterised, which would help in
functions-templates:

current usege of 'pi' in function_constants:

 q_volume::m3 V1 = 4.0/3.0 * boost::math::double_constants::pi *
power<3>(r);

Alternative parameterised pi ...useful for template functions:

 q_volume::m3 V2 = 4.0/3.0 * boost::math::constants<double>::pi *
power<3>(r);
 q_volume::m3 V3 = 4.0/3.0 * boost::math::constants<float>::pi *
power<3>(r);

2) the value could be returned by const-reference.

An implementation which does both of these and shouldnt break the original:
(tested in gcc3.2 and VC7.1):

regards
Andy Little

---------------------------

#ifndef HPP_FUNCTION_CONSTANTS_BY_REF
#define HPP_FUNCTION_CONSTANTS_BY_REF

namespace boost{namespace math {

    template<typename T>
    struct constants{
        struct pi_constant
        {
            operator const T& ()const;
        };
        static pi_constant const pi;

        /* other constants */
    };

    // conv operators
    template<>
    inline constants<
        double
>::pi_constant::operator
    const double&() const
    { static const double val
        = 3.141592653589793238462643383279502884197;
        return val;
    }

    template<>
    inline constants<
        float
>::pi_constant::operator
    const float&() const
    { static const float val
        = 3.141592653589793238462643383279502884197F;
        return val;
    }

    // static members
    // initialisers reqd which is possibly the problem ? :-)

    template<>
    constants<double>::pi_constant const
    constants<double>::pi = constants<double>::pi_constant();

    template<>
    constants<float>::pi_constant const
    constants<float>::pi = constants<float>::pi_constant();

  }}// boost::math

#endif


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk