[Python-ideas] Disallow orderring comparison to NaN

Alexander Belopolsky alexander.belopolsky at gmail.com
Thu Apr 28 10:52:34 CEST 2011

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.

More information about the Python-ideas mailing list