Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2001-04-19 14:39:02


----- Original Message -----
From: "Matt Austern" <austern_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Thursday, April 19, 2001 3:00 PM
Subject: Re: [boost] Boost.MathConstants: Review

> "Paul A. Bristow" wrote:
> >
> > > -----Original Message-----
> > > From: Ed Brey [mailto:brey_at_[hidden]]
> > > Sent: Thursday, April 19, 2001 4:19 PM
> > > To: boost_at_[hidden]
> > > Subject: [boost] Boost.MathConstants: Review
> > >
> > I am content to provide an .hpp file of this form
> >
> > > namespace boost {
> > > namespace math {
> > > template<typename T>
> > > struct constants {
> > > static T pi() {return T(3.1415...L);}
> > > };
> > > }
> > > }
> > >
> > > where a typical user experience would be documented as this:
> > >
> > > typedef boost::math::constants<float> c;
> > > std::cout << "Baseball and apple " << c::pi();
> >
> > as suggested (but alas not suggested in previous discussions!)
> >
> > BUT I am VERY keen to use the macro values because of the
> > long and tedious work in altering the generation program
> > to write this file with the constants embedded as above.
> > (And to write a validation program too!)
> > (And it leaves the file of macros useful for C programs -
> > I agree that should be at no extra cost to C++ users).
>
> Why not hide the macros away in a .cxx file somewhere? I
> don't see any good reason to put the actual numerical
> values in the header. It's just as good to have something
> like this:
> // header file
> namespace whatever {
> ...
> extern const double pi;
> ...
> }
>
> // implementation file
> namespace whatever {
> const double pi = THE_ACTUAL_NUMERICAL_VALUE;
> }

I'm not sure that's true. Just because SOME compilers can change the
representation of floating point numbers at runtime doesn't mean that all of
them can. Isn't it possible that some compiler will be able to embed a
floating constant in the source code and avoid hitting data memory to look
it up?

> If you find it reasonable to have macros, you can hide them
> away in that implementation file where they're out of sight.
>
> This strikes me as a cleaner solution, and I think you'd be
> hard pressed to find a platform where it made a noticable
> difference in performance.

Oh, maybe you just answered my question. But could you please go into a bit
more detail?

> It also has the advantage that,
> on some platforms you could initialize the numerical value
> in tricky ways that wouldn't be appropriate in a header.
> (I'm thinking of awful stuff, like using unions to control
> the exact bitwise representation.)

Mmm, delicious! I guess that having a carefully-generated exact
representation is probably more important than speed, here. But isn't there
an initialization-order issue in this case?

-Dave


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