<div class="gmail_quote">On Tue, Jul 5, 2011 at 8:49 PM, Don <span dir="ltr"><<a href="mailto:nospam@nospam.com" target="_blank">nospam@nospam.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
James Fisher wrote:<div><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Tue, Jul 5, 2011 at 12:31 PM, James Fisher <<a href="mailto:jameshfisher@gmail.com" target="_blank">jameshfisher@gmail.com</a> <mailto:<a href="mailto:jameshfisher@gmail.com" target="_blank">jameshfisher@gmail.com</a><u></u>>> wrote:<br>
<br>
Sorry, I didn't state this very clearly. Multiplying the<br>
approximation of PI in std.math should yield the exact double of<br>
that approximation, as it should just involve increasing the<br>
exponent by 1. However, [double the approximation of the constant]<br>
is not necessarily equal to [the approximation of double the<br>
constant]. Does that make sense?<br>
</blockquote>
<br></div>
I understand what you're getting at, but actually multiplication by powers of 2 is always exact for binary floating point numbers.<br>
The reason is that the rounding is based on the values after the lowest bit of the _significand_. The exponent plays no role.<br>
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.<br>
<br>
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.<br>
</blockquote><div><br></div><div>Great explanation, thanks.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
(I think this is why the constants in math.d <<a href="https://github.com/D-Programming-Language/phobos/blob/master/std/math.d#L206" target="_blank">https://github.com/D-<u></u>Programming-Language/phobos/<u></u>blob/master/std/math.d#L206</a>> are each defined separately rather than in terms of each other.)<br>
</blockquote>
<br>
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.<br>
</blockquote></div><br><div>Another thing -- why are some constants defined in decimal, others in hex, and one (E) with the long 'L' suffix? And is there a significance to the number of decimal/hexadecimal places -- e.g., is this the minimum places required to ensure the closest floating point value for all common hardware accuracies?</div>
<meta http-equiv="content-type" content="text/html; charset=utf-8">