Previous
Next

The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Floating Point Numbers (No Excuses!)

(The title shamelessly mimicked from Joel Spolsky.)

If you ever happen to wonder what the hell is going on with situations like this:

 >>> print 0.1
 0.1
 >>> 0.1
 0.10000000000000001
 

please, please, please read the Python tutorial appendix: Floating Point Arithmetic: Issues and Limitations.

It's all there. Explained by a seasoned expert. If you don't understand floating point numbers after reading it, don't use floating point numbers. (At least for anything serious.)

A very important concept regarding floating points is the fact that you can't "round" the approximation for the number 0.1, that is, 0.10000000000000001, to get rid off the ugly tail; the approximation is already as accurate as possible!

[4 comments]

Filed under:

Comments:

Posted by David at 09.11.2005, 23:07

The appendix doesn't credit the seasoned expert's name. Just wondering who...

Posted by Jarno Virtanen at 10.11.2005, 08:31

Oh. That is indeed the case. I am pretty sure that it's Tim Peters who was the main author of that appendix, though I cannot find one definite confirmation right now.

Posted by masklinn at 10.11.2005, 15:22

It's hardly "all there" in my opinion. This is a quite good explanation, but the True Reference is still Goldberg's What Every Computer Scientist Should Know About Floating Point Arithmetic (http://cch.loria.fr/documentation/IEEE754/ACM/goldberg.pdf).

Much heavier mathematics and theory wise, much harder to grasp too (the paper is 44 pages), but still a must read.

Posted by Andrew Dalke at 11.11.2005, 13:51

Compare "The Absolute Minimum Every Software Developer ..." to
"What Every Computer Scientist ...". Software developers aren't computer scientists.