Type Promotion Rules ¶
Array API specification for type promotion rules.
A conforming implementation of the array API standard must implement the following type promotion rules governing the common result type for two array operands during an arithmetic operation.
A conforming implementation of the array API standard may support additional type promotion rules beyond those described in this specification.
Type codes are used here to keep tables readable; they are not part of the standard.
In code, use the data type objects specified in
The following type promotion tables specify the casting behaviour for operations involving two arrays. In situations where more than two arrays participate, the table can be used repeatedy on pairs of input arrays (the result does not depend on the order in which the arrays are given).
Signed integer type promotion table ¶
i1 : 8-bit signed integer (i.e.,
i2 : 16-bit signed integer (i.e.,
i4 : 32-bit signed integer (i.e.,
i8 : 64-bit signed integer (i.e.,
Unsigned integer type promotion table ¶
u1 : 8-bit unsigned integer (i.e.,
u2 : 16-bit unsigned integer (i.e.,
u4 : 32-bit unsigned integer (i.e.,
u8 : 64-bit unsigned integer (i.e.,
Mixed unsigned and signed integer type promotion table ¶
Floating-point type promotion table ¶
f4 : single-precision (32-bit) floating-point number (i.e.,
f8 : double-precision (64-bit) floating-point number (i.e.,
Type promotion rules strictly apply when determining the common result type for two array operands during an arithmetic operation, regardless of array dimension. Accordingly, zero-dimensional arrays are subject to the same type promotion rules as dimensional arrays.
Type promotion of non-numerical data types to numerical data types is unspecified (e.g.,
Non-array (“scalar”) operands must not participate in type promotion.
Mixed integer and floating-point type promotion rules are not specified because behavior varies between implementations.
Mixing arrays with Python scalars ¶
Using Python scalars (i.e. instances of
) together with arrays must be supported for:
array <op> scalar,
scalar <op> array,
is a built-in operator (see
supported by the array object), and
is of the same kind as the array
dtype (e.g. a
scalar if the array’s dtype is
The expected behaviour is then equivalent to:
Convert the scalar to a 0-D array with the same dtype as that of the array used in the expression.
Execute the operation for
array <op> 0-D array(or
0-D array <op> arrayif
scalarwas the left-hand argument).
Note again that mixed integer and floating-point behaviour is not specified.
Mixing an integer array with a Python float may give
or raise an exception - behaviour of implementations will differ.