You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
The type conversion problem of array operations #11485
a = np.array([0,255], dtype='uint8')
output:array([ 0, 255], dtype=uint8)
a * 2
output:array([ 0, 254], dtype=uint8) // become the top eight after the operation
b = np.array([1, 255], dtype='uint8')
b * 256
output:array([ 256, 65280], dtype=uint16) // Automatically become the type of uint16
The change of the variable type depends on the minimum of the array?
Hmm, that looks like a bug, and maybe a very old one. The array data type should dominate the scalar unless a float is involved. It may be intended behavior, but I cannot think why.
Its the old problem of "oh a scalar, lets see what the minimum type is when considering the other array", and if the arrays type happens to be good enoug, it uses that... I.e. the 256 scalar is too large for uint8, so it decides that uint16 is necessary, but 2 fits uint8, so that is taken in the first example.
Arggghhhh, I didn't know that was the policy, that's awful. I thought that within the same kind, the array type was always dominant, which is simple to understand and has the virtue that the output type only depends on the code, not the code and the input values.
Yeah, that would make more sense, at least for python types (for numpy
types they could just be treated like an array too as an option, I find
it annoying that if I happen to have a 0d array things might suddenly
stop casting as well).
And we can put a warning anyway when the python scalar cannot cast
safely...
There have been a lot of discussions about this IIRC, but they always
somewhat died out from a lack of clarity what is right, and then the
fact that changing things seems tricky, I think the last time it
changed was 1.7, not quite sure what the behaviour was before.
Activity
charris commentedon Jul 3, 2018
Hmm, that looks like a bug, and maybe a very old one. The array data type should dominate the scalar unless a float is involved. It may be intended behavior, but I cannot think why.
seberg commentedon Jul 3, 2018
Its the old problem of "oh a scalar, lets see what the minimum type is when considering the other array", and if the arrays type happens to be good enoug, it uses that... I.e. the
256scalar is too large foruint8, so it decides thatuint16is necessary, but2fitsuint8, so that is taken in the first example.charris commentedon Jul 3, 2018
Arggghhhh, I didn't know that was the policy, that's awful. I thought that within the same kind, the array type was always dominant, which is simple to understand and has the virtue that the output type only depends on the code, not the code and the input values.
seberg commentedon Jul 3, 2018
eric-wieser commentedon Jul 10, 2018
Relevant is #10322, which here looks like:
seberg commentedon Jan 30, 2024
Closing, this is effectively fixed by NEP 50 adoption in main/NumPy 2.0.