[Numpy-discussion] Behaviour of integer powers
Stéfan van der Walt
stefan@sun.ac...
Sun Feb 8 03:54:04 CST 2009
Hi all,
Ticket #955 (http://scipy.org/scipy/numpy/ticket/955) touches on the
following issue:
>>> 0.0 ** np.array([-1, 0, 1], dtype=np.int32)
array([ Inf, 1., 0.])
>>> 0.0 ** np.array([-1, 0, 1], dtype=np.int32)[0]
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
ZeroDivisionError: 0.0 cannot be raised to a negative power
This is on a 32-bit platform.
As I understand this happens because, in the second case, Python sees
that "-1" is an int and does the power operation. In other words,
when we raise to the power of an array, the NumPy machinery is
involved, whereas if we raise to np.int32(-1), it is not. This is due
to the int32 type deriving from the Python int.
I can't think of an easy way to address the problem, but I was hoping
to get some advice from the list.
Thanks
Stéfan
More information about the Numpy-discussion
mailing list