-
-
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/question: Should np.abs always return a positive number? #26145
Comments
It would subtly change promotion and in strange ways if you consider things like:
and voila, you got a float64 :). I think we have an old issue about whether we should just raise an error here.
We would have to encode that part, e.g. by adding explicit |
Also see #21289 |
Sorry for the duplication! Looking at #21648,it seems For arrays, I guess this ends up getting back to the more general question of whether one should have optional over/underflow warnings. |
At one point I think I remember a discussion about having overflow-warning integer dtypes that could be used instead of the faster overflow-ignoring ones. |
Even without putting the data in something like
|
My ideal would be a solution that uses (or is similar to) |
Also see #25396, I got pinged the other day about it so Lysandros might be looking into this now as well. |
Looked a bit more into this, but #21648 just adjusted |
I really miss having an |
Following that trace, also gh-5657, which has more discussion |
From gh-5657, there also is the suggestion to have an Separately, perhaps for |
Describe the issue:
Currently,
np.abs
for the most negative integer of a given types cannot return the absolute value in the same type, so instead wraps and gives a negative value. This is surprising, and could be avoided by promoting to the unsigned type (e.g.,i1
->u1
). However, this may have its own issues if further calculations are done, so really not sure what is the best option. (Hence, not sure whether this is a bug or a question...)Note that passing in
dtype='u1'
works, though one then has to also givecasting='unsafe'
- even though in this case it is of course not unsafe at all...Reproduce the code example:
EDIT: I didn't even notice that using
u1
actually produces nonsense for-127
. From gh-5657, the "correct" way to avoid the overflow (or, rather, correct for the overflow) is,Error message:
No response
Python and NumPy Versions:
2.1.0.dev0+git20240326.9992c3a
3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0]
Runtime Environment:
N/A
Context for the issue:
Found indirectly in #26143
The text was updated successfully, but these errors were encountered: