-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecimal_test.go
148 lines (138 loc) · 4 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2020 Denis Bernard <[email protected]>. 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
import (
"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 BenchmarkDecimal_Float(b *testing.B) {
d := new(Decimal).SetPrec(100).SetUint64(2)
d.Sqrt(d)
f := d.Float(nil)
for i := 0; i < b.N; i++ {
d.Float(f)
}
}
func TestDecimal_FMA(t *testing.T) {
x := NewDecimal(123, -2).SetPrec(3)
y := NewDecimal(227, -2).SetPrec(3)
u := NewDecimal(3, -3).SetPrec(3)
z := new(Decimal).SetPrec(3).Mul(x, y) // == 2.7921
z.Add(z, u)
if s := z.String(); s != "2.79" {
t.Fatalf("Precision 3, 2.79 + 0.003 = %s, want 2.79", s)
}
z = z.FMA(x, y, u)
if s := z.String(); s != "2.8" {
t.Fatalf("Precision 3, %v * %v + 0.003 = %s, want 2.8", x, y, s)
}
// test aliasing z and u
u.FMA(x, y, u)
if s := z.String(); s != "2.8" {
t.Fatalf("Aliasing z & u, %v * %v + 0.003 = %s, want 2.8", x, y, s)
}
}