Suppose I define a constant of dtype float64
and an array of ones of dtype float32
:
c = np.arccos(-1)
ones = np.array([1,1,1], dtype='float32')
print(c.dtype, ones.dtype)
float64 float32
If I multiply the scalar by the entire array, I get a result with dtype float32
:
result_array = c*ones
print(result_array, result_array.dtype)
[3.1415927 3.1415927 3.1415927] float32
However if I multiply the scalar by just the first element, I get a result of dtype float64
:
result_scalar = c*ones[0]
print(result_scalar, result_scalar.dtype)
3.141592653589793 float64
How can I perform the multiplication c*ones
in double precision, such that the resulting array is of dtype float64
? (Without multiplying each element by the constant individually, of course.)
(I've reviewed this related question/answer, which has some good information on the dtype resulting from Numpy scalar and array operations, but doesn't quite answer my question: Numpy casting float32 to float64)