diff --git a/tape/decode.go b/tape/decode.go index 466324b..bcf3fd1 100644 --- a/tape/decode.go +++ b/tape/decode.go @@ -1,6 +1,7 @@ package tape import "io" +import "math" // Decodable is any type that can decode itself from a decoder. type Decodable interface { @@ -125,3 +126,17 @@ func (this *Decoder) ReadGBEU() (value uint64, n int, err error) { } } } + +// ReadFloat32 decldes a 32-bit floating point value from the input reader. +func (this *Decoder) ReadFloat32() (value float32, n int, err error) { + bits, nn, err := this.ReadUint32() + n += nn; if err != nil { return 0, n, err } + return math.Float32frombits(bits), n, nil +} + +// ReadFloat64 decldes a 64-bit floating point value from the input reader. +func (this *Decoder) ReadFloat64() (value float64, n int, err error) { + bits, nn, err := this.ReadUint64() + n += nn; if err != nil { return 0, n, err } + return math.Float64frombits(bits), n, nil +} diff --git a/tape/encode.go b/tape/encode.go index 121f1e9..b84c70f 100644 --- a/tape/encode.go +++ b/tape/encode.go @@ -1,6 +1,7 @@ package tape import "io" +import "math" // Encodable is any type that can write itself to an encoder. type Encodable interface { @@ -118,3 +119,13 @@ func (this *Encoder) EncodeGBEU(value uint64) (n int, err error) { return this.Write(buffer[:]) } + +// WriteFloat32 encodes a 32-bit floating point value to the output writer. +func (this *Encoder) WriteFloat32(value float32) (n int, err error) { + return this.WriteUint32(math.Float32bits(value)) +} + +// WriteFloat64 encodes a 64-bit floating point value to the output writer. +func (this *Encoder) WriteFloat64(value float64) (n int, err error) { + return this.WriteUint64(math.Float64bits(value)) +}