The result of np.sum(a)
is a NumPy scalar, rather than an array. Operations involving only scalars use different casting rules from operations involving (positive-dimensional) NumPy arrays, described in the docs for numpy.result_type
.
When an operation involves only scalars (including 0-dimensional arrays), the result dtype is determined purely by the input dtypes. The same is true for operations involving only (positive-dimensional) arrays.
However, when scalars and (positive-dimensional) arrays are mixed, instead of using the actual dtypes of the scalars, NumPy examines the values of the scalars to see if a "smaller" dtype can hold them, then uses that dtype for the type promotion. (The arrays do not go through this process, even if their values would fit in a smaller dtype.)
Thus,
np.sum(a)+1
is a scalar operation, converting 1
to a NumPy scalar of dtype int_
(either int32 or int64 depending on the size of a C long) and then performing promotion based on dtypes float32 and int32/int64, but
a+1
involves an array, so the dtype of 1
is treated as int8 for the purposes of promotion.
Since a float32 can't hold all values of dtype int32 (or int64), NumPy upgrades to float64 for the first promotion. (float64 can't hold all values of dtype int64, but NumPy won't promote to numpy.longdouble for this.) Since a float32 can hold all values of dtype int8, NumPy sticks with float32 for the second promotion.
If you use a number bigger than 1, so it doesn't fit in an int8:
In [16]: (a+1).dtype
Out[16]: dtype('float32')
In [17]: (a+1000000000).dtype
Out[17]: dtype('float64')
you can see different promotion behavior.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…