April 28, 2003
Floating Point Arithmetic: Issues and Limitations

Floating Point Arithmetic: Issues and Limitations

About once a week or so, someone will post to c.l.py, reporting as a bug the fact that 0.1 is showing up as 0.10000000000000001. This isn't a bug, it's just the way that floating point works. There is nothing peculiar to Python about this behaviour, either - any floating point implementation will show some variation on this behaviour. Java, RPG, C#, VB, you name it.

In a (so far unsuccessful) attempt to have this 'bug' reported less frequently, Tim Peters added this appendix to the Python Tutorial. It's worth a read whatever you code in.

In general, then, business oriented software should avoid using floating point, and should certainly never use it to store currency values. For that you shoud use Decimal or java.math.BigDecimal, or the like.

Posted to Python by Simon Brunning at April 28, 2003 05:49 PM
Post a comment

Email Address:



Remember info?