[Python-ideas] [Python-Dev] Why is nan != nan?

Mark Dickinson dickinsm at gmail.com
Sat Mar 27 13:39:13 CET 2010

[Moving back to python-ideas]

On Sat, Mar 27, 2010 at 12:05 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> Certainly not. That defeats the whole purpose of NANs. I wish floating
> point calculations in Python would return NANs rather than raise the
> exceptions they do now.

I'd prefer the opposite:  that arithmetic operations and math
functions raise rather than produce nans;  this seems friendlier for
users who don't want to know or care about infinities and nans.  But I
agree that there are uses for nonstop mode.


How about default behaviour like the above, with a "from __options__
import non-stop-arithmetic" for people who want 1./0. to give infinity
and sqrt(-1) to give nan?  In order words, default behaviour roughly
corresponds to the IEEE 754 spec with the 'invalid operation',
'overflow' and 'divide-by-zero' operations trapped (exactly as the
default context in the decimal module does currently), and the
non-stop behaviour corresponds to IEEE 754 with none of those
operations trapped and returning default values.

A more elaborate proposal would allow individual control over the
three signals above.  (Full control over the 'underflow' and 'inexact'
signals isn't really feasible given C's poor support for these



More information about the Python-ideas mailing list