[Python-Dev] decimal.py: == and != comparisons involving NaNs

Raymond Hettinger python at rcn.com
Sun Nov 8 19:53:10 CET 2009

[Stefan Krah]
> in a (misguided) bugreport (http://bugs.python.org/issue7279) I was
> questioning the reasons for allowing NaN comparisons with == and !=
> rather than raising InvalidOperation.

Do you have any actual use case issues or are these theoretical musings?
I ask only because a good use case might suggest the best way to adapt
the standard to the regular python api for equality/inequality operators.

NaNs are odd ducks.  They are unique in violating our basic notions
of equality (any relation that is reflexsive, symmetric, and transitive).
Once you use them in a context that goes beyond the decimal spec,
it is no surprise that you run into difficulties where NaNs don't fit
very well (because they violate basic assumptions and invariants in other code).

> Are there cases where == and != are actually needed to give a result
> for NaNs?

I would say that anywhere someone needs the full behaviors specified by
the standard, they need to use the actual compare() method which
allows for a decimal context to be specified and allows for more than
just a true/false return value (i.e. a NaN is a possible result).


More information about the Python-Dev mailing list