Numerics, NaNs, IEEE 754 and C99

Nick Maclaren nmm1 at
Thu Jun 15 11:10:50 CEST 2006

In article <44912058$0$29674$636a55ce at>,
Christophe <chris.cavalaria at> writes:
|> > Now, can you explain why 1/0 => -Inf wouldn't work as well?  I.e. why
|> > are ALL of your zeroes, INCLUDING those that arise from subtractions,
|> > are known to be positive?
|> I would say that the most common reason people assume 1/0 = Inf is 
|> probably because they do not make use of negative numbers or they forgot 
|> they exist at all.

Usually the latter :-(

Yes, if you are working with the non-negative real numbers (or even
non-negative integers), then the best result of 1/0 is +infinity.
Thus, if you are working with events, a count of zero is a true zero,
but its inverse can reasonably be said to be +infinity.

However, it is common for non-experts to assume that they are working
in the non-negative real domain, because numbers are conventionally
measured that way, but in fact that is not so.  The use of elapsed
times is one such "gotcha".  I have seen quite a few programs that
have assumed that elapsed times were always positive, and which have
blown up when applied to real problems, where the measurement of an
elapsed time may be negative.

Hence, the SAFE approach is to make the inverse of all zeros a NaN.

Nick Maclaren.

More information about the Python-list mailing list