[Numpy-discussion] 0/0 is 0 or nan?

Robert Kern robert.kern@gmail....
Thu Mar 8 20:45:27 CST 2007

Chris Ball wrote:
> Hi,
> I noticed some behavior that seems inconsistent to me:
>>>> from numpy import divide, seterr
>>>> seterr(divide='ignore')
> {'over': 'raise', 'divide': 'raise', 'invalid': 'raise', 'under': 'raise'}
>>>> seterr()
> {'over': 'raise', 'divide': 'ignore', 'invalid': 'raise', 'under': 'raise'}
>>>> divide(0,0)
> 0
>>>> divide(0.0,0.0)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> FloatingPointError: invalid value encountered in divide
> Please could someone tell me whether this behavior comes from numpy or
> elsewhere?

seterr() would normally only control the signal handlers for IEEE-754 floating
point exceptions (note: these are not Python exception objects, but signals that
are thrown at the hardware/OS level, I believe). We have extended this
functionality to work with numpy arrays and numpy scalars of non-floating point

However, I believe that divide() on two Python ints goes through a different
path than for numpy arrays and numpy scalars. Rather, they are recognized as
objects that implement .__div__(), and that is called instead. Builtin Python
ints defer the computation to C directly without the extra error-handling that
we've implemented in numpy.

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 Numpy-discussion mailing list