Boost logo

Boost :

From: Ed Brey (brey_at_[hidden])
Date: 2001-04-24 09:18:03


From: "Paul A. Bristow" <pbristow_at_[hidden]>

> // math_constants.hpp <<< math constants header file - the interface.
> namespace boost
> {
> namespace math_constants
> {
> extern const long double pi;
> } // namespace math_constants
> } // namespace boost

Having only a long double defined makes for more typing by users who are
using less precision. For example, someone working with floating point
would have to write "a = pi * r * r" as "a = float(pi) * r * r". The
solution should make it easy to get the precision desired. One approach
is "float pi = float(math_constants::pi);", which is fine by itself, but
doesn't scale well when working with many constants (see the later point
on multiple constants).

How is constant folding accomplished, given that the definition appears
to be out-of-line?

How would generic algorithms that do not know the desired type at coding
time be written?

> // math_constants.h <<< the definition file
> // Contains macro definitions BOOST_PI, BOOST_E ... as long doubles
> #define BOOST_PI 3.14159265358979323846264338327950288L /* pi */

What is the purpose of the macro? How is it invisioned to be used?

> cout << "pi is " << boost::math_constants::pi << endl;
> using boost::math_constants::pi; // Needed for all constants used.
> // recommended as useful documentation! whereas:
> // using namespace boost::math_constants; // exposes ALL names in
> math_constants.
> // that could cause some name collisions!

Pulling all math constants into the global namespace is indeed asking
for trouble in general, although it would be nice to allow it as it can
be practical within a function. However, it is also less than
desireable to have to perform a using directive for every constant in
use. It's a lot of code that isn't directly related to getting the job
done, and it creates a maintence problem because there is no easy way to
garbage collect as constants go out of use.

Fortunately, namespace renaming solves this problem well (although it do
esn't solve the problem with having the constants be the right types
described above).


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