Fixed float handling

This commit is contained in:
Matthias Fulz 2020-05-24 19:05:04 +02:00
parent 00bcc6c5d2
commit 2318ba02ff
2 changed files with 6 additions and 4 deletions

View File

@ -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
}

View File

@ -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)