-
Notifications
You must be signed in to change notification settings - Fork 3
/
float.go
69 lines (60 loc) · 2.07 KB
/
float.go
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
package varint
import "math"
// MarshalFloat64 fills bs with the MUS encoding (Varint) of a float64 value.
//
// Returns the number of used bytes. It will panic if receives too small bs.
func MarshalFloat64(v float64, bs []byte) int {
return MarshalUint64(math.Float64bits(v), bs)
}
// MarshalFloat32 fills bs with the MUS encoding (Varint) of a float32 value.
//
// Returns the number of used bytes. It will panic if receives too small bs.
func MarshalFloat32(v float32, bs []byte) int {
return MarshalUint32(math.Float32bits(v), bs)
}
// UnmarshalFloat64 parses a MUS-encoded (Varint) float64 value from bs.
//
// In addition to the float64 value and the number of used bytes, it can also
// return mus.ErrTooSmallByteSlice or com.ErrOverflow.
func UnmarshalFloat64(bs []byte) (v float64, n int, err error) {
uv, n, err := UnmarshalUint64(bs)
if err != nil {
return
}
v = math.Float64frombits(uv)
return
}
// UnmarshalFloat32 parses a MUS-encoded (Varint) float32 value from bs.
//
// In addition to the float32 value and the number of used bytes, it can also
// return mus.ErrTooSmallByteSlice or com.ErrOverflow.
func UnmarshalFloat32(bs []byte) (v float32, n int, err error) {
uv, n, err := UnmarshalUint32(bs)
if err != nil {
return
}
v = math.Float32frombits(uv)
return
}
// SizeFloat64 returns the size of a MUS-encoded (Varint) float64 value.
func SizeFloat64(v float64) (size int) {
return SizeUint64(math.Float64bits(v))
}
// SizeFloat32 returns the size of a MUS-encoded (Varint) float32 value.
func SizeFloat32(v float32) (size int) {
return SizeUint32(math.Float32bits(v))
}
// SkipFloat64 skips a MUS-encoded (Varint) float64 value.
//
// In addition to the number of skipped bytes, it can also return
// mus.ErrTooSmallByteSlice or com.ErrOverflow.
func SkipFloat64(bs []byte) (n int, err error) {
return SkipUint64(bs)
}
// SkipFloat32 skips a MUS-encoded (Varint) float32 value.
//
// In addition to the number of skipped bytes, it can also return
// mus.ErrTooSmallByteSlice or com.ErrOverflow.
func SkipFloat32(bs []byte) (n int, err error) {
return SkipUint32(bs)
}