[Python-ideas] Disallow orderring comparison to NaN

MRAB python at mrabarnett.plus.com
Thu Apr 28 18:00:29 CEST 2011


On 28/04/2011 16:02, Alexander Belopolsky wrote:
> On Thu, Apr 28, 2011 at 10:22 AM, Mike Graham<mikegraham at gmail.com>  wrote:
>> 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.
>>
>> I'm -0 on this -- I really favor having NaNs behave like NaNs.
>
> .. but IEEE 754 specifies that NaNs compare as "unordered".
>
I get this (Python 3.1.2):

 >>> sorted([1, float('nan'), 0, 0])
[1, nan, 0, 0]
 >>> sorted([1, 0, float('nan'), 0])
[0, 0, 1, nan]

I think that as NaNs behave like this:

 >>> float('nan') == float('nan')
False
 >>> float('nan') < float('nan')
False
 >>> float('nan') > float('nan')
False

trying to sort them should raise an exception, just to preserve users'
sanity!

IMHO, the current behaviour just makes it look like a bug.



More information about the Python-ideas mailing list