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

Diez B. Roggisch deets at nospam.web.de
Sun Aug 3 22:05:42 CEST 2008


Jorgen Grahn schrieb:
> 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?

PHP and Java, amongst others. The implicitly apply a formatting when 
producing a string-representation.

> 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.

It sure is and I don't know too much myself. But IMHO python does 
something right when making the programmer aware of the problems that 
can appear.

Diez



More information about the Python-list mailing list