float("nan") in set or as key

Chris Torek nospam at torek.net
Mon May 30 00:29:19 EDT 2011


In article <4de31635$0$29990$c3e8da3$5496439d at news.astraweb.com>,
Steven D'Aprano  <steve+comp.lang.python at pearwood.info> wrote:
>That's also completely wrong. The correct way to test for a NAN is with 
>the IEEE-mandated function isnan(). The NAN != NAN trick is exactly that, 
>a trick, used by programmers when their language or compiler doesn't 
>support isnan().

Perhaps it would be reasonable to be able to do:

    x.isnan()

when x is a float.

>Without support for isinf(), identifying an INF is just as hard as 
>identifying an NAN, and yet their behaviour under equality is the 
>complete opposite:
>
>>>> inf = float('inf')
>>>> inf == inf
>True

Fortunately:

    def isnan(x):
        return x != x
    _inf = float("inf")
    def isinf(x):
        return x == _inf
    del _inf

both do the trick here.

I would like to have both modes (non-exception-ing and exception-ing)
of IEEE-style float available in Python, and am not too picky about
how they would be implemented or which one would be the default.
Python could also paper over the brokenness of various actual
implementations (where signalling vs quiet NaNs, and so on, do not
quite work right in all cases), with some performance penalty on
non-conformant hardware.
-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)      http://web.torek.net/torek/index.html



More information about the Python-list mailing list