I'm trying to find some documentation understand how dtypes are combined. For example:
x : np.int32 = ...
y : np.float64 = ...
- What is going to be the type of
x + y? - does it depend on the operator (here
+) ? - does it depend where it is stored (
z = x + yvsz[...] = x + y) ?
I'm looking for part of the documentation that describe these kind of scenario but so far I'm empty-handed.
If data types don't match, then NumPy will upcast the data to the higher precision data types if possible. And it doesn't depend on the type of (arithmetic) operation that we do or to the variables that we assign to, unless that variable already has some other dtype. Here is a small illustration:
If you don't explicitly assign a datatype, then the result will be upcasted to the higher of data types of the given inputs. For example,
numpy.add()accepts adtypekwarg where you can specify the datatype of the resultant array.And, one can check whether two different datatypes can be safely casted according to casting rules by using
numpy.can_cast()For the sake of completeness, I add the following
numpy.can_cast()matrix:And the output would be the following matrix which shows what dtypes can be safely casted (indicated by
1) and what dtypes cannot be casted (indicated by0), following the order of from casting (along axis-0) to to casting (axis-1) :Since the characters are cryptic, we can use the following for better understanding of the above casting matrix:
And the output would be: