How to convert vectors and arrays from Float32 to Float64?

I have an Array A that is in Float32. My goal is to convert it from Float32 to Float64. I tried the below and neither of them worked:
B = float(A);
B = convert(Float64, A)

When I tried to check the type using typeof, it still shows Float32.

What should be the correct way to do the conversion? Many thanks!

1 Like

Float64.(A). The difference is Float64(A) tries to convert the array to a Float64 while the . broadcasts the operation to the elements of A.

7 Likes

Got you! Many thanks for the quick solution :+1:

You can also use convert but with the “full type”:

julia> convert(Vector{Float64}, x_f32)
2-element Vector{Float64}:
 1.0
 2.0

julia> convert(Vector{Float64}, x_f64)
2-element Vector{Float64}:
 1.0
 2.0

The advantage over Float64.(x) is that it is a no-op if the data is already of the correct type:

julia> convert(Vector{Float64}, x_f64) === x_f64
true
9 Likes