std.math.TAU

Don nospam at nospam.com
Tue Jul 5 12:49:37 PDT 2011


James Fisher wrote:
> On Tue, Jul 5, 2011 at 12:31 PM, James Fisher <jameshfisher at gmail.com 
> <mailto:jameshfisher at gmail.com>> wrote:
> 
>     Sorry, I didn't state this very clearly.  Multiplying the
>     approximation of PI in std.math should yield the exact double of
>     that approximation, as it should just involve increasing the
>     exponent by 1.  However, [double the approximation of the constant]
>     is not necessarily equal to [the approximation of double the
>     constant].  Does that make sense?

I understand what you're getting at, but actually multiplication by 
powers of 2 is always exact for binary floating point numbers.
The reason is that the rounding is based on the values after the lowest 
bit of the _significand_. The exponent plays no role.
Multiplication or division by two doesn't change the significand at all, 
only the exponent, so if the rounding was correct before, it is still 
correct after the multiplication.

Or to put it another way: PI in binary is a infinitely long string of 1s 
and zeros. Multiplying it by two only shifts the string left and right, 
it doesn't change any of the 1s to 0s, etc, so the approximation doesn't 
change either.


> (I think this is why the constants in math.d 
> <https://github.com/D-Programming-Language/phobos/blob/master/std/math.d#L206> 
> are each defined separately rather than in terms of each other.)

Hmm. I'm not sure why PI_2 and PI_4 are there. They should be defined in 
terms of PI. Probably should fix that.


More information about the Digitalmars-d mailing list