-
Notifications
You must be signed in to change notification settings - Fork 1
/
fixed.go
130 lines (103 loc) · 2.26 KB
/
fixed.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package fixed
import (
"encoding/binary"
"errors"
)
var ErrOverflow = errors.New("overflow")
func (x Fixed) String() string {
return x.format()
}
func New(val int) Fixed {
return fixed(int64(val))
}
func New64(val int64) Fixed {
return fixed(val)
}
func From(val float64) Fixed {
return from(val)
}
var (
One = fixedOne
Zero = Fixed{}
)
func (x Fixed) Abs() Fixed {
return x.abs()
}
func (x Fixed) Neg() Fixed {
return x.neg()
}
func (x Fixed) Floor() int64 {
return x.floor()
}
func (x Fixed) Ceil() int64 {
return x.ceil()
}
func (x Fixed) Round() int64 {
return x.round()
}
func (x Fixed) Float() float64 {
return x.float()
}
func (x Fixed) Mul(y Fixed) Fixed {
return mul(x, y)
}
func (x Fixed) Div(y Fixed) Fixed {
return div(x, y)
}
func (x Fixed) Add(y Fixed) Fixed {
return add(x, y)
}
func (x Fixed) Sub(y Fixed) Fixed {
return sub(x, y)
}
func (x Fixed) LessThan(y Fixed) bool {
return x.less(y)
}
// GreaterThan compares fixed values and returns true if x > y
func (x Fixed) GreaterThan(y Fixed) bool {
return x.greater(y)
}
// EqualTo compares fixed values and returns true if x == y
func (x Fixed) EqualTo(y Fixed) bool {
return x.equal(y)
}
func DivUint64(p, q uint64) Fixed {
return udiv(ufixed(p), ufixed(q))
}
// Div64 creates new Fixed equal to p/q signed result
func Div64(p, q int64) Fixed {
return div(fixed(p), fixed(q))
}
// FracFromBytes takes only fractional part from bytes array and return fixed value
func FracFromBytes(x []byte) Fixed {
return rawfixed(int64(binary.LittleEndian.Uint64(x)) & fracMask)
}
// FromBytes creates fixed value from bytes array
func FromBytes(x []byte) Fixed {
return Fixed{lo: binary.LittleEndian.Uint64(x[:8]), hi: binary.LittleEndian.Uint64(x[8:])}
}
// Bytes converts fixed value into bytes array
func (x Fixed) Bytes() []byte {
b := [16]byte{}
binary.LittleEndian.PutUint64(b[:8], x.lo)
binary.LittleEndian.PutUint64(b[8:], x.hi)
return b[:]
}
func BinCDF(n int, p Fixed, x int) Fixed {
if x < 0 {
return Zero
} else if x >= n {
return One
} else {
return incomplete(int64(n-x), int64(x+1), oneValue-p.fixed56())
}
}
func BinCDF64(n int64, p Fixed, x int64) Fixed {
if x < 0 {
return Zero
} else if x >= n {
return One
} else {
return incomplete(n-x, x+1, oneValue-p.fixed56())
}
}