[Python-Dev] decimal.py: == and != comparisons involving NaNs

Adam Olsen rhamph at gmail.com
Fri Nov 13 19:18:45 CET 2009


On Mon, Nov 9, 2009 at 06:01, Mark Dickinson <dickinsm at gmail.com> wrote:
> Well, when running in some form of 'non-stop' mode, where (quiet) NaN
> results are supposed to be propagated to the end of a computation, you
> certainly want equality comparisons with nan just to silently return false.
> E.g., in code like:
>
> if x == 0:
>    <deal with zero special case>
> else:
>    <usual algorithm>
>
> nans should just end up in the second branch, without the programmer
> having had to think about it too hard.

if x != 0:
    <usual algorithm>
else:
    <deal with zero special case>

nans should just end up in the first branch, without the programmer
having had to think about it too hard.

There is a more consistent alternative: have all comparisons involving
NaN also return NaN, signifying they're unordered.  Let bool coercion
raise the exception.  Thus, both examples would raise an exception,
but a programmer who wants to handle NaN could do so explicitly:

temp = x == 0
if temp.isnan() or temp:
    <usual algorithm>
else:
    <deal with zero special case>

IEEE 754 is intended for a very different context.  I don't think it
makes sense to attempt literal conformance to it.


-- 
Adam Olsen, aka Rhamphoryncus


More information about the Python-Dev mailing list