Numpy casting float32 to float64

后端 未结 1 654
闹比i
闹比i 2020-12-12 03:52

I want to perform some standard operations on numpy float32 arrays in python 3, however I\'m seeing some strange behavior when working with numpy sum()

相关标签:
1条回答
  • 2020-12-12 04:10

    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.

    0 讨论(0)
提交回复
热议问题