[Numpy-discussion] Zero Division not handled correctly?

David Cournapeau cournape@gmail....
Tue Dec 8 02:04:18 CST 2009


On Mon, Dec 7, 2009 at 6:16 AM, Skipper Seabold <jsseabold@gmail.com> wrote:
> I believe this is known, but I am surprised that division by "integer"
> zero results in the following.
>
> In [1]: import numpy as np
>
> In [2]: np.__version__
> Out[2]: '1.4.0.dev7539'
>
> In [3]: 0**-1 # or 0**-1/-1
> ---------------------------------------------------------------------------
> ZeroDivisionError                         Traceback (most recent call last)
>
> /home/skipper/school/Data/ascii/numpy/<ipython console> in <module>()
>
> ZeroDivisionError: 0.0 cannot be raised to a negative power
>
> In [4]: np.array([0.])**-1
> Out[4]: array([ Inf])
>
> In [5]: np.array([0.])**-1/-1
> Out[5]: array([-Inf])
>
> In [6]: np.array([0])**-1.
> Out[6]: array([ Inf])
>
> In [7]: np.array([0])**-1./-1
> Out[7]: array([-Inf])
>
> In [8]: np.array([0])**-1
> Out[8]: array([-9223372036854775808])
>
> In [9]: np.array([0])**-1/-1
> Floating point exception
>
> This last command crashes the interpreter.
>
> There have been some threads about similar issues over the years, but
> I'm wondering if this is still intended/known or if this should raise
> an exception or return inf or -inf.  I expected a -inf, though maybe
> this is incorrect on my part.

The crash is fixed, but you get some potentially spurious warning,
still. A thorough solution will require some more work.

David


More information about the NumPy-Discussion mailing list