[Python-ideas] [Python-Dev] The Case Against Floating Point ==

Imri Goldberg lorgandon at gmail.com
Mon Mar 17 00:13:25 CET 2008

I've given it more thought over the past few days.

Given the discussion here, and some more reading on my part, it seems to 
me that there isn't much chance for me convincing anyone to raise an 
exception on FP ==. I'm not too sure that it's the right move anyway. 
While I'll probably avoid FP == in my code, it seems to me that there 
are some cases it is useful (even given the inaccuracy of the results).

Regarding adding warnings to pychecker/pylint, I think it's a good idea. 
Probably for another mailing list though :).

Also, I considered the subject of runtime warnings as well.

Adding the relevant warnings to any static checker could be really hard 
work while warning during runtime could be a lot easier. Therefore, it 
seems worthwhile to consider this option. I didn't happen to use the 
warnings module before, so I read its documentation now (also the PEP) 
and played with it a little.

First, if a warning is generated for floating point ==, it can be turned 
off globally, or on a line-by-line basis.

Second, regarding Mark's comment on SmellyCodeWarning. I thought about 
it a bit, and it seems no joke to me. gcc has a -Wall mode, so does 
Python. Why not use it in this situation? (i.e. having some warnings not 
displayed by default.)

I think it would be interesting to consider more cases of 
'SmellyCodeWarning' in general, and adding them under some warning 
category. If there's a need for a use case, we've already got the first 
one - floating point comparisons.



Imri Goldberg
Insert Signature Here

Mark Dickinson wrote:

> On Fri, Mar 14, 2008 at 5:01 AM, Imri Goldberg <lorgandon at gmail.com 
> <mailto:lorgandon at gmail.com>> wrote:
>     Alright, I agree it's a good idea to drop the proposal to changing
>     floating point == into an epsilon compare.
>     What about issuing a warning though?
>     Consider the following course of action. It is the one with the least
>     changes:
>     == for regular floating point numbers now issues a warning, but still
>     works. This warning might be turned off. All other operators are left
>     unchanged.
>     Do you think this should be dropped as well?
> To be honest, yes.  There isn't currently a SmellyCodeWarning or
> IsThatReallyWhatYouMeanWarning in Python, and there doesn't
> seem to be a lot of precedent for warning on code constructs that
> may often be wrong but also have legitimate uses.  Most of
> the current warnings have more to do with syntactic or semantic
> changes between various versions of Python.
> But I think it would be entirely appropriate to warn about
> floating-point (in)equality checks in something like PyChecker
> or Pylint, if you can get past the technical difficulties of detecting
> floating-point comparisons statically.
>  Mark

More information about the Python-ideas mailing list