comparing nan "number"

Alexander Schmolck a.schmolck at gmx.net
Mon Dec 1 16:26:40 EST 2003


Michael Hudson <mwh at python.net> writes:

> result = some_calculation()
> if result != result:
>     # it's a nan
> 
> *might* work, depending probably on compiler, Python version, platform
> and phase of the moon.

Since this gives different results for two recent python versions installed on
my machine, I'd rather recommend against it if you value your sanity. IMO it's
either the repr hack or scipy, unlike you like to live dangerously.

[In a similar vain, I'd also recommend resisting the temptation of putting
nans (or richly comparing types) into containers like lists etc. because
that's just asking for undefined behavior. E.g. ``x in [x,x,x]`` will quite
possibly be `False` and {x:"some value}[x] might well raise an exception if
`x` is a nan (or indeed one of infinitely many Numeric arrays with
non-symmetric `==`) . Somehow all this suggests to me that too many things are
folded into python's __eq__.]


'as




More information about the Python-list mailing list