-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdec128.go
265 lines (217 loc) · 5.83 KB
/
dec128.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Package dec128 provides 128-bit fixed-point decimal type, operations and constants.
package dec128
import (
"github.com/jokruger/dec128/errors"
"github.com/jokruger/dec128/uint128"
)
// Dec128 represents a 128-bit fixed-point decimal number.
type Dec128 struct {
coef uint128.Uint128
exp uint8
err errors.Error
neg bool
}
// New creates a new Dec128 from a uint64 coefficient, uint8 exponent, and negative flag.
// In case of errors it returns NaN with the error.
func New(coef uint128.Uint128, exp uint8, neg bool) Dec128 {
if exp > MaxPrecision {
return NaN(errors.PrecisionOutOfRange)
}
if coef.IsZero() && exp == 0 {
return Zero
}
return Dec128{coef: coef, exp: exp, neg: neg}
}
// NaN returns a Dec128 with the given error.
func NaN(reason errors.Error) Dec128 {
if reason == errors.None {
return Dec128{err: errors.NotANumber}
}
return Dec128{err: reason}
}
// IsZero returns true if the Dec128 is zero.
// If the Dec128 is NaN, it returns false.
func (self Dec128) IsZero() bool {
return self.err == errors.None && self.coef.IsZero()
}
// IsNegative returns true if the Dec128 is negative and false otherwise.
// If the Dec128 is NaN, it returns false.
func (self Dec128) IsNegative() bool {
return self.neg && self.err == errors.None && !self.coef.IsZero()
}
// IsPositive returns true if the Dec128 is positive and false otherwise.
// If the Dec128 is NaN, it returns false.
func (self Dec128) IsPositive() bool {
return !self.neg && self.err == errors.None && !self.coef.IsZero()
}
// IsNaN returns true if the Dec128 is NaN.
func (self Dec128) IsNaN() bool {
return self.err != errors.None
}
// ErrorDetails returns the error details of the Dec128.
// If the Dec128 is not NaN, it returns nil.
func (self Dec128) ErrorDetails() error {
return self.err.Value()
}
// Sign returns -1 if the Dec128 is negative, 0 if it is zero, and 1 if it is positive.
func (self Dec128) Sign() int {
if self.err != errors.None || self.IsZero() {
return 0
}
if self.IsNegative() {
return -1
}
return 1
}
// Precision returns the precision of the Dec128.
func (self Dec128) Precision() uint8 {
return self.exp
}
// Rescale returns a new Dec128 with the given precision.
// If the Dec128 is NaN, it returns itself.
// In case of errors it returns NaN with the error.
func (self Dec128) Rescale(prec uint8) Dec128 {
if self.err != errors.None {
return self
}
if self.exp == prec {
return self
}
if prec > MaxPrecision {
return NaN(errors.PrecisionOutOfRange)
}
if prec > self.exp {
// scale up
diff := prec - self.exp
coef, err := self.coef.Mul64(Pow10Uint64[diff])
if err != errors.None {
return NaN(err)
}
return Dec128{coef: coef, exp: prec, neg: self.neg}
}
// scale down
diff := self.exp - prec
coef, err := self.coef.Div64(Pow10Uint64[diff])
if err != errors.None {
return NaN(err)
}
return Dec128{coef: coef, exp: prec, neg: self.neg}
}
// Equal returns true if the Dec128 is equal to the other Dec128.
func (self Dec128) Equal(other Dec128) bool {
if self.err != errors.None && other.err != errors.None {
return true
}
if self.err != errors.None || other.err != errors.None {
return false
}
if self.neg != other.neg {
return false
}
if self.exp == other.exp {
return self.coef.Equal(other.coef)
}
prec := max(self.exp, other.exp)
a := self.Rescale(prec)
b := other.Rescale(prec)
if !a.IsNaN() && !b.IsNaN() {
return a.coef.Equal(b.coef)
}
return false
}
// Compare returns -1 if the Dec128 is less than the other Dec128, 0 if they are equal, and 1 if the Dec128 is greater than the other Dec128.
// NaN is considered less than any valid Dec128.
func (self Dec128) Compare(other Dec128) int {
if self.err != errors.None && other.err != errors.None {
return 0
}
if self.err != errors.None {
return -1
}
if other.err != errors.None {
return 1
}
if self.neg && !other.neg {
return -1
}
if !self.neg && other.neg {
return 1
}
if self.exp == other.exp {
if self.neg {
return -self.coef.Compare(other.coef)
}
return self.coef.Compare(other.coef)
}
prec := max(self.exp, other.exp)
a := self.Rescale(prec)
if a.IsNaN() {
return 1
}
b := other.Rescale(prec)
if b.IsNaN() {
return -1
}
if a.neg {
return -a.coef.Compare(b.coef)
}
return a.coef.Compare(b.coef)
}
// Canonical returns a new Dec128 with the canonical representation.
// If the Dec128 is NaN, it returns itself.
func (self Dec128) Canonical() Dec128 {
if self.err != errors.None {
return Dec128{err: self.err}
}
if self.IsZero() {
return Zero
}
if self.exp == 0 {
return self
}
coef := self.coef
exp := self.exp
for {
t, r, err := coef.QuoRem64(10)
if err != errors.None || r != 0 {
break
}
coef = t
exp--
if exp == 0 {
break
}
}
return Dec128{coef: coef, exp: exp, neg: self.neg}
}
// Exponent returns the exponent of the Dec128.
func (self Dec128) Exponent() uint8 {
return self.exp
}
// Coefficient returns the coefficient of the Dec128.
func (self Dec128) Coefficient() uint128.Uint128 {
return self.coef
}
// LessThan returns true if the Dec128 is less than the other Dec128.
func (self Dec128) LessThan(other Dec128) bool {
return self.Compare(other) < 0
}
// LessThanOrEqual returns true if the Dec128 is less than or equal to the other Dec128.
func (self Dec128) LessThanOrEqual(other Dec128) bool {
return self.Compare(other) <= 0
}
// GreaterThan returns true if the Dec128 is greater than the other Dec128.
func (self Dec128) GreaterThan(other Dec128) bool {
return self.Compare(other) > 0
}
// GreaterThanOrEqual returns true if the Dec128 is greater than or equal to the other Dec128.
func (self Dec128) GreaterThanOrEqual(other Dec128) bool {
return self.Compare(other) >= 0
}
// Copy returns a copy of the Dec128.
func (self Dec128) Copy() Dec128 {
if self.err != errors.None {
return self
}
return Dec128{coef: self.coef, exp: self.exp, neg: self.neg}
}