3

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)

JCOidl
  • 452
  • 1
  • 5
  • 12
  • `c = np.arccos([-1])` appears to do the trick. `ones = np.ones(3)` should also make a 64 array. – hpaulj Apr 03 '20 at 18:53
  • `result_array = c*ones.astype(np.float64)` will result in a float64 array, or just create the "ones" as a float64 array. – Hiho Apr 04 '20 at 04:29

0 Answers0