forked from dimdin/go-decimal
/
dec.go
398 lines (367 loc) · 7.37 KB
/
dec.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright 2014 Dimitris Dinodimos. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package decimal implements the Dec and NullDec types suitable for financial and monetary calculations.
package decimal
import (
"bytes"
"database/sql/driver"
"errors"
"io"
"math"
"strconv"
"strings"
)
// Dec is represented as an 128 bit integer scaled by a power of ten.
type Dec struct {
coef Int128
scale uint8
}
func New(n int64) *Dec {
d := new(Dec)
d.SetInt64(n)
return d
}
var decOne = New(1)
// Set sets d to x and returns d.
func (d *Dec) Set(x *Dec) *Dec {
if d != x {
d.coef = x.coef
d.scale = x.scale
}
return d
}
// SetInt128 sets d to x and returns d.
func (d *Dec) SetInt128(x *Int128) *Dec {
d.scale = 0
d.coef = *x
return d
}
// Sign returns:
//
// -1 if d < 0
// 0 if d == 0
// +1 if d > 0
//
func (d Dec) Sign() int {
return d.coef.Sign()
}
func exp10(scale uint8) *Int128 {
switch scale {
case 0:
return intOne
case 1:
return intTen
case 2:
return &Int128{100, 0}
case 3:
return &Int128{1000, 0}
case 4:
return &Int128{10000, 0}
case 5:
return &Int128{100000, 0}
case 6:
return &Int128{1000000, 0}
case 7:
return &Int128{10000000, 0}
case 8:
return &Int128{100000000, 0}
}
var z Int128
z.Power(intTen, uint(scale))
return &z
}
func (d *Dec) rescale(scale uint8) *Dec {
if scale == d.scale {
return d
} else if scale < d.scale {
panic("loss of precision")
}
z := d.coef
z.Mul(&z, exp10(scale-d.scale))
return &Dec{
coef: z,
scale: scale,
}
}
func maxscale(x, y *Dec) (*Dec, *Dec) {
if x.scale == y.scale {
return x, y
}
if x.scale > y.scale {
return x, y.rescale(x.scale)
}
return x.rescale(y.scale), y
}
// Cmp compares x and y and returns:
//
// -1 if x < y
// 0 if x == y
// +1 if x > y
//
func (x Dec) Cmp(y *Dec) int {
dx, dy := maxscale(&x, y)
return dx.coef.Cmp(&dy.coef)
}
// Abs sets d to |x| (the absolute value of x) and returns d.
func (d *Dec) Abs(x *Dec) *Dec {
d.coef.Abs(&x.coef)
if d != x {
d.scale = x.scale
}
return d
}
// Neg sets d to -x and returns d.
func (d *Dec) Neg(x *Dec) *Dec {
d.coef.Neg(&x.coef)
if d != x {
d.scale = x.scale
}
return d
}
// Add sets d to the sum x+y and returns d.
// The scale of d is the larger of the scales of the two operands.
func (d *Dec) Add(x, y *Dec) *Dec {
dx, dy := maxscale(x, y)
if d != dx {
d.scale = dx.scale
}
d.coef.Add(&dx.coef, &dy.coef)
return d
}
// Sub sets d to the difference x-y and returns d.
// The scale of d is the larger of the scales of the two operands.
func (d *Dec) Sub(x, y *Dec) *Dec {
dx, dy := maxscale(x, y)
if d != dx {
d.scale = dx.scale
}
d.coef.Sub(&dx.coef, &dy.coef)
return d
}
// Mul sets d to the product x*y and returns d.
// The scale of d is the sum of the scales of the two operands.
func (d *Dec) Mul(x, y *Dec) *Dec {
d.scale = x.scale + y.scale
d.coef.Mul(&x.coef, &y.coef)
return d
}
// Div sets d to the rounded quotient x/y and returns d.
// If y is zero panics with Division by zero.
// The resulting value is rounded half up to the given scale.
func (d *Dec) Div(x, y *Dec, scale uint8) *Dec {
shift := int(scale) - int(x.scale) + int(y.scale)
var sx, sy *Int128
if shift > 0 {
var z Int128
sx = z.Mul(&x.coef, exp10(uint8(shift)))
sy = &y.coef
} else if shift < 0 {
sx = &x.coef
var z Int128
sy = z.Mul(&y.coef, exp10(uint8(-shift)))
} else {
sx = &x.coef
sy = &y.coef
}
d.scale = scale
var r Int128
d.coef.DivMod(sx, sy, &r)
var roundUp bool
if r.Sign() != 0 {
r.Abs(&r)
var v Int128
v.Abs(sy)
roundUp = r.Add(&r, &r).Cmp(&v) >= 0
}
if roundUp {
if d.coef.Sign() < 0 {
d.coef.Sub(&d.coef, intOne)
} else {
d.coef.Add(&d.coef, intOne)
}
}
return d
}
// Round d half up to the given scale and returns d
func (d *Dec) Round(scale uint8) *Dec {
if d.scale <= scale {
return d
}
return d.Div(d, decOne, scale)
}
// Power sets d = x**n and returns d
func (d *Dec) Power(x *Dec, n int) *Dec {
if n < 0 {
scale := x.scale
d.Power(x, -n)
return d.Div(decOne, d, scale-uint8(n))
} else if n == 0 {
return d.Set(decOne)
} else if n == 1 {
return d.Set(x)
} else if (n & 1) == 0 { // n even
d.Mul(x, x)
if d.scale > 18 {
d.Round(18)
}
return d.Power(d, n/2)
}
// n odd
var z Dec
z.Set(x)
d.Mul(x, x)
if d.scale > 18 {
d.Round(18)
}
d.Power(d, (n-1)/2)
return d.Mul(d, &z)
}
// String returns the value of d
func (d Dec) String() string {
return string(d.Bytes())
}
// Bytes returns the value of d
func (d Dec) Bytes() []byte {
var dec Int128
dec.Abs(&d.coef)
digits := make([]byte, 0, 30)
for i := 0; dec.Sign() != 0; i++ {
var z Int128
dec.DivMod(&dec, intTen, &z)
digits = append(digits, byte(z.Int64()+'0'))
}
for int(d.scale) >= len(digits) {
digits = append(digits, '0')
}
dst := make([]byte, 0, len(digits)+2)
if d.Sign() < 0 {
dst = append(dst, '-')
}
for i := len(digits); i != 0; i-- {
if i == int(d.scale) {
dst = append(dst, '.')
}
dst = append(dst, digits[i-1])
}
return dst
}
// SetFloat64 sets d to the value of f
func (d *Dec) SetFloat64(f float64) error {
return d.SetString(strconv.FormatFloat(f, 'f', -1, 64))
}
// SetInt64 sets d to the value of i and returns d.
func (d *Dec) SetInt64(i int64) *Dec {
d.coef.SetInt64(i)
return d
}
// SetString sets d to the value of s
func (d *Dec) SetString(s string) error {
if len(s) == 0 {
return errors.New("SetString: empty string")
}
r := strings.NewReader(s)
err := d.scan(r)
if err != nil {
return err
}
_, _, err = r.ReadRune()
if err != io.EOF {
return errors.New("SetString: non digit")
}
return nil
}
// SetBytes sets d to the value of buf
func (d *Dec) SetBytes(buf []byte) error {
if len(buf) == 0 {
return errors.New("SetBytes: empty buffer")
}
r := bytes.NewReader(buf)
err := d.scan(r)
if err != nil {
return err
}
_, _, err = r.ReadRune()
if err != io.EOF {
return errors.New("SetBytes: non digit")
}
return nil
}
func (d *Dec) scan(r io.RuneScanner) error {
d.coef.hi = 0
d.coef.lo = 0
d.scale = 0
ch, _, err := r.ReadRune()
if err != nil {
return err
}
var neg bool
switch ch {
case '-':
neg = true
case '+':
default:
r.UnreadRune()
}
var dec bool
for {
ch, _, err = r.ReadRune()
if err == io.EOF {
goto ExitLoop
}
if err != nil {
return err
}
switch {
case ch == '.':
if dec {
r.UnreadRune()
goto ExitLoop
}
dec = true
case ch >= '0' && ch <= '9':
d.coef.Mul(&d.coef, intTen)
var z Int128
z.SetInt64(int64(ch - '0'))
d.coef.Add(&d.coef, &z)
if dec {
d.scale++
}
default:
r.UnreadRune()
goto ExitLoop
}
}
ExitLoop:
if neg {
d.Neg(d)
}
return nil
}
// Float64 returns the nearest float64 representation of d.
func (d Dec) Float64() float64 {
return d.coef.Float64() / math.Pow10(int(d.scale))
}
// Scan implements the database Scanner interface.
func (d *Dec) Scan(value interface{}) error {
if value == nil {
return errors.New("Cannot Scan null into Dec")
}
switch value := value.(type) {
case []byte:
return d.SetBytes(value)
case string:
return d.SetString(value)
case int64:
d.SetInt64(value)
return nil
case float64:
return d.SetFloat64(value)
default:
return errors.New("Invalid type Scan into Dec")
}
}
// Value implements the database driver Valuer interface.
func (d Dec) Value() (driver.Value, error) {
return d.Bytes(), nil
}