Skip to content
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

pow with integer arrays that overflow differs from numpy in 2.8.7 #454

Closed
mroeschke opened this issue Sep 26, 2023 · 5 comments
Closed

pow with integer arrays that overflow differs from numpy in 2.8.7 #454

mroeschke opened this issue Sep 26, 2023 · 5 comments
Labels

Comments

@mroeschke
Copy link

It appears that pow was not implemented in 2.8.4 and newly implemented(?) in 2.8.7, but it appears that pow that overflows has differing behavior from numpy

In [1]: import numpy as np

In [2]: import numexpr as ne

In [3]: arr = np.array([10000, 20000])

In [4]: arr**arr
Out[4]: array([0, 0])

In [5]: ne.evaluate('arr**arr')  # '2.8.4'
TypeError: '<' not supported between instances of 'str' and 'int'

In [5]: ne.evaluate('arr**arr')  # '2.8.7'
Out[5]: array([9223372036854775807, 9223372036854775807], dtype=int64)
@robbmcleod
Copy link
Member

You can see the context here for the changes were made:

#434

2.8.4 was ignoring the virtual machine (VM) and short-cutting the calculation, but the implementation was buggy. Someone at some point, put in a check implemented a > 0 check for the entire input array in the pre-processing for integer power, which is totally against the whole design objective of NumExpr.

I would argue that the NumPy behavior is also wrong. NumPy raises an exception if you feed a negative exponent to an integer base. It should also error in this case since the answer is clearly not 0.

The VM doesn't have any faculty to, for example, set an error flag that would result in an exception to be raised after the calculation is completed.

@mroeschke
Copy link
Author

Agreed that the numpy result is also not correct/useful.

So in general for numexpr, any overflowing expression is expected to truncate at the max value of the specified data type?

@robbmcleod
Copy link
Member

How NumExpr will behave in this situation will depend on the CPU architecture I think... C++ doesn't specify a behavior for integer overflow conditions so it's undefined.

@rebecca-palmer
Copy link
Contributor

The other operations don't - pow is special because it's implemented by casting to double and back. See pandas-dev/pandas#54546.

Copy link

Message to comment on stale issues. If none provided, will not mark issues stale

@github-actions github-actions bot added the Stale label Feb 17, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants