numpy.result_type#

numpy.result_type(*arrays_and_dtypes)#

Returns the type that results from applying the NumPytype promotion rules to the arguments.

Type promotion in NumPy works similarly to the rules in languageslike C++, with some slight differences. When both scalars andarrays are used, the array’s type takes precedence and the actual valueof the scalar is taken into account.

For example, calculating 3*a, where a is an array of 32-bit floats,intuitively should result in a 32-bit float output. If the 3 is a32-bit integer, the NumPy rules indicate it can’t convert losslesslyinto a 32-bit float, so a 64-bit float should be the result type.By examining the value of the constant, ‘3’, we see that it fits inan 8-bit integer, which can be cast losslessly into the 32-bit float.

Parameters:
arrays_and_dtypeslist of arrays and dtypes

The operands of some operation whose result type is needed.

Returns:
outdtype

The result type.

Notes

The specific algorithm used is as follows.

Categories are determined by first checking which of boolean,integer (int/uint), or floating point (float/complex) the maximumkind of all the arrays and the scalars are.

If there are only scalars or the maximum category of the scalarsis higher than the maximum category of the arrays,the data types are combined withpromote_typesto produce the return value.

Otherwise,min_scalar_type is called on each scalar, andthe resulting data types are all combined withpromote_typesto produce the return value.

The set of int values is not a subset of the uint values for typeswith the same number of bits, something not reflected inmin_scalar_type, but handled as a special case inresult_type.

Examples

>>>importnumpyasnp>>>np.result_type(3,np.arange(7,dtype='i1'))dtype('int8')
>>>np.result_type('i4','c8')dtype('complex128')
>>>np.result_type(3.0,-2)dtype('float64')
On this page