comparing nan "number"

Georgy Pruss see_signature__ at hotmail.com
Mon Dec 1 08:37:40 EST 2003


"Boštjan Jerko" <bostjan.jerko at mf.uni-lj.si> wrote in message news:mailman.1215.1070277937.702.python-list at python.org...
> Hello !
>
> I need to know if the result of math formula is nan (Not a number).
> How can I do that?
>
> Thanks,
>
> B.

I used repr() as a workaround:

>>> INF = 1e9999
>>> NAN = INF - INF
>>> def isnan( x ):
...     rx = repr(float(x))
...     return rx==repr(NAN) or rx==repr(-NAN)
...
>>> t = 2*NAN
>>> t == NAN
False
>>> t is NAN
False
>>> isnan(t)
True

You can also use binary representation of numbers:

>>> import struct
>>> NAN_POS = struct.pack('d',NAN)
>>> NAN_NEG = struct.pack('d',-NAN)
>>> def isnan2( x ):
...     xs = struct.pack('d',float(x))
...     return xs==NAN_POS or xs==NAN_NEG
...


HTH
-- 
Georgy Pruss
E^mail: 'ZDAwMTEyMHQwMzMwQGhvdG1haWwuY29t\n'.decode('base64')






More information about the Python-list mailing list