Boost logo

Boost :

Subject: Re: [boost] Boost.Math and Math Constants
From: Barend Gehrels (barend_at_[hidden])
Date: 2011-12-07 16:00:25


On 7-12-2011 16:33, Paul A. Bristow wrote:
> Boost.Math has always had an inconspicuous collection of useful math constants, mainly those that
> are used internally.
>
> The presentation of these was as a function-template, called for example, as pi<double>().
>
> using namespace boost::math::constants;
> return pi<Real>() * r * r;
>
> This leads to some rather ugly equations and is most unpopular with scientific and engineering
> users.
>
> John Maddock has now added a new cunning feature that allows the vast majority of users who just
> want a built-in double to get the constants quickly as plain variables. So in non-template code,
> users can write
>
> using boost::math::double_constants;
> double area = pi * r * r;
>
> and users can read the equations easily.

Nice. But actually I need something else too. I don't think I mailed
this earlier so I use this announcement to describe this additional
wish/suggestion shortly.

pi<Real>() is a macro/function, but there should be a structure behind.
If I want to use a templated user defined type, such as ttmath (I'm a
fan of that library), I'm stuck (unless I oversee something). Because it
boost::math::constants::pi<T> cannot be partially specialized because it
is a function.

To solve this Boost.Geometry defines a small class:

template <typename T>
struct define_pi
{
     static inline T apply()
     {
         // Default calls Boost.Math
         return boost::math::constants::pi<T>();
     }
};

and a utility function:
template <typename T>
inline T pi() { return detail::define_pi<T>::apply(); }

The structure behing can then be perfectly specialized for ttmath:

     template <ttmath::uint Exponent, ttmath::uint Mantissa>
     struct define_pi<ttmath::Big<Exponent, Mantissa> >
     {
         static inline ttmath::Big<Exponent, Mantissa> apply()
         {
             static ttmath::Big<Exponent, Mantissa> const the_pi("3.1415etc"
             return the_pi;
         }
     };

This works. But Boost.Math would define this "define_pi" structure, or
similar, this workaround would not be necessary...

Is that feasable?

> Some draft detailed docs about just these new proposals are at
>
> http://boost-sandbox.sourceforge.net/libs/math_constants/doc/html/index.html

What is the SVN repos behind? At boost sandbox I assume?

Thanks, Barend


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