Boost logo

Boost :

Subject: Re: [boost] Boost.Math and Math Constants
From: Brent Spillner (spillner_at_[hidden])
Date: 2011-12-07 22:23:07


On Wed, 7 Dec 2011 15:33:21 Paul A. Bristow wrote:
>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.
>
>(Float and long double are similarly available in their own namespaces).

It's a little bit of work up front, but if you define a custom type
with separate overloads for each arithmetic operator/operand type
combination (i.e. *(float), *(double), +(float), +(double), etc.) you
should be able to offer a single namespace with constants that
auto-promote (or demote) to the right type in expressions. That's
also easily extensible to user-defined storage classes such as an
extended precision float, and I think it's more maintainable since the
using declaration (which is now untyped) can't get out of sync with
the usage context. To prevent ambiguous overloads (in contexts where
the constant itself is supplied as an argument to a polymorphic
function, not in expressions), you can leave just one type (perhaps
double) as implicitly castable-to, with explicit casts for the others.

That seems more Boost-y to me, at any rate.


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