Boost logo

Boost :

From: Petr Kocmid (pkocmid_at_[hidden])
Date: 2001-02-11 05:56:26


Hi Paul,

> From: Paul A. Bristow [mailto:pbristow_at_[hidden]]
> 7 static_cast ing provides the way for users to get what they want.
> In practice, I cannot foresee wanting more than one type at a time.
> Either one is doing the calculations in float, double or long double,
> but not both piFloat and piDouble at the same time. So other suggestions
> made are not sufficiently useful here to justify their complexity.

I feel limiting to one fp representation is problematic. Consider a 3D
visualisation application, doing RT world geometry transformations in floats
(as certain API needs) and some math computation (RT signal processing) in
doubles (as another API needs) at the same time. Also, customer made numeric
types are often needed in addition, often on platforms which do not have
floating point hw at all (an extreme example: Hubble scope is controled by
286 cpu...).

I think a solution with no complexity for you would be to wrap all your
constants as statics to template class (otherwise empty, so no overhead (in
case of floats and doubles)), and define constant specialisations for them.
The class is seen by library user as a wraping namespace, not an object.

template <typename Precision> class math_const {
public:
        typedef Precision PrecisionType; // publish the type for possible reuse
        static const Precision pi;
};

template<> const float math_const<float>::pi = YOUR_PI_DEFINITION;

template<> const double math_const<double>::pi = YOUR_PI_DEFINITION;

Usage should be quite pleasing the eyes of even the boost purists:
cout << math_const<float>::pi << math_const<double>::pi;

Not mention the extensibility of the mechanism to custom numeric types. This
could be done with macro:
#define INSTANTIATE_PI(TYPE) template<> const TYPE math_const<TYPE>::pi =
YOUR_PI_DEFINITION

INSTANTIATE_PI(float);
INSTANTIATE_PI(fixed<int,int>);

In later case, if fixed<int,int> suppplies initialization from double, your
constants will work with fixed<int,int> too.

Petr Kocmid
pkocmid_at_[hidden]


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