floating point in 2.0

Aahz Maruch aahz at panix.com
Tue Jun 5 14:04:54 EDT 2001


In article <3B1D154B.81016685 at home.net>,
Chris Barker  <chrishbarker at home.net> wrote:
>Aahz Maruch wrote:
>>
>> Actually, 2.0 *still* lies:
>> 
>> >>> 0.1
>> 0.10000000000000001
>> >>> '%.55f' % 0.1
>> '0.1000000000000000055511151231257827021181583404541015625'
>> 
>> It just lies less than it used to.
>
>What the heck are all those digits at the end? Talk about lies, you can
>be sure all those digits aren't being stored (at least not in an IEEE
>754 64bit double, which stores approx. 16 decimal digits. Approx.
>because it is really a given number of binary digits. I don't have the
>exact number off of the top of my head).

It's 52 binary bits.  The decimal 0.1 is converted to the nearest
approximation using all 52 bits.  The number you see above is the
precise conversion of that approximation back to decimal.  So yes, all
of those digits are being stored.  Note carefully that 754 doesn't store
*any* decimal digits, it only stores binary digits.  What you meant is
that up to 16 decimal digits of precision can be *converted* to 52 binary
digits with "reasonable" accuracy.  Because decimal contains a power of
two in its base, the reverse conversion *can* be made precisely.

(Did I get it right this time, Uncle Timmy?)
-- 
                      --- Aahz  <*>  (Copyright 2001 by aahz at pobox.com)

Hugs and backrubs -- I break Rule 6                 http://www.rahul.net/aahz/
Androgynous poly kinky vanilla queer het Pythonista   

"Do not taunt happy fun for loops. Do not change lists you are looping over."
--Remco Gerlich, comp.lang.python



More information about the Python-list mailing list