|
Boost : |
From: Paul A. Bristow (pbristow_at_[hidden])
Date: 2001-05-11 06:06:22
There has been a lot of dissention about the format of my proposed
mathematical constants.
It is clear that there is no one solution that will satisfy all users.
And that there is no acceptable compromise.
I therefore propose to re-submit the constants in ALL the formats proposed,
in a separate .h and .hpp files.
1 MACROs - these will be useful to C programmers, and C++ users
who have some very special requirement.
#define BOOST_PI 31459L
NOT in a namespace. math_constants.h
2 Since naive C and C++ users (but ace physicists!)
only want const double so they can write
double area = pi * r * r;
a collection of
namespace math_double_constants
{
const double pi = 31459;
}
(This is probably 98% of use, so a template solution is
cosmetically unacceptable).
in math_double_constants.hpp
3 A collection of long double constants
namespace math_constants
{
const long double pi = 31459L;
}
in math_constants.hpp
4 A collection of templated constants in math_template_constants.hpp
namespace boost
{
namespace math
{
// Following B Stroustrup ISBN 0 201 70073 5, p 854
template<typename T> struct constants
{
public: // redundant if struct, but good documentation.
static const T pi; //
}; // template constants
template<typename T>T constants<T>::pi = static_cast<T>3.1459L; // Long
double constant but NOT specialised.
OR template<typename T>T constants<T>::pi = implicit_cast<T>3.1459L; // Not
specialised.
template<>const float constants<const float>::pi = 3.1459F; // Is
specialised for float.
template<>const double constants<const double>::pi = 3.1459; // Is
specialised for double.
template<>const long double constants<const long double>::pi = 3.1459L; //
Is specialised for long double.
// Leaving room for math functions in the math namespace?
}; // namespace math
}; // namespace boost
Question 1 - should these all be specialised for float, double and long
double, or just left unspecialised?
Question 2 = should it be static_cast<T> or implicit_cast<T>
Question 3 - should it be template<typename T> (clearer but not all
compilers yet?)
or template<class T> (less clear but always works?)
used thus:
cout << boost::math::constants<const long double>pi << endl;
Note problems with namespaces using the MS VC++6SP5 non-compiler.
I can write
using boost::math::constants;
but if I try to use it
cout << "Pi is " << constants<const double>::pi << endl; // fails
// error C2653: 'constants<double const >' : is not a class or namespace
name
using boost::math::constants<const double>::pi; // C2885
boost::math::constants<double const >::pi' : a using-declaration as a member
of a class must be a member-declaration
And forgetting the const in constants<const double> produces an inscrutable
error message.
Question: Can anyone confirm if these problems are solved in version 7?
5 constants as template functions - for the optimisation speed-freaks!
in math_function_constants.hpp
Question: how do we accomodate the reasonable wish for binary assignments
to override these definitions?
namespace boost
{
namespace math
{ // templated constants, constant functions, other math functions.
template<typename T>
struct function_constants
{
public:
static const T pi() {return T(3.14159265358979323846264338327950288L);}
static const T e() {return T(2.18...L);}
};
// Leaving room for other math functions? T Bessel(T); ...???
} // namespace math
} // namespace boost
Question: How does this fit in with namespace SI? and proposed physical
constants?
I wil of course document the rationale for whatever is agreed.
Finally, I need to have full agreement on the a protortype layout before I
carry out the not
inconsiderable work in actually creating the full files of all constants.
Paul
Dr Paul A Bristow, hetp Chromatography
Prizet Farmhouse
Kendal, Cumbria
LA8 8AB UK
+44 1539 561830
mailto:pbristow_at_[hidden]
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk