[SciPy-user] nan?

Robert Kern robert.kern at gmail.com
Wed Nov 29 17:59:47 CST 2006


Joshua Petterson wrote:
> hello,
> my question is maybe trivial, but what is the utility of numpy.NAN?

It's a special kind of floating point number defined by the IEEE-754 floating
point specification. It means that it is "Not a Number". It comes out of certain
floating point operations (e.g. inf/inf) to specify that the operation actually
has indeterminate value or that the operation is invalid.

The alternative is to raise an exception every time such an operation occurs.
With arrays, that kind of behavior is often undesirable since it means that all
of your expensive calculations stop even though only one element was bad.
Returning NaNs for those values might allow the programmer to inspect the
results and locate the problem (if indeed it is a problem). Of course, sometimes
you want an exception to be raised to stop execution. You can control this
behavior with numpy.seterr().

One of the quirks of NaNs is that they are defined to not equal anything,
including themselves. You can use numpy.isnan() to locate NaNs. In some systems,
NaNs are used as a way to represent missing data (people often have strong
opinions about this, both for and against; I'd advise searching the archives of
this list for discussions about masked arrays instead of opening that argument
again).

You can find more information about NaNs and other tricky bits of floating point
from the classic paper "What Every Computer Scientist Should Know About
Floating-Point Arithmetic":

  http://docs-pdf.sun.com/800-7895/800-7895.pdf

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco


More information about the SciPy-user mailing list