-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
BUG: Inconsistent behaviour of return type of int64.__pow__ #8809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Partials resolves numpy#8809 Previously, the signatures were: * power(int, int) -> int * power(uint, uint) -> uint. Since we forbid the second argument from being negative, we can also define * power(int, uint) -> int: same as power(int, int), without an error check * power(uint, int) -> uint: same as power(uint, uint), with an error check If we do not define these, then the signature used is: * t = common(int, uint); power(t, t) -> t: This is bad, because common(int64, uint64) is float
Isn't this a problem with all ufuncs? Extending your example a little, and testing for multiplication val = 1
cases = [(' p', val)]
for dtype,name in zip([np.int64, np.uint64], ['i', 'u']):
cases.extend([('s' + name, dtype(val)),
('0' + name, np.array(val, dtype=dtype)),
('1' + name, np.array([val], dtype=dtype)),
('2' + name, np.array([[val]], dtype=dtype))])
names, tps = zip(*cases)
ret = [[(a*b) for n,b in cases] for n1,a in cases]
kinds = [['p' if type(k) == int else k.dtype.kind for k in r] for r in ret]
print " " + " ".join(names)
print "\n".join(n + " " + " ".join(d) for n,d in zip(names, kinds)) Output: (p = python, i = int, u = uint, s = scalar, 0,1,2 = 0d,1d,2d)
the power operation gives the same table. |
Also, this is loosely related to #8231 and the related discussions on the mailing list last year about integer powers: First in June under subject "Integers to integer powers, let's make a decision ", then in October under the same thread plus others like "fpower". (but to be clear, this is a somewhat different issue) |
The shape inconsistencies is #10322 again |
This is the result of NumPy's "type demotion" rules for mixed scalar/array operations. When a positive-dimensional array and a 0-dimensional array or array scalar participate in a binary operation, the scalar gets "type demoted" based on its value, so 3 gets demoted to uint8, and the result dtype is uint64. On the other hand, when both operands are scalar, NumPy treats 3 as having dtype np.int_, which is signed, and numpy can't find an integer dtype big enough to hold all values of uint64 and int_, so it uses float64. This is documented in the docs for The Stack Overflow question also showed what looked like NumPy using a floating-point power implementation for the uint64 output case. I don't know why that was happening, but it seems to have been fixed at some point after the question was asked. |
Closing as duplicate of gh-22624 |
Raised by this stackoverflow question. The following code , run on master...
Gives the alarming output of:
The return
dtype
of power should not depend on the shape of the input arrayThe text was updated successfully, but these errors were encountered: