don't NaN & infinities hide FP errors

Grant Edwards grante at
Wed Nov 17 21:56:40 CET 2004

On 2004-11-18, Jeremy Bowers <jerf at> wrote:
> On Wed, 17 Nov 2004 01:30:33 -0800, kartik wrote:
>> Since Python floats are implemented using C doubles, assuming
>> IEEE 754 compliance, certain operations should result in NaN
>> or infinity. Don't such special values hide errors?


>> Would we want our variables to be in such states?

Assuming you're not using the royal we, yes "we" do.

>> I feel it would be better if a floating point division by zero
>> (or other actions that produce NaNs or infinities) throws an
>> exception, just like an integer divide by zero.

What if I don't _want_ an exception?  What if NaN or Infinity
is _the_correct_answer_?  For what I do, most of the time, NaN
or Infinity is exactly what I want.

>> Am I right?
> You may think, while dealing with your problems and programs, that NaN is
> an error. 
> I may want them, because NaN, Inf, and exceptions are not the same things.

I certainly want them.  I'd be right annoyed if somebody
decided all of a sudden than NaNs are errors.  NaNs are
tremendously useful things, and making them errors would be
like deciding that negative integers are errors since you
obviously can have "minus seven" apples sitting on the table in
front of you.

Let's say you've got a bunch of process control modules. Each
of them takes a set of input values and produces a set of

Now let's say one of the inputs fails (no valid value is
available).  You can just shut down the whole refinery, you've
got to try to keep going as best you can.  The easiest way to
do that is to feed a NaN in on the invalid input, and let it
propogate through the network of modules.  The outputs that
don't depend on the invalid input remain valid.  The outputs
that do depend on the invalid input are NaNs.  

At the output end of things you don't have to do all sorts of
logic to figure out which outputs are valid and which ones
aren't -- all you have to do is decide what to do when each
output is a NaN.

Grant Edwards                   grante             Yow!  I love ROCK 'N
                                  at               ROLL! I memorized the
                                 all WORDS to "WIPE-OUT"

More information about the Python-list mailing list