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

Stefan Krah stefan-usenet at bytereef.org
Sun Nov 8 17:26:48 CET 2009


Hi,

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


I think two main issues emerge from the brief discussion:

1. Should the comparison operators follow the 'compare' function
   from the standard?

The standard says:

"An implementation may use this operation [compare] under the covers to
 implement a closed set of comparison operations (greater than, equal, etc.)
 if desired. It need not, in this case, expose the compare operation
 itself."

So, I'd say that this supports following 'compare' as closely as possible.
In fact the operators <, <=, >, >= already follow 'compare-signal' in
their behavior (they raise for any NaN operand).


2. What is the use of == and != outside the decimal scope?

Mark mentions that Python uses == to test for set and dict memberships,
but that you cannot put decimal NaNs into sets:

'TypeError: Cannot hash a NaN value'


I want to add that Decimal('NaN') == Decimal('NaN') gives False, which
should somewhat limit the uses of == for NaNs outside the decimal realm
anyway.


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


Stefan Krah





More information about the Python-Dev mailing list