diff --git a/marshal.go b/marshal.go index 5738615..e52f8b0 100644 --- a/marshal.go +++ b/marshal.go @@ -3,6 +3,7 @@ package ssob import ( "encoding/binary" "errors" + "math" "reflect" ) @@ -62,7 +63,7 @@ func MarshalUint32(in uint32) (ret []byte) { func MarshalFloat32(in float32) (ret []byte) { out := make([]byte, 4) - binary.BigEndian.PutUint32(out, uint32(in)) + binary.BigEndian.PutUint32(out, math.Float32bits(in)) return out } @@ -83,7 +84,7 @@ func MarshalUint64(in uint64) (ret []byte) { func MarshalFloat64(in float64) (ret []byte) { out := make([]byte, 8) - binary.BigEndian.PutUint64(out, uint64(in)) + binary.BigEndian.PutUint64(out, math.Float64bits(in)) return out } diff --git a/unmarshal.go b/unmarshal.go index 2c6aa68..0f1269d 100644 --- a/unmarshal.go +++ b/unmarshal.go @@ -3,6 +3,7 @@ package ssob import ( "encoding/binary" "errors" + "math" "reflect" ) @@ -82,7 +83,7 @@ func UnmarshalFloat32(in []byte) (ret float32, n int, err error) { return 0, 0, errors.New("ssob: Invalid input to decode float32") } - return float32(binary.BigEndian.Uint64(in[0:4])), 4, nil + return float32(math.Float32frombits(binary.BigEndian.Uint32(in[0:4]))), 4, nil } func UnmarshalInt64(in []byte) (ret int64, n int, err error) { @@ -106,7 +107,7 @@ func UnmarshalFloat64(in []byte) (ret float64, n int, err error) { return 0, 0, errors.New("ssob: Invalid input to decode float64") } - return float64(binary.BigEndian.Uint64(in[0:8])), 8, nil + return float64(math.Float64frombits(binary.BigEndian.Uint64(in[0:8]))), 8, nil } type unmarshalFunc func(e interface{}, in []byte) (n int, err error)