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

BUG: Overflow reported when negating uint64 #24737

Closed
bashtage opened this issue Sep 18, 2023 · 9 comments
Closed

BUG: Overflow reported when negating uint64 #24737

bashtage opened this issue Sep 18, 2023 · 9 comments
Labels
00 - Bug 09 - Backport-Candidate PRs tagged should be backported triage review Issue/PR to be discussed at the next triage meeting

Comments

@bashtage
Copy link
Contributor

Describe the issue:

A usually valid operation, negation of an unsigned integer, is raising an underflow warning.

Reproduce the code example:

import numpy as np

a = np.uint64(0xffffffff)
b = -a

Error message:

`RuntimeWarning: overflow encountered in scalar negative`

Runtime information:

1.25.2
3.11.5 | packaged by Anaconda, Inc. | (main, Sep 11 2023, 13:26:23) [MSC v.1916 64 bit (AMD64)]

Context for the issue:

Wrapping unsigned integer values is fairly common in C using negation. This used to be free from warnings, so was surprised to see this.

@bashtage bashtage changed the title BUG: Underflow reported when negating uint64 BUG: Overflow reported when negating uint64 Sep 18, 2023
@ngoldbaum
Copy link
Member

I can reproduce this. It looks like this changed sometime after NumPy 1.23, with the 1.23.3 wheel:

In [1]: import numpy as np

In [2]: a = np.uint64(0xffffffff)

In [3]: b = -a

and the NumPy 1.24.3 wheel:

In [1]: import numpy as np
   ...: 
   ...: a = np.uint64(0xffffffff)

In [2]: b = -a
<ipython-input-2-064521cdc161>:1: RuntimeWarning: overflow encountered in scalar negative

@charris
Copy link
Member

charris commented Sep 18, 2023

Works for me: gcc 13.2.1 in the development branch.

Apparently C++ does have a preference, the hex literal has the type of the first type where it fits:

  • int
  • unsigned int
  • long int
  • unsigned long int
  • long long int
  • unsigned long long int

Maybe that is related? I assume the conversion is done by Python or a library.

@ngoldbaum
Copy link
Member

I bisected this to #21648, ping @Micky774.

(I posted an incorrect comment earlier that said a different PR was the cause and I deleted that comment, sorry for the duplicate e-mail)

@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Sep 18, 2023
@charris charris added this to the 1.26.1 release milestone Sep 18, 2023
@seberg seberg added the triage review Issue/PR to be discussed at the next triage meeting label Sep 19, 2023
@seberg
Copy link
Member

seberg commented Sep 19, 2023

This needs a decision that we want to allow some scalar operators to wrap silently to users while we warn for others.
I.e. there was a reason for this change, that if you have an unsigned int and don't realize it, getting the wrong result deserves a warning. (something that NEP 50 may make slightly more common)

@bashtage
Copy link
Contributor Author

Would it be possible to separate unary negation from multiplication by a negative number, where unary would not warn?

@seberg
Copy link
Member

seberg commented Sep 19, 2023

Yes, we can do all of that. We just need to decide to do it.

@Micky774
Copy link
Contributor

Micky774 commented Sep 19, 2023

IMO it's safer to offer a warning that can be suppressed than to remove the warning because the underlying operation is sometimes intended. The behavior is still noteworthy in some cases and I think it is safe to optimize for those cases.

@RubTalha
Copy link

https://stackoverflow.com/questions/77467422/runtimewarning-overflow-encountered-in-scalar-negative

@mattip
Copy link
Member

mattip commented Jan 10, 2024

In the latest triage meeting we decided to close this for now. In the future, we would prefer that scalars perform more like 0d arrays (and not warn), but that would be part of a larger effort and may even need a NEP.

@mattip mattip closed this as completed Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
00 - Bug 09 - Backport-Candidate PRs tagged should be backported triage review Issue/PR to be discussed at the next triage meeting
Projects
None yet
Development

No branches or pull requests

7 participants