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
Comments

Note that while other language's floating point implementations share the same issues, some of them may *lie* about it, and truncate displayed numbers. This hides the issue, but doesn't resolve it.

Posted by: Simon Brunning on August 28, 2003 12:11 PM

nostalgia merchanvhs movies funny car drag video blonde boy snowboarding dem franchize boyz suckas come and try me

Posted by: Dimka on October 27, 2008 11:29 AM

Posted by: Alina_m on November 1, 2008 01:17 AM

Posted by: Alina_m on November 4, 2008 10:39 PM

Posted by: Asina on December 2, 2008 06:25 PM
Post a comment
Name:


Email Address:


URL:



Comments:


Remember info?