Question
Why the addion of float32 array and float64 scalar is float32 array in Numpy?
If one add float32 and float64 scalars, the result is float64: float32 is promoted to float64.
However, I find that, adding a float32 array and a float64 scalar, the result is a float32 array, rather than one might expect to be a float64 array.
I have written some code repreduce the problem.
My question is why the dtype of np.add(np.array([a]), b)
is float32?
import numpy as np
import sys
print(sys.version_info) # sys.version_info(major=3, minor=10, micro=9, releaselevel='final', serial=0)
print(np.__version__) # '1.24.1'
a = np.float32(1.1)
b = np.float64(2.2)
print((a+b).dtype) # float64
print(np.add(a, np.array([b])).dtype) # float64
print(np.add(np.array([a]), np.array([b])).dtype) # float64
print(np.add(np.array([a]), b).dtype) # float32 ?? expcting float64
This contradicts the doc of np.add
(https://numpy.org/doc/stable/reference/generated/numpy.add.html), which says in the Notes
Equivalent to x1 + x2 in terms of array broadcasting.
x, y = numpy.broadcast_arrays(np.array([a]), b)
print(np.add(x, y).dtype) ## float64