Boost logo

Boost :

Subject: [boost] Numeric constants
From: John Maddock (john_at_[hidden])
Date: 2009-01-30 05:35:17


Folks, someone's just filed a feature request asking for more numeric
constants to be added to Boost.Math (these are currently an implementation
detail, albeit a semi-documented one:
http://www.boost.org/doc/libs/1_37_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals1/constants.html),
but we know what troubles we had agreeing on anything last time this came up
for review! Even so it appears to be time to fearlessly open up this
particular can of worms again ;-)

So, it appears that there is still interest, but as you know last time we
tried this there was no agreement on interface, some folks felt a template
interface was essential:

template <class T> T pi();

leading to:

template <class T>
T circumference(T r)
{
   return 2 * boost::math::constants::pi<T>() * r;
}

While others were absolutely adament that they wouldn't use a syntax other
than:

template <class T>
T circumference(T r)
{
   return 2 * pi * r;
}

Various attempts were made to solve this conundrum via expression templates
and/or overloaded operators, but all introduced their own new issues as
well, and IMO none were wholly satisfactory (not least due to the complexity
introduced, a complexity that becomes all too apparent to the user if they
do get a compiler error due to an unsupported use case).

So.... here's what I think Paul Bristow and I might be persuaded to submit:

* A library based on the existing Boost.Math code, with pi<T>() as the main
interface / use case.
* Possibly add constants of specific precision in alternative namespaces, so
we could have:

double circumference(double r)
{
   using namspace boost::math::double_constants;
   return 2 * pi * r;
}

And that's pretty much it: no overly complex stuff, just a really simple
library with a few constants in it, that makes it really easy to add new
constants when required.

Thoughts?

Regards, John.


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