-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecimal_test.go
133 lines (125 loc) · 3.63 KB
/
decimal_test.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
package decimal
import (
"math"
"math/big"
"math/rand"
"reflect"
"strconv"
"testing"
)
var benchU uint
var intData = []struct {
s string
b int
p uint
d dec
pr uint
e int32
}{
{"00000000000000000001232", 10, 0, dec{1232000000000000000}, DefaultDecimalPrec, 4},
{"1234567890123456789_0123456789012345678_9012345678901234567_8901234567890123456_78901234567890", 0, 90,
dec{7890123456789000000, 8901234567890123456, 9012345678901234567, 123456789012345678, 1234567890123456789},
90, 90},
{"1235", 0, 0, dec{1235000000000000000}, DefaultDecimalPrec, 4},
{"1235", 0, 3, dec{1240000000000000000}, 3, 4},
{"1245", 0, 3, dec{1240000000000000000}, 3, 4},
{"12451", 0, 3, dec{1250000000000000000}, 3, 5},
{"0", 0, 0, nil, DefaultDecimalPrec, 0},
}
func TestDecimal_dnorm(t *testing.T) {
for i := 0; i < 10000; i++ {
again:
w := uint(rand.Uint64()) % _DB
e := uint(rand.Intn(_DW + 1))
h, l := mulWW(Word(w), pow10(e))
// convert h, l from base _B (2**64) to base _BD (10**19) or 2**32 -> 10**9
h, l = div10W(h, l)
d := dec{Word(l), Word(h)}.norm()
if len(d) == 0 {
if w == 0 {
goto again
}
t.Fatalf("dec{%v, %v}).norm() returned dec{} for word %d", l, h, w)
}
dd := dec(nil).set(d)
s := dnorm(dd)
// d should now have a single element with e shifted left
ew := w * uint(pow10(_DW-decDigits(w)))
es := int64(uint(len(d)*_DW) - (decDigits(w) + e))
if dd[len(dd)-1] != Word(ew) || s != es {
t.Fatalf("%ve%v => dnorm(%v) = %v, %v --- Expected %d, %d",
w, e, d, dd, s, w, es)
}
}
}
func TestDecimal_SetInt(t *testing.T) {
for i, td := range intData {
t.Run(strconv.Itoa(i), func(t *testing.T) {
b, _ := new(big.Int).SetString(td.s, td.b)
d := new(Decimal).SetMode(ToNearestEven).SetPrec(td.p).SetInt(b)
ep := td.pr
if td.p == 0 && ep < DefaultDecimalPrec {
ep = DefaultDecimalPrec
}
if !reflect.DeepEqual(td.d, d.mant) {
t.Fatalf("\nexpected mantissa %v\n got %v", td.d, d.mant)
}
if ep != d.Prec() {
t.Fatalf("\nexpected precision %v\n got %v", ep, d.Prec())
}
if td.e != d.exp {
t.Fatalf("\nexpected exponent %v\n got %v", td.p, d.Prec())
}
})
}
}
func TestDecimal_SetString(t *testing.T) {
for i, td := range intData {
t.Run(strconv.Itoa(i), func(t *testing.T) {
d, _ := new(Decimal).SetMode(ToNearestEven).SetPrec(td.p).SetString(td.s)
if !reflect.DeepEqual(td.d, d.mant) {
t.Fatalf("\nexpected mantissa %v\n got %v", td.d, d.mant)
}
if td.pr != d.Prec() {
t.Fatalf("\nexpected precision %v\n got %v", td.pr, d.Prec())
}
if td.e != d.exp {
t.Fatalf("\nexpected exponent %v\n got %v", td.p, d.Prec())
}
})
}
}
func BenchmarkDecimal_dnorm(b *testing.B) {
d := dec(nil).make(1000)
for i := range d {
d[i] = Word(rand.Uint64()) % _DB
}
for i := 0; i < b.N; i++ {
d[0] = Word(rand.Uint64()) % _DB
d[len(d)-1] = Word(rand.Uint64()) % _DB
benchU = uint(dnorm(d))
}
}
func BenchmarkDecimal_Sqrt(b *testing.B) {
x := new(Decimal).SetUint64(2)
z := new(Decimal).SetPrec(34)
for i := 0; i < b.N; i++ {
z.Sqrt(x)
}
}
func TestDecimal_String(t *testing.T) {
z := big.NewFloat(1.1)
t.Logf("z: %g!", z)
// x, _ := new(Decimal).SetPrec(34).SetString("9223372036854775808")
x := new(Decimal).SetPrec(17).SetFloat64(math.MaxFloat64)
// t.Log(x.Text('p', 0))
t.Logf("x: %g!", x)
x.SetPrec(17).SetFloat(z)
t.Logf("z = %g -> x (%d) = %g", z, x.Prec(), x)
x.Float(z)
t.Logf("x = %g -> z = %g", x, z)
x.Sqrt(x.SetPrec(33))
t.Logf("x: %g!", x)
t.Logf("x: %.*f!", x.Prec()-1, x)
t.Log(" 1.41421356237309504880168872420969807856967187537694807317667974")
}