Skip to content

Commit 6ceca07

Browse files
authored
cleanup + fix decimal_debug bugs (#83)
- Remove testify. - Fix `go test -tags=decimal-debug` failures. - Make all test cases parallel.
1 parent 778691a commit 6ceca07

21 files changed

+625
-541
lines changed

decimal64.go

+35-54
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"math"
66
"math/bits"
7+
"strconv"
78
)
89

910
type discardedDigit int
@@ -108,58 +109,42 @@ func (ctx Rounding) round(significand uint64, rndStatus discardedDigit) uint64 {
108109
var ErrNaN64 error = Error("sNaN64")
109110

110111
var small64s = []Decimal64{
111-
newFromPartsRaw(1, -14, 1*decimal64Base),
112-
113-
newFromPartsRaw(1, -15, 9*decimal64Base),
114-
newFromPartsRaw(1, -15, 8*decimal64Base),
115-
newFromPartsRaw(1, -15, 7*decimal64Base),
116-
newFromPartsRaw(1, -15, 6*decimal64Base),
117-
newFromPartsRaw(1, -15, 5*decimal64Base),
118-
newFromPartsRaw(1, -15, 4*decimal64Base),
119-
newFromPartsRaw(1, -15, 3*decimal64Base),
120-
newFromPartsRaw(1, -15, 2*decimal64Base),
121-
newFromPartsRaw(1, -15, 1*decimal64Base),
112+
new64str(newFromPartsRaw(1, -14, 1*decimal64Base).bits, "-10"),
113+
114+
new64str(newFromPartsRaw(1, -15, 9*decimal64Base).bits, "-9"),
115+
new64str(newFromPartsRaw(1, -15, 8*decimal64Base).bits, "-8"),
116+
new64str(newFromPartsRaw(1, -15, 7*decimal64Base).bits, "-7"),
117+
new64str(newFromPartsRaw(1, -15, 6*decimal64Base).bits, "-6"),
118+
new64str(newFromPartsRaw(1, -15, 5*decimal64Base).bits, "-5"),
119+
new64str(newFromPartsRaw(1, -15, 4*decimal64Base).bits, "-4"),
120+
new64str(newFromPartsRaw(1, -15, 3*decimal64Base).bits, "-3"),
121+
new64str(newFromPartsRaw(1, -15, 2*decimal64Base).bits, "-2"),
122+
new64str(newFromPartsRaw(1, -15, 1*decimal64Base).bits, "-1"),
122123

123124
// TODO: Decimal64{}?
124-
newFromPartsRaw(0, 0, 0),
125-
126-
newFromPartsRaw(0, -15, 1*decimal64Base),
127-
newFromPartsRaw(0, -15, 2*decimal64Base),
128-
newFromPartsRaw(0, -15, 3*decimal64Base),
129-
newFromPartsRaw(0, -15, 4*decimal64Base),
130-
newFromPartsRaw(0, -15, 5*decimal64Base),
131-
newFromPartsRaw(0, -15, 6*decimal64Base),
132-
newFromPartsRaw(0, -15, 7*decimal64Base),
133-
newFromPartsRaw(0, -15, 8*decimal64Base),
134-
newFromPartsRaw(0, -15, 9*decimal64Base),
135-
136-
newFromPartsRaw(0, -14, 1*decimal64Base),
137-
}
138-
139-
var small64Strings = map[Decimal64]string{
140-
small64s[0]: "-10",
141-
small64s[1]: "-9",
142-
small64s[2]: "-8",
143-
small64s[3]: "-7",
144-
small64s[4]: "-6",
145-
small64s[5]: "-5",
146-
small64s[6]: "-4",
147-
small64s[7]: "-3",
148-
small64s[8]: "-2",
149-
small64s[9]: "-1",
150-
small64s[10]: "0",
151-
small64s[11]: "1",
152-
small64s[12]: "2",
153-
small64s[13]: "3",
154-
small64s[14]: "4",
155-
small64s[15]: "5",
156-
small64s[16]: "6",
157-
small64s[17]: "7",
158-
small64s[18]: "8",
159-
small64s[19]: "9",
160-
small64s[20]: "10",
125+
new64str(newFromPartsRaw(0, 0, 0).bits, "0"),
126+
127+
new64str(newFromPartsRaw(0, -15, 1*decimal64Base).bits, "1"),
128+
new64str(newFromPartsRaw(0, -15, 2*decimal64Base).bits, "2"),
129+
new64str(newFromPartsRaw(0, -15, 3*decimal64Base).bits, "3"),
130+
new64str(newFromPartsRaw(0, -15, 4*decimal64Base).bits, "4"),
131+
new64str(newFromPartsRaw(0, -15, 5*decimal64Base).bits, "5"),
132+
new64str(newFromPartsRaw(0, -15, 6*decimal64Base).bits, "6"),
133+
new64str(newFromPartsRaw(0, -15, 7*decimal64Base).bits, "7"),
134+
new64str(newFromPartsRaw(0, -15, 8*decimal64Base).bits, "8"),
135+
new64str(newFromPartsRaw(0, -15, 9*decimal64Base).bits, "9"),
136+
137+
new64str(newFromPartsRaw(0, -14, 1*decimal64Base).bits, "10"),
161138
}
162139

140+
var small64Strings = func() map[uint64]string {
141+
m := make(map[uint64]string, len(small64s))
142+
for i := -10; i <= 10; i++ {
143+
m[small64s[10+i].bits] = strconv.Itoa(i)
144+
}
145+
return m
146+
}()
147+
163148
// New64FromInt64 returns a new Decimal64 with the given value.
164149
func New64FromInt64(i int64) Decimal64 {
165150
if i >= -10 && i <= 10 {
@@ -258,10 +243,6 @@ func countTrailingZeros(n uint64) int {
258243
return zeros
259244
}
260245

261-
func new64Raw(bits uint64) Decimal64 {
262-
return Decimal64{bits: bits}
263-
}
264-
265246
func newFromParts(sign int, exp int, significand uint64) Decimal64 {
266247
return new64(newFromPartsRaw(sign, exp, significand).bits)
267248
}
@@ -272,12 +253,12 @@ func newFromPartsRaw(sign int, exp int, significand uint64) Decimal64 {
272253
if significand < 0x8<<50 {
273254
// s EEeeeeeeee (0)ttt tttttttttt tttttttttt tttttttttt tttttttttt tttttttttt
274255
// EE ∈ {00, 01, 10}
275-
return new64Raw(s | uint64(exp+expOffset)<<(63-10) | significand)
256+
return Decimal64{bits: s | uint64(exp+expOffset)<<(63-10) | significand}
276257
}
277258
// s 11EEeeeeeeee (100)t tttttttttt tttttttttt tttttttttt tttttttttt tttttttttt
278259
// EE ∈ {00, 01, 10}
279260
significand &= 0x8<<50 - 1
280-
return new64Raw(s | uint64(0xc00|(exp+expOffset))<<(63-12) | significand)
261+
return Decimal64{bits: s | uint64(0xc00|(exp+expOffset))<<(63-12) | significand}
281262
}
282263

283264
func (d Decimal64) parts() (fl flavor, sign int, exp int, significand uint64) {

decimal64_debug.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,23 @@ type Decimal64 struct {
1717
bits uint64
1818
}
1919

20-
// This should be the only point at which Decimal64 instances are constructed raw.
21-
// The verbose construction below makes it easy to audit accidental raw cosntruction.
22-
// A search for (?<!\[\])Decimal64\{ must come up empty.
2320
func new64(bits uint64) Decimal64 {
24-
d := new64Raw(bits)
21+
d := new64nostr(bits)
22+
d.s = d.String()
23+
return d
24+
}
25+
26+
func new64str(bits uint64, s string) Decimal64 {
27+
d := new64nostr(bits)
28+
d.s = s
29+
return d
30+
}
31+
32+
func new64nostr(bits uint64) Decimal64 {
33+
d := Decimal64{bits: bits}
2534

2635
fl, sign, exp, significand := d.parts()
2736

28-
d.s = d.String()
2937
d.fl = fl
3038
d.sign = sign
3139
d.exp = exp

decimal64_ndebug.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ type Decimal64 struct {
1010
bits uint64
1111
}
1212

13-
// This should be the only point at which Decimal64 instances are constructed raw.
14-
// The verbose construction below makes it easy to audit accidental raw cosntruction.
15-
// A search for (?<!\[\])Decimal64\{ must come up empty.
1613
func new64(bits uint64) Decimal64 {
17-
return new64Raw(bits)
14+
return Decimal64{bits: bits}
15+
}
16+
17+
func new64str(bits uint64, _ string) Decimal64 {
18+
return Decimal64{bits: bits}
19+
}
20+
21+
func new64nostr(bits uint64) Decimal64 {
22+
return Decimal64{bits: bits}
1823
}
1924

2025
func checkSignificandIsNormal(significand uint64) {}

0 commit comments

Comments
 (0)