test for nan

Michael Hudson mwh at python.net
Thu Apr 1 06:03:20 EST 2004


Peter Maas <fpetermaas at netscape.net> writes:

> John Hunter wrote:
> > I have a C extension module that is returning some doubles.  When the
> > doubles get out of range, the numbers print as 'nan'.
> > Is there a better way to test for NaN than
> >    str(p1)=='nan'
> > where p1 is a float?
> > python2.3
> > Thanks,
> > John Hunter
> > 
> 
> As far as I know NaN is th only value that yields false when
> compared to itself. 

There can be more than one NaN, by the way.

> This leads to
> 
> def isNaN(x):
>      return (x == x) == False

This will work with 2.3 on Windows (I believe), current CVS on Windows
(if compiled with VC7.1), current CVS on Linux (assuming an even
vaguely recent gcc), but not current CVS on Windows compiled with VC6,
nor Python 2.3 on Linux/gcc.  Confused yet?

Other platforms I haven't the faintest idea about.

Cheers,
mwh

-- 
41. Some programming languages manage to absorb change, but
    withstand progress.
  -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html



More information about the Python-list mailing list