I’m lecturing Python II for a Software Carpentry Bootcamp in Jan, and I thought I’d find a little example of a funny fact I’ve heard: IEEE floating point addition does not obey the associative law.
I must be spending too much time with doctors, because I didn’t try to make an example myself and started by looking it up in Google. The first example I found put it cleanly: http://www.walkingrandomly.com/?p=5380
>>> x=(0.1+0.2)+0.3 >>> y=0.1+(0.2+0.3) >>> x==y False >>> print('%.17f' %x) 0.60000000000000009 >>> print('%.17f' %y) 0.59999999999999998
This shows the rumor is true: addition is not associative. It does not seem like a big deal, though, since I usually round my numbers to one or two significant digits, and I know how to test with `np.allclose`.
The second example I found makes the problem clearer, though: http://cass-mt.pnnl.gov/docs/pubs/pnnleffects_of_floating-pointpaper.pdf
x = (17 + 1e32) - 1e32 y = 17 + (1e32 - 1e32)
Can’t `np.allclose` that, unless you know what “close” means…
Additional reading: http://www.macaulay.ac.uk/fearlus/floating-point/