-
Notifications
You must be signed in to change notification settings - Fork 8
/
dnum.go
784 lines (717 loc) · 14.6 KB
/
dnum.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
// Copyright Suneido Software Corp. All rights reserved.
// Governed by the MIT license found in the LICENSE file.
/*
Package dnum implements decimal floating point numbers.
Uses uint64 to hold the coefficient and int8 for exponent.
Only uses 16 decimal digits.
Value is sign * .coef * 10^exp, i.e. assumed decimal to left
Coefficient is kept "maximized" in 16 decimal digits.
Zeroed value is 0.
*/
package dnum
import (
"bytes"
"math"
"math/bits"
"strconv"
"strings"
)
// Dnum is a decimal floating point number
type Dnum struct {
coef uint64
sign int8
exp int8
}
const (
signPosInf = +2
signPos = +1
signZero = 0
signNeg = -1
signNegInf = -2
expMin = math.MinInt8
expMax = math.MaxInt8
coefMin = 1000_0000_0000_0000
coefMax = 9999_9999_9999_9999
digitsMax = 16
shiftMax = digitsMax - 1
)
// common values
var (
Zero = Dnum{}
One = Dnum{1000_0000_0000_0000, signPos, 1}
NegOne = Dnum{1000_0000_0000_0000, signNeg, 1}
PosInf = Dnum{1, signPosInf, 0}
NegInf = Dnum{1, signNegInf, 0}
)
var pow10 = [...]uint64{
1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000,
100000000000,
1000000000000,
10000000000000,
100000000000000,
1000000000000000,
10000000000000000,
100000000000000000,
1000000000000000000}
var halfpow10 = [...]uint64{
0,
5,
50,
500,
5000,
50000,
500000,
5000000,
50000000,
500000000,
5000000000,
50000000000,
500000000000,
5000000000000,
50000000000000,
500000000000000,
5000000000000000,
50000000000000000,
500000000000000000,
5000000000000000000}
// NOTE: comment out body in production
func check( /*cond*/ bool) {
// if !cond {
// panic("check failed")
// }
}
// FromInt returns a Dnum for an int
func FromInt(n int64) Dnum {
if n == 0 {
return Zero
}
n0 := n
sign := int8(signPos)
if n < 0 {
n = -n
sign = signNeg
}
dn := New(sign, uint64(n), digitsMax)
check(reversible(n0, dn))
return dn
}
func reversible(n int64, dn Dnum) bool {
n2, ok := dn.ToInt64()
return ok && n2 == n
}
const log2of10 = 3.32192809488736234
// FromFloat converts a float64 to a Dnum
func FromFloat(f float64) Dnum {
switch {
case math.IsInf(f, +1):
return PosInf
case math.IsInf(f, -1):
return NegInf
case math.IsNaN(f):
panic("dnum.FromFloat can't convert NaN")
}
n := int64(f)
if f == float64(n) {
return FromInt(n)
}
sign := int8(signPos)
if f < 0 {
f = -f
sign = signNeg
}
_, e := math.Frexp(f)
e = int(float32(e) / log2of10)
c := uint64(f / math.Pow(10, float64(e-16)))
return New(sign, c, e)
}
// Raw constructs a Dnum without normalizing - arguments must be valid.
// Used by SuDnum Unpack
func Raw(sign int8, coef uint64, exp int) Dnum {
return Dnum{coef, sign, int8(exp)}
}
// New constructs a Dnum, maximizing coef and handling exp out of range
// Used to normalize results of operations
func New(sign int8, coef uint64, exp int) Dnum {
if sign == 0 || coef == 0 || exp < expMin {
return Zero
} else if sign == signPosInf {
return PosInf
} else if sign == signNegInf {
return NegInf
} else {
atmax := false
for coef > coefMax {
coef = (coef + 5) / 10 // drop/round least significant digit
exp++
atmax = true
}
if !atmax {
p := maxShift(coef)
coef *= pow10[p]
exp -= p
}
if exp > expMax {
return Inf(sign)
}
return Dnum{coef, sign, int8(exp)}
}
}
func maxShift(x uint64) int {
i := ilog10(x)
if i > shiftMax {
return 0
}
return shiftMax - i
}
func ilog10(x uint64) int {
// based on Hacker's Delight
if x == 0 {
return 0
}
y := (19 * (63 - bits.LeadingZeros64(x))) >> 6
if y < 18 && x >= pow10[y+1] {
y++
}
return y
}
func Inf(sign int8) Dnum {
switch {
case sign < 0:
return NegInf
case sign > 0:
return PosInf
default:
return Zero
}
}
// String returns a string representation of the Dnum
func (dn Dnum) String() string {
if dn.sign == 0 {
return "0"
}
const maxLeadingZeros = 7
sign := ""
if dn.sign < 0 {
sign = "-"
}
if dn.IsInf() {
return sign + "inf"
}
digits := getDigits(dn.coef)
nd := len(digits)
e := int(dn.exp) - nd
if -maxLeadingZeros <= dn.exp && dn.exp <= 0 {
// decimal to the left
return sign + "." + strings.Repeat("0", -e-nd) + digits
} else if -nd < e && e <= -1 {
// decimal within
dec := nd + e
return sign + digits[:dec] + "." + digits[dec:]
} else if 0 < dn.exp && dn.exp <= digitsMax {
// decimal to the right
return sign + digits + strings.Repeat("0", e)
} else {
// scientific notation
after := ""
if nd > 1 {
after = "." + digits[1:]
}
return sign + digits[:1] + after + "e" + strconv.Itoa(int(dn.exp-1))
}
}
func getDigits(coef uint64) string {
var digits [digitsMax]byte
i := shiftMax
nd := 0
for coef != 0 {
digits[nd] = byte('0' + (coef / pow10[i]))
coef %= pow10[i]
nd++
i--
}
return string(digits[:nd])
}
// FromStr parses a numeric string and returns a Dnum representation.
// It panics for invalid input.
func FromStr(s string) Dnum {
r := &reader{s, 0}
sign := getSign(r)
if r.matchStr("inf") {
return Inf(sign)
}
coef, exp := getCoef(r)
exp += getExp(r)
if r.len() != 0 { // didn't consume entire string
panic("invalid number")
} else if coef == 0 || exp < math.MinInt8 {
return Zero
} else if exp > math.MaxInt8 {
return Inf(sign)
}
check(coefMin <= coef && coef <= coefMax)
return Dnum{coef, sign, int8(exp)}
}
type reader struct {
s string
i int
}
func (r *reader) cur() byte {
if r.i >= len(r.s) {
return 0
}
return byte(r.s[r.i])
}
func (r *reader) prev() byte {
if r.i == 0 {
return 0
}
return byte(r.s[r.i-1])
}
func (r *reader) len() int {
return len(r.s) - r.i
}
func (r *reader) match(c byte) bool {
if r.cur() == c {
r.i++
return true
}
return false
}
func (r *reader) matchDigit() bool {
c := r.cur()
if '0' <= c && c <= '9' {
r.i++
return true
}
return false
}
func (r *reader) matchStr(pre string) bool {
if strings.HasPrefix(r.s[r.i:], pre) {
r.i += len(pre)
return true
}
return false
}
func getSign(r *reader) int8 {
if r.match('-') {
return int8(signNeg)
}
r.match('+')
return int8(signPos)
}
func getCoef(r *reader) (uint64, int) {
digits := false
beforeDecimal := true
for r.match('0') {
digits = true
}
if r.cur() == '.' && r.len() > 1 {
digits = false
}
n := uint64(0)
exp := 0
p := shiftMax
for {
c := r.cur()
if r.matchDigit() {
digits = true
// ignore extra decimal places
if c != '0' && p >= 0 {
n += uint64(c-'0') * pow10[p]
}
p--
} else if beforeDecimal {
// decimal point or end
exp = shiftMax - p
if !r.match('.') {
break
}
beforeDecimal = false
if !digits {
for r.match('0') {
digits = true
exp--
}
}
} else {
break
}
}
if !digits {
panic("numbers require at least one digit")
}
return n, exp
}
func getExp(r *reader) int {
e := 0
if r.match('e') || r.match('E') {
esign := getSign(r)
for r.matchDigit() {
e = e*10 + int(r.prev()-'0')
}
e *= int(esign)
}
return e
}
// end of FromStr ---------------------------------------------------
// IsInf returns true if a Dnum is positive or negative infinite
func (dn Dnum) IsInf() bool {
return dn.sign == signPosInf || dn.sign == signNegInf
}
// IsZero returns true if a Dnum is zero
func (dn Dnum) IsZero() bool {
return dn.sign == signZero
}
// ToFloat converts a Dnum to float64
func (dn Dnum) ToFloat() float64 {
if dn.IsInf() {
return math.Inf(int(dn.sign))
}
g := float64(dn.coef)
if dn.sign == signNeg {
g = -g
}
e := math.Pow10(int(dn.exp) - digitsMax)
return g * e
}
// ToInt64 converts a Dnum to an int64, returning whether it was convertible
func (dn Dnum) ToInt64() (int64, bool) {
if dn.sign == 0 {
return 0, true
}
if dn.sign != signNegInf && dn.sign != signPosInf {
if 0 < dn.exp && dn.exp < digitsMax &&
(dn.coef%pow10[digitsMax-dn.exp]) == 0 { // usual case
return int64(dn.sign) * int64(dn.coef/pow10[digitsMax-dn.exp]), true
}
if dn.exp == digitsMax {
return int64(dn.sign) * int64(dn.coef), true
}
if dn.exp == digitsMax+1 {
return int64(dn.sign) * (int64(dn.coef) * 10), true
}
if dn.exp == digitsMax+2 {
return int64(dn.sign) * (int64(dn.coef) * 100), true
}
if dn.exp == digitsMax+3 && dn.coef < math.MaxInt64/1000 {
return int64(dn.sign) * (int64(dn.coef) * 1000), true
}
}
return 0, false
}
func (dn Dnum) ToInt() (int, bool) {
// if int is int64, this is a nop
n, ok := dn.ToInt64()
if !ok || int64(int(n)) != n {
return 0, false
}
return int(n), true
}
// Sign returns -1 for negative, 0 for zero, and +1 for positive
func (dn Dnum) Sign() int {
return int(dn.sign)
}
// Coef returns the coefficient
func (dn Dnum) Coef() uint64 {
return dn.coef
}
// Exp returns the exponent
func (dn Dnum) Exp() int {
return int(dn.exp)
}
// Frac returns the fractional portion, i.e. x - x.Int()
func (dn Dnum) Frac() Dnum {
if dn.sign == 0 || dn.sign == signNegInf || dn.sign == signPosInf ||
dn.exp >= digitsMax {
return Zero
}
if dn.exp <= 0 {
return dn
}
frac := dn.coef % pow10[digitsMax-dn.exp]
if frac == dn.coef {
return dn
}
return New(dn.sign, frac, int(dn.exp))
}
type RoundingMode int
const (
Up RoundingMode = iota
Down
HalfUp
)
// Trunc returns the integer portion (truncating any fractional part)
func (dn Dnum) Trunc() Dnum {
return dn.integer(Down)
}
func (dn Dnum) integer(mode RoundingMode) Dnum {
if dn.sign == 0 || dn.sign == signNegInf || dn.sign == signPosInf ||
dn.exp >= digitsMax {
return dn
}
if dn.exp <= 0 {
if mode == Up ||
(mode == HalfUp && dn.exp == 0 && dn.coef >= One.coef*5) {
return New(dn.sign, One.coef, int(dn.exp)+1)
}
return Zero
}
e := digitsMax - dn.exp
frac := dn.coef % pow10[e]
if frac == 0 {
return dn
}
i := dn.coef - frac
if (mode == Up && frac > 0) || (mode == HalfUp && frac >= halfpow10[e]) {
return New(dn.sign, i+pow10[e], int(dn.exp)) // normalize
}
return Dnum{i, dn.sign, dn.exp}
}
func (dn Dnum) Round(r int, mode RoundingMode) Dnum {
if dn.sign == 0 || dn.sign == signNegInf || dn.sign == signPosInf ||
r >= digitsMax {
return dn
}
if r <= -digitsMax {
return Zero
}
n := New(dn.sign, dn.coef, int(dn.exp)+r) // multiply by 10^r
n = n.integer(mode)
if n.sign == signPos || n.sign == signNeg { // i.e. not zero or inf
return New(n.sign, n.coef, int(n.exp)-r)
}
return n
}
// arithmetic operations -------------------------------------------------------
// Neg returns the Dnum negated i.e. sign reversed
func (dn Dnum) Neg() Dnum {
return Dnum{dn.coef, -dn.sign, dn.exp}
}
// Abs returns the Dnum with a positive sign
func (dn Dnum) Abs() Dnum {
if dn.sign < 0 {
return Dnum{dn.coef, -dn.sign, dn.exp}
}
return dn
}
// Equal returns true if two Dnum's are equal
func Equal(x, y Dnum) bool {
return x.sign == y.sign && x.exp == y.exp && x.coef == y.coef
}
// Compare compares two Dnum's returning -1 for <, 0 for ==, +1 for >
func Compare(x, y Dnum) int {
switch {
case x.sign < y.sign:
return -1
case x.sign > y.sign:
return 1
case x == y:
return 0
}
sign := int(x.sign)
switch {
case sign == 0 || sign == signNegInf || sign == signPosInf:
return 0
case x.exp < y.exp:
return -sign
case x.exp > y.exp:
return +sign
case x.coef < y.coef:
return -sign
case x.coef > y.coef:
return +sign
default:
return 0
}
}
// Sub returns the difference of two Dnum's
func Sub(x, y Dnum) Dnum {
return Add(x, y.Neg())
}
// Add returns the sum of two Dnum's
func Add(x, y Dnum) Dnum {
switch {
case x.sign == signZero:
return y
case y.sign == signZero:
return x
case x.IsInf():
if y.sign == -x.sign {
return Zero
}
return x
case y.IsInf():
return y
}
if !align(&x, &y) {
return x
}
if x.sign != y.sign {
return usub(x, y)
}
return uadd(x, y)
}
func uadd(x, y Dnum) Dnum {
return New(x.sign, x.coef+y.coef, int(x.exp))
}
func usub(x, y Dnum) Dnum {
if x.coef < y.coef {
return New(-x.sign, y.coef-x.coef, int(x.exp))
}
return New(x.sign, x.coef-y.coef, int(x.exp))
}
func align(x, y *Dnum) bool {
if x.exp == y.exp {
return true
}
if x.exp < y.exp {
*x, *y = *y, *x // swap
}
yshift := ilog10(y.coef)
e := int(x.exp - y.exp)
if e > yshift {
return false
}
yshift = e
check(0 <= yshift && yshift <= 20)
y.coef = (y.coef + halfpow10[yshift]) / pow10[yshift]
check(int(y.exp)+yshift == int(x.exp))
return true
}
const e7 = 10000000
// Mul returns the product of two Dnum's
func Mul(x, y Dnum) Dnum {
sign := x.sign * y.sign
switch {
case sign == signZero:
return Zero
case x.IsInf() || y.IsInf():
return Inf(sign)
}
e := int(x.exp) + int(y.exp)
// split unevenly to use full 64 bit range to get more precision
// and avoid needing xlo * ylo
xhi := x.coef / e7 // 9 digits
xlo := x.coef % e7 // 7 digits
yhi := y.coef / e7 // 9 digits
ylo := y.coef % e7 // 7 digits
c := xhi * yhi
if xlo != 0 || ylo != 0 {
c += (xlo*yhi + ylo*xhi) / e7
}
return New(sign, c, e-2)
}
// Div returns the quotient of two Dnum's
func Div(x, y Dnum) Dnum {
sign := x.sign * y.sign
switch {
case x.sign == signZero:
return x
case y.sign == signZero:
return Inf(x.sign)
case x.IsInf():
if y.IsInf() {
if sign < 0 {
return NegOne
}
return One
}
return Inf(sign)
case y.IsInf():
return Zero
}
coef := div128(x.coef, y.coef)
return New(sign, coef, int(x.exp)-int(y.exp))
}
// Hash returns a hash value for a Dnum
func (dn Dnum) Hash() uint32 {
return uint32(dn.coef>>32) ^ uint32(dn.coef) ^
uint32(dn.sign)<<16 ^ uint32(dn.exp)<<8
}
// Format converts a number to a string with a specified format
func (dn Dnum) Format(mask string) string {
if dn.IsInf() {
return "#"
}
n := dn
before := 0
after := 0
intpart := true
for _, mc := range mask {
switch mc {
case '.':
intpart = false
case '#':
if intpart {
before++
} else {
after++
}
}
}
if before+after == 0 || n.Exp() > before {
return "#" // too big to fit in mask
}
n = n.Round(after, HalfUp)
e := n.Exp()
var digits []byte
if n.IsZero() && after == 0 {
digits = []byte("0")
e = 1
} else {
digits = strconv.AppendUint(make([]byte, 0, digitsMax), n.Coef(), 10)
digits = bytes.TrimRight(digits, "0")
}
nd := len(digits)
di := e - before
check(di <= 0)
var buf strings.Builder
sign := n.Sign()
signok := (sign >= 0)
frac := false
for _, mc := range []byte(mask) {
switch mc {
case '#':
if 0 <= di && di < nd {
buf.WriteByte(digits[di])
} else if frac || di >= 0 {
buf.WriteByte('0')
}
di++
case ',':
if di > 0 {
buf.WriteByte(',')
}
case '-', '(':
signok = true
if sign < 0 {
buf.WriteByte(mc)
}
case ')':
if sign < 0 {
buf.WriteByte(mc)
} else {
buf.WriteByte(' ')
}
case '.':
frac = true
fallthrough
default:
buf.WriteByte(mc)
}
}
if !signok {
return "-" // negative not handled by mask
}
return buf.String()
}