How do I parse a binary into a 32 bit float?

I’m playing around with binaries, and in the docs it says that floats can be either 64, 32, or 16 bits. I can parse 64 bits just fine, but I can’t figure out how to parse 32:

55> <<A/float>> = <<1,17,0,42,43,11,24,11>>.
<<1,17,0,42,43,11,24,11>>
56> A.
1.5494220711692294e-303
57> <<B/float>> = <<1,17,0,42>>.            
** exception error: no match of right hand side value <<1,17,0,42>>

How am I supposed to write it?

1 Like

Add the size in bits:
<<B:32/float>> = <<1,17,0,42>>.

The complete bit syntax is here:
https://www.erlang.org/doc/programming_examples/bit_syntax.html#

3 Likes

Ah, so 64 bits is the default, gotcha. Thanks!

1 Like