简体   繁体   中英

Numpy function type casting automatically or not

consider the following numpy code

foo = np.array([255, 255], dtype=np.uint8)
foo_sum = foo.sum()

foo_square_v1 = foo**2
foo_square_v2 = np.square(foo)
foo_square_v3 = np.pow(foo, 2)

foo_sum get correctly casted to uint64 and return 510 But all the 3 foo_square trigger no type casting and have an overflow (,!!), returning [1,1]

What's the rule of thumb to know whether I should do casting manually, or whether numpy do it automatically for me?

My system: Python 3.x, X64 machine, Linux, numpy 1.20.3

You seem to want NumPy to detect overflow and promote dtypes in response. NumPy will never do that. Most NumPy functionality will just use the same dtype as the input, or follow the default promotion rules when there are multiple dtypes.

numpy.sum is unusual, but even its dtype rules aren't what you want. Quoting thedocs for numpy.sum ( a is the input array):

The dtype of a is used by default unless a has an integer dtype of less precision than the default platform integer. In that case, if a is signed then the platform integer is used while if a is unsigned then an unsigned integer of the same precision as the platform integer is used.

The "default platform integer" here is C long (not C int ). If C long is 32-bit (particularly on Windows) and you need a 64-bit output to avoid overflow, you'll still need to specify the dtype manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM