1package ieee754
2
3import (
4 "encoding/binary"
5 "io"
6 "math"
7)
8
9// DecodeFloat32 decodes a float32 in IEEE 754 binary representation.
10// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#floating-point%E2%91%A2
11func DecodeFloat32(buf []byte) (float32, error) {
12 if len(buf) < 4 {
13 return 0, io.ErrUnexpectedEOF
14 }
15
16 raw := binary.LittleEndian.Uint32(buf[:4])
17 return math.Float32frombits(raw), nil
18}
19
20// DecodeFloat64 decodes a float64 in IEEE 754 binary representation.
21// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#floating-point%E2%91%A2
22func DecodeFloat64(buf []byte) (float64, error) {
23 if len(buf) < 8 {
24 return 0, io.ErrUnexpectedEOF
25 }
26
27 raw := binary.LittleEndian.Uint64(buf)
28 return math.Float64frombits(raw), nil
29}