Boost logo

Boost :

From: Herve Bronnimann (hbr_at_[hidden])
Date: 2001-04-20 16:18:53


>And in some cases you might not have to use any such tricks.
>I'm thinking of something like this:
>
>union fp_constant {
> unsigned int n;
> float f;
>};
>
>fp_constant internal_pi = { 0x40490fdb };
>float pi = internal_pi.f;
>
>(Digression: why is this important? It's becuase when you
>write something like "pi = 3.141592653589793238", you're
>relying on your compiler's radix conversion routine. There
>are no guarantees about how accurate the radix conversion is,
>and it has been proven to be impossible to write a radix
>conversion routine that (a) gives the best binary approximation
>to an arbitrary decimal fraction; and (b) takes constant space.
>If you know the exact binary representation for a platform,
>and if you know a trick that will let you specify it, you
>should.)

While this is true, there are algorithms for determining the closest
representable number at a given precision, and these don't necessarily
have to be represented as unions, or exotic bit-twiddling schemes Matt
was referring to in his message. A simple native representation (binary
for most computers, maybe decimal for some rarities) would suffice.
Check out:

  William D Clinger. How to Read Floating Point Numbers Accurately. In
  Proceedings of the 1990 ACM Conference on Principles of Programming
  Languages, pages 92-101.

  http://www.ccs.neu.edu/home/will/papers.html

Given that anybody can compute a pretty close approximation of their
constants if they need to, I guess the great interest of a
MathConstant library could be to have the best possible constant for
your computer. Not that you NEED to do this... but it's great if it's
available. See recents messages by Ed and Matt about this.

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...

Best,

-- 
Hervé

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