[Python-Dev] FP vs. tutorial
Tim Peters
tim.one@home.com
Wed, 23 May 2001 02:31:29 -0400
[Greg Ward, on http://www.lahey.com/float.htm]
> I found this article more useful, interesting, and informative than
> whatever I learned about binary floating-point in my academic years.
> Good link, Tim. Two catches:
>
> * I can just barely follow the FORTRAN examples; I very much doubt
> the average Python newbie would have any more luck than me
The goal is to frighten them: the ones with the right stuff to use fp
without destroying a satellite, bringing down the Internet, designing a
pacemaker that fails when rounding a corner clockwise at 1.37g, causing a
small country's economy to collapse, making jet fighters spontaneously turn
upside down when crossing the equator, or triggering WW III by accident, will
persist <wink>. BTW, not all of those were made up!
> * I tried several of the FORTRAN examples in Python, and did not
> witness any of the gotchas they are meant to illustrate. Possibly
> it's just single-precision vs. double-precision difference, but
> Python 2.1 under Linux 2.2 on an Athlon compiled with gcc 2.95.2
> doesn't demonstrate the same gotchas as that article does.
You can't illustrate the last half of their examples in Python without
playing obscure games with the struct module, because they rely on the
existence of more than one size of floating-point type.
Your lack of luck with the first half of their examples is indeed solely due
to that he used single-precision examples and Python's float is double. You
need to find different numbers to show the same things in Python; like so:
# Binary Floating Point
x = 100000000000. * 0.00000000001
if x != 1.0:
print "Oops! It's %r" % x
# Inexactness
a = 98. / 49.
reciprocal = 1./49.
b = 98. * reciprocal
if a != b:
print "Oops! They're %r and %r" % (a, b)
# Crazy Conversions
x = 32.05
y = x * 100. # "looks like" 3205. if display rounded
i = int(y) # actually truncates to 3204
print y, i, repr(y)
It's Real Work coming up with stuff like that. What I'm hearing is that
people won't understand it anyway -- so screw it. If they want an education,
they can prove it by doing a google search <0.6 wink>.