<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Mar 7, 2014 at 2:33 PM, Chris Kaynor <span dir="ltr"><<a href="mailto:ckaynor@zindagigames.com" target="_blank">ckaynor@zindagigames.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class=""><div class="gmail_quote">
Presuming enough digits of precision, every binary float could be represented precisely in decimal. Decimal is base 10, which factors to 2 and 5, meaning all powers of 1/2 and 1/5 can be precisely represented. Binary floats can only be powers of 1/2.<br>
</div></div></div></div></blockquote><div><br></div><div>This is half true. Yes, the factors of 10 are 2 and 5. So every number that can be exactly represented in base-2 or in base-5 can also be represented exactly in base-10 (given enough digits.</div>
<div><br></div><div>The problem is that the converse is not the case. So in the case Mark presents several times:</div><div><br></div><div><div>>>> from decimal import Decimal<br>>>> a = 0.1 # store a binary float<br>
>>> Decimal(a)<br>Decimal('0.1000000000000000055511151231257827021181583404541015625')<span style="font-family:arial;font-size:small"> </span><br></div></div><div><br></div><div>That decimal number *is* an exact representation of the value referenced by 'a'. So if that's all you want, no problem exists.</div>
<div><br></div><div>But the problem that does exist is that there is *no* binary floating point representation that is exactly the same numeric value as Decimal('0.1'). This would be true, not only for float32 or float64 values that Python has (depending on the machine architecture and compilation switches), it would also be true if we used float128, or float256, or float8192 numbers.</div>
<div><br></div><div>And again, while that one example might lead you to think that decimal floating point is simply better, we don't need to go far to find a value like Fraction('1/3') that simply cannot have an exact representation in decimal either, no matter how many digits of precision we decide to use.</div>
<div><br></div></div>-- <br>Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>advocates of freedom in prisons. Intellectual property is<br>
to the 21st century what the slave trade was to the 16th.<br>
</div></div>