这是this个问题的后续.
When we use a numpy array with a specific type, it preserves its type following numeric operations.
For example adding 1 to a uint32
array will wrap up the value to 0 if needed (when the array contained the max uint32
value) and keep the array of type uint32
:
import numpy
a = numpy.array([4294967295], dtype='uint32')
a += 1 # will wrap to 0
print(a)
print(a.dtype)
输出:
uint32
[0]
uint32
对于具有相同类型的数组scalar,此行为不成立:
import numpy
a = numpy.uint32(4294967295)
print(a.dtype)
a += 1 # will NOT wrap to 0, and change the scalar type
print(a)
print(a.dtype)
输出:
uint32
4294967296
int64
但根据前array scalars documentation名:
使用数组标量的主要优点是它们保留数组类型
...
因此,数组标量的使用确保了identical behaviour between arrays and scalars,不管值是否在数组内部.
(emphasys is mine)
My question:
Why do I observe the above different behavior between arrays and scalars despite the explicit documentation that states they should behave identically ?