[Python-Dev] 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
    >>> "%.55f" % 0.1
    >>> "%.55f" % (0.5+0.1)

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)
    >>> "%.55f" % (0.8)

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