[Python-ideas] Disallow orderring comparison to NaN

Mike Graham mikegraham at gmail.com
Thu Apr 28 16:22:40 CEST 2011


On Thu, Apr 28, 2011 at 4:52 AM, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
> Another spin-off from the "[Python-Dev] PyObject_RichCompareBool
> identity shortcut" thread:
>
>> I would like to discuss another peculiarity of NaNs:
>>
>>>>> float('nan') < 0
>> False
>>>>> float('nan') > 0
>> False
>>
>> This property in my experience causes much more trouble than nan ==
>> nan being false.  The problem is that common sorting or binary search
>> algorithms may degenerate into infinite loops in the presence of nans.
>>  This may even happen when searching for a finite value in a large
>> array that contains a single nan.  Errors like this do happen in the
>> wild and and after chasing a bug like this programmers tend to avoid
>> nans at all costs.  Oftentimes this leads to using "magic"
>> placeholders such as 1e300 for missing data.
>>
>> 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.
Obviously this is a weird fit for Python, but so what? Python does its
best never to give you NaNs. If you've done something to get a NaN
it's because of a library bug or because you really wanted the NaNs
and should know what you're doing.



More information about the Python-ideas mailing list