[Python-ideas] Disallow orderring comparison to NaN
Alexander Belopolsky
alexander.belopolsky at gmail.com
Thu Apr 28 18:12:21 CEST 2011
On Thu, Apr 28, 2011 at 4:52 AM, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
..
>> Since py3k has already made None < 0 an error, it may be reasonable
>> for float('nan') < 0 to raise an error as well (probably ValueError
>> rather than TypeError). This will not make lists with nans sortable
>> or searchable using binary search, but will make associated bugs
>> easier to find.
>>
>
Furthermore, IEEE 754 specifies exactly what I propose:
"""
IEEE 754 assigns values to all relational expressions involving NaN.
In the syntax of C , the predicate x != y is True but all others, x <
y , x <= y , x == y , x >= y and x > y, are False whenever x or y or
both are NaN, and then all but x != y and x == y are INVALID
operations too and must so signal.
"""
-- Lecture Notes on the Status of IEEE Standard 754 for Binary
Floating-Point Arithmetic by Prof. W. Kahan
http://www.cs.berkeley.edu/~wkahan/ieee754status/ieee754.ps
The problem with faithfully implementing IEEE 754 in Python is that
exceptions in IEEE standard don't have the same meaning as in Python.
IEEE 754 requires that a value is computed even when the operation
signals an exception. The program can then decide whether to
terminate computation or propagate the value. In Python, we have to
choose between raising an exception and returning the value. We
cannot have both. It appears that in most cases IEEE 754 "INVALID"
exception is treated as a terminating exception by Python and
operations that signal INVALID in IEEE 754 raise an exception in
Python. Therefore making <, >, etc. raise on NaN while keeping the
status quo for != and == would bring Python floats closer to
compliance with IEEE 754.
More information about the Python-ideas
mailing list