[Doc-SIG] Feedback on new floating point info in tutorial

Skip Montanaro skip@pobox.com (Skip Montanaro)
Mon, 11 Jun 2001 15:20:33 -0500


    Fred> Substantial additional material on floating point arithmetic in
    Fred> the tutorial, written by Tim Peters to explain why FP can fail to
    Fred> reflect the decimal world presented to the user.

I took a quick look at that appendix.  One thing that confused me a bit was
that if 0.1 is approximated by something ever-so-slightly larger than 0.1,
how is it that if you add ten of them together you wind up with a result
that is ever-so-slightly less than 1.0?  I didn't expect it to be exactly
1.0.  Other floating point naifs may be confused in the same way:

    >>> "%.55f" % 0.5
    '0.5000000000000000000000000000000000000000000000000000000'
    >>> "%.55f" % 0.1
    '0.1000000000000000055511151231257827021181583404541015625'
    >>> "%.55f" % (0.5+0.1)
    '0.5999999999999999777955395074968691915273666381835937500'

I guess the explanation is that not only can't most decimals be represented
exactly, but that summing the same approximation multiple times doesn't
always skew the error in the same direction either:

    >>> "%.55f" % (0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1)
    '0.7999999999999999333866185224906075745820999145507812500'
    >>> "%.55f" % (0.8)
    '0.8000000000000000444089209850062616169452667236328125000'

IEEE-754-is-full-of-traps-for-the-unwary-ly y'rs,

Skip