[Python-Dev] Mixing float and Decimal -- thread reboot
steve at pearwood.info
Thu Mar 25 23:05:10 CET 2010
On Thu, 25 Mar 2010 10:25:35 pm Nick Coghlan wrote:
> Steven D'Aprano wrote:
> > I'd like to turn the question around ... what algorithms are there
> > that rely on NaN == NaN being True?
> Absolutely anything that expects "x is y" to imply that "x == y". The
> builtin containers enforce this by checking identity before they
> check equality, but there are plenty of algorithms that won't.
Fair point, but I was actually thinking about mathematical algorithms.
Builtin containers may also fail with any object that violates the
expectation that identity implies equality, e.g.:
def __eq__(self, other):
def __ne__(self, other):
I don't see this as a problem -- if you choose to use such objects,
you're responsible for understanding what is going on. If you choose to
use floats, then you need to understand that NANs are weird.
Personally, I'm less concerned about sets of floats ending up with
strange combinations of NANs than I am about the possibility of
disastrous maths errors caused by allowing NANs to test as equal.
Here's a simplistic example:
def func(a, b):
if a == b:
(I say "simplistic" because it currently fails for a=b=INF.) Currently
this function will do the right thing for a, b both NANs:
>>> func(float('nan'), float('nan'))
but making NANs test as equal will cause it to give the wrong answer. I
fear that kind of error far more than the current funny behaviour of
builtin containers with NANs.
More information about the Python-Dev