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

Mark Dickinson dickinsm at gmail.com
Thu Mar 13 17:08:56 CET 2008


(with apologies for the random extra level of quoting in the below...)


> On Thu, Mar 13, 2008 at 11:09 AM, Imri Goldberg <lorgandon at gmail.com>
> wrote:
>
> As I said earlier, I'd like static checkers (like Python-Lint) to catch
> > this sort of cases, whatever the decision may be.
> >
>
Hmm.  Isn't that tricky?  How does the static checker decide
whether the objects being compared are floats?  I guess one could
be content with catching some cases where the operands to ==
are clearly floats...  Wouldn't you have to have run-time warnings
to be really sure of catching all the cases?


> > It's already too late for Python 3.0.
> > Still, I believe it is worth discussing.
> >
>

Sure.  I didn't mean that to come out in quite the dismissive way it did :).
Apologies.  Maybe a PEP aimed at Python 4.0 is in order.  If you're open
to the idea of just having some way to enable warnings, it could be
much sooner.


> > While checking against a==0.0 (and other similar conditions) before
> > dividing will indeed protect from outright division by zero, it will
> > enlarge any error you will have in the computation. I guess it would be
> > better to do the same check for 'a is small' for appropriate values of
> > 'small'.
>
>
Still, a check for 0.0 is good enough in some cases:  if a is tiny, the
large intermediate values may appear and then disappear happily
before giving a sensible final result.  These are usually the sort
of cases where just having division by 0.0 return an infinity
would have "just worked" too (making the whole "if" redundant), but
that's not (currently!) an option in Python.

It's a truism that floating-point equality tests should be avoided, but
it's just not true that floating-point equality testing is *always* wrong,
and I don't think that Python should make it so.

Actually, one of the reasons I thought about this subject in the first
> > place, was dict lookup for floating point numbers. It seems to me that
> > it's something you just shouldn't do.
> >
>
So your proposal would presumably include making

  x in dict

and

  x not in dict

errors for any float x, regardless of the contents of the dictionary
(or list, or set, or frozenset, or...) dict?

What would you do about Decimals? A Decimal is just another
floating point format (albeit base 10 instead of base 2); so
presumably all these warnings/errors should apply equally
to Decimal instances?  If not, why not?

I'm not trying to be negative here---as Aahz says, this is an
interesting idea;  I'm just trying to understand exactly how
things might work.

Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20080313/a31d4e07/attachment.html>


More information about the Python-ideas mailing list