NaN

Chris Rebert clp2 at rebertia.com
Thu Apr 28 19:01:49 CEST 2011


On Thu, Apr 28, 2011 at 9:21 AM, Eric Snow <ericsnowcurrently at gmail.com> wrote:
> There's a big discussion going on at python-dev and python-ideas about NaN
> (not-a-number, from IEEE 754).  I haven't really gotten into any scientific
> computing or numeric programming so I have never knowingly dealt with NaN.
>  However, I have read the discussions and several things are not clear:
> 1. Why is NaN not an exception?  (not "why not change it to one?"  Changing
> it now would probably break stuff.)

Most things that would produce NaN [e.g. x/0, sqrt(-abs(x))] in
lower-level languages do produce exceptions in Python. If you
encounter a NaN in Python, it was probably either explicitly created
or came from a C extension library. It's basically an abstraction
leaked through from C/hardware for compatibility.

> 2. What are the use cases for NaN?  Looks like it gets used a lot as a
> numeric (float?) object with non-value.

FWICT, it's useful in lower-level languages (which typically lack
exceptions and often lack nice ways of returning multiple values from
a function) as a convenient way of signaling a mathematical error. It
also lets some calculations continue to limp along (albeit with a NaN
result) after an error rather than forcing the probably slower
explicit handling of the error at the time of its occurrence. And it's
used to represent missing numeric data values, sort of like a
numerical None/Null: "How much does the truck weigh? NaN (i.e. No
idea; we haven't put it on the scale yet)."

Cheers,
Chris
--
http://rebertia.com



More information about the Python-list mailing list