This repository was archived by the owner on Mar 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfloat.go
More file actions
75 lines (70 loc) · 1.87 KB
/
float.go
File metadata and controls
75 lines (70 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package cbor
import (
"bytes"
"encoding/binary"
"fmt"
"math"
)
// EncodeFloat encodes a 64-bit floating point value to a cbor value
func EncodeFloat(in float64) []byte {
u := math.Float64bits(in)
out := make([]byte, 9)
out[0] = 0xFB
binary.BigEndian.PutUint64(out[1:], u)
return out
}
// DecodeFloat16 decodes a cbor value to a 64-bit floating point value (the cbor value was 16-bit though)
func DecodeFloat16(_ byte, r *bytes.Reader) (interface{}, error) { // nolint: interfacer
b := make([]byte, 2)
n, err := r.Read(b)
if err != nil {
return nil, err
}
if n < 2 {
return nil, fmt.Errorf("expected 2 bytes of data, got only %d", n)
}
// The following conversion has been adapted from the CBOR specification in RFC 7049
half := (int(b[0]) << 8) + int(b[1])
exp := (half >> 10) & 0x1f
mant := half & 0x3ff
var val float64
if exp == 0 {
val = math.Ldexp(float64(mant), -24)
} else if exp != 31 {
val = math.Ldexp(float64(mant+1024), exp-25)
} else if mant == 0 {
val = math.Inf(0)
} else {
val = math.NaN()
}
if half&0x8000 != 0 {
return -val, nil
}
return val, nil
}
// DecodeFloat32 decodes a cbor value to a 32-bit floating point value
func DecodeFloat32(_ byte, r *bytes.Reader) (interface{}, error) { // nolint: interfacer
b := make([]byte, 4)
n, err := r.Read(b)
if err != nil {
return nil, err
}
if n < 4 {
return nil, fmt.Errorf("expected 4 bytes of data, got only %d", n)
}
ui := binary.BigEndian.Uint32(b)
return math.Float32frombits(ui), nil
}
// DecodeFloat64 decodes a cbor value to a 64-bit floating point value
func DecodeFloat64(_ byte, r *bytes.Reader) (interface{}, error) { // nolint: interfacer
b := make([]byte, 8)
n, err := r.Read(b)
if err != nil {
return nil, err
}
if n < 8 {
return nil, fmt.Errorf("expected 8 bytes of data, got only %d", n)
}
ui := binary.BigEndian.Uint64(b)
return math.Float64frombits(ui), nil
}