How to reinterpret Float64 as two Float32's

question

#1

reinterpret can slice an Int64 into two Int32’s. Is there a way to do this for floats?

Relatedly, is there an inverse of bits?


#2

You can do this by wrapping the Float64 in an array:

julia> reinterpret(Float32, [1.0])
2-element Array{Float32,1}:
 0.0  
 1.875

#3

Yes, and that’s useful, but I doubt it is for floats, even though you can (just interpreting the upper and lower bits).

I’m just curious, what are you really trying to do?


#4

I have some data I saved off an instrument. The file format is not well documented. I knew that the file starts with the model and serial number of the instrument, and then a list of the instrument settings, which are a mix of Int32’s, Float32’s and Float64’s. As I read out the file, if I get a value that seems wacky, I want to reinterpret it.


#5

To avoid the array allocation overhead, you can also do:

u = reinterpret(UInt64, x)
y1 = reinterpret(Float32, (u >> 32) % UInt32)
y2 = reinterpret(Float32, u % UInt32)