|
Boost : |
From: Ed Brey (brey_at_[hidden])
Date: 2001-04-20 17:04:11
From: "Herve Bronnimann" <hbr_at_[hidden]>
>
> Side note: it doesn't solve the case of a user doing things like
> double two_pi = 2.0 * boost::mathconstants<double>::pi();
> So if you have a short program that generates those numbers (perhaps a
> C++ template meta-code, although it's hard to imagine how to do THAT)
> it would be best if you include it in the distribution for users who
> care to be able to derive their own constants...
Hardly a side note. This is a key point for the interface design.
AFAICT, it is impossible to have a single interface that works for both:
(a) compile-time computation of expressions, and
(b) custom-binary values, which are more accurate than the compiler
can produce.
Here's my suggestion to provide the user the choice of benefits, as well
as an implementation strategy.
namespace boost {
namespace math {
template<typename T> struct constants {
static T pi() {return T(3.14...L);}
...
};
template<typename T> struct tailored_constants:
public constants {};
#ifdef PLATFORM1
template<float> struct tailored_constants {
static float pi() {union {unsigned u; float f;} n = {0x12345678};
return n.f;}
...
#endif
};
}
}
The user would code like this:
typedef boost::math::constants c;
or
typedef boost::math::tailored_constants c;
and then
r = 4./3. * c::pi() * pow(r,3)
or
r = c::pi() * r * r
depending on what the user wanted to optimize (speed or accuracy).
(Of course in the volume of sphere case, if the user wanted accuracy,
he's have to generate his own tailored constant.)
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk