Decimals not equalling themselves (e.g. 0.2 = 0.2000000001)

Jorgen Grahn grahn+nntp at snipabacken.se
Sun Aug 3 15:57:09 EDT 2008


On Sun, 03 Aug 2008 16:50:22 +0200, Diez B. Roggisch <deets at nospam.web.de> wrote:
> CNiall schrieb:
...
>>  >>> 0.2
>> 0.20000000000000001
...

> Welcome to the wonderful world of IEEE754. Just because other languages 
> shield you from the gory details they still are there. Python chose to 
> not do that, instead showing the rounding errors introduced and making 
> the developer decide how to deal with these.

Which other languages try to hide how floating-point numbers behave?

The correct way of dealing with this (you probably agree) is never to
expect infinite precision from floats, and design your code/algorithms
accordingly -- never use "if f1==f2:" and so on.

Floating-point is a tricky area. Lots of programmers (including me)
know too little about it.

/Jorgen

-- 
  // Jorgen Grahn <grahn@        Ph'nglui mglw'nafh Cthulhu
\X/     snipabacken.se>          R'lyeh wgah'nagl fhtagn!



More information about the Python-list mailing list