-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
BUG: abs(np.int8(-128)) should arguably give an OverflowWarning #21289
Comments
This is known behavior. One option would be to return
|
Thanks for your response! |
Hey @charris, This behavior is a bit confusing, what do you think about printing a warning in cases like these? Otherwise - is there an easy way to check if an array contains the |
Yeah, this is mainly a duplicate of e.g. gh-21237 (which is very similar). The basic thing, is that this issue is part of a long list of issues around the fact that it would be good if NumPy was able to give (maybe optional) integer overflow/rollover warnings. However, we do give warnings for scalar operations. That may be slightly strange, but for scalar operations the check does not impose any significant performance cost. In that sense, the following arguably should already give a warning: abs(np.int8(-128)) but does not. I think this is a nice easy thing to fix, although would prefer it happening after gh-21188 is merged. EDIT: Fixing this requires diving into a fairly complex file of C code ( |
Checks condition a == NPY_MIN_@NAME@ to determine whether an overflow error has occurred for np.int8 type. See #21289 and #21188 (comment) for reference. This also adds error integer overflow handling to the `-scalar` paths and "activates" a test for the unsigned versions. A few tests are skipped, because the tests were buggy (they never ran). These paths require followups to fix.
Describe the issue:
The range of np.int8 is -128 to 127.
The ideal range of np.abs(np.int8) is -127 to 128.
But apparently 128 overflow.
So np.abs(-128, dtype=int8) = -128, while the rest of the cases are all correct.
This error is hard to detect and is also found in np.int16.
Reproduce the code example:
Error message:
No response
NumPy/Python version information:
numpy: 1.18.5
sys: 3.7.10 (default, Jun 4 2021, 14:48:32)
[GCC 7.5.0]
The text was updated successfully, but these errors were encountered: