Matt wrote:
Does the language have header files containing definitions of numbers
such as pi and e?
I believe some implementations used to have those, but I can't find them
in Stroustrup.
Thanks.
FWIW...
There is a history to trying to solve constants problem in an elegant
way with various solutions having being proposed and rejected on
gmane.comp.lib.boost.devel newsgroup.
The solutions fell into 4 areas IIRC:
solution1: Use a variable e.g
//some math header
extern const double pi;
Note that one shouldnt define it in the header due to the ODR rule so
it would require linking a library in the application or defining in a
source file, which was felt to be inconvenient. The other drawback was
that it cant simultaneously be float, double and long double so it will
never be right for everybody.
solution 2: use a function e.g
inline double pi()
{
return 3.141592653589793238462643383279502884197;
}
This suffers in that you cant control the floating point type and it
seems that people hate the function notation to call it. You could make
it a template of course, but you would have to provide the template
parameter when invoked.
solution 3: Use a smart object
struct pi{
template <typename T>
T pi_function(); // get pi as a T
template <typename T>
operator T() { return pi_function<T>();}
}
This may or may not be how it went. Whatever... The problem this had
was deducing the type to convert to in some situations IIRC.
solution 4: My preferred solution and the one I use, but suffers from
various of the problems above
template <typename T>
struct constant_{
static const T pi;
static const T e;
};
// convenient version for My Favourite float
#ifndef MY_FAVOURITE_FLOAT
#define MY_FAVOURITE_FLOAT double
#endif
struct constant : constant_<MY_FAVOURITE_FLOAT>{};
int main()
{
float val1 = constant_<float>::pi;
double val = constant::pi;
}
Of course this requires a library or source and visible use of
templates in the first invocation at least.
Finally there is an M_PI macro available in some versions of <cmath>
normally via its nested include <math.h>, but that is horrible...
So If anyone has any ideas .... ???
regards
Andy Little