refactoring div test. benchmarks for old Div vs. New Div

This commit is contained in:
Roland Averkamp 2015-09-03 20:11:07 +02:00
parent 696232de64
commit 5c1d2008f5

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"encoding/xml" "encoding/xml"
"math" "math"
"math/big"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
@ -742,24 +743,47 @@ func TestDecimal_QuoRem(t *testing.T) {
} }
} }
func TestDecimal_QuoRem2(t *testing.T) { type DivTestCase struct {
d Decimal
d2 Decimal
prec int32
}
func createDivTestCases() []DivTestCase {
res := make([]DivTestCase, 0)
var n int32 = 5 var n int32 = 5
a := []int{1, 2, 3, 6, 7, 10, 100, 14, 5, 400, 0} a := []int{1, 2, 3, 6, 7, 10, 100, 14, 5, 400, 0, 1000000, 1000000 + 1, 1000000 - 1}
for s := -1; s < 2; s = s + 2 { for s := -1; s < 2; s = s + 2 { // 2
for s2 := -1; s2 < 2; s2 = s2 + 2 { for s2 := -1; s2 < 2; s2 = s2 + 2 { // 2
for e1 := -n; e1 < n; e1++ { for e1 := -n; e1 <= n; e1++ { // 2n+1
for e2 := -n; e2 < n; e2++ { for e2 := -n; e2 <= n; e2++ { // 2n+1
var prec int32 var prec int32
for prec = -n; prec < n; prec++ { for prec = -n; prec <= n; prec++ { // 2n+1
for _, v1 := range a { for _, v1 := range a { // 11
for _, v2 := range a { for _, v2 := range a { // 11, even if 0 is skipped
if v2 == 0 {
continue
}
sign1 := New(int64(s), 0) sign1 := New(int64(s), 0)
sign2 := New(int64(s2), 0) sign2 := New(int64(s2), 0)
d := sign1.Mul(New(int64(v1), int32(e1))) d := sign1.Mul(New(int64(v1), int32(e1)))
d2 := sign2.Mul(New(int64(v2), int32(e2))) d2 := sign2.Mul(New(int64(v2), int32(e2)))
res = append(res, DivTestCase{d, d2, prec})
}
}
}
}
}
}
}
return res
}
func TestDecimal_QuoRem2(t *testing.T) {
for _, tc := range createDivTestCases() {
d := tc.d
if sign(tc.d2) == 0 {
continue
}
d2 := tc.d2
prec := tc.prec
q, r := d.QuoRem(d2, prec) q, r := d.QuoRem(d2, prec)
// rule 1: d = d2*q +r // rule 1: d = d2*q +r
if !d.Equals(d2.Mul(q).Add(r)) { if !d.Equals(d2.Mul(q).Add(r)) {
@ -783,9 +807,59 @@ func TestDecimal_QuoRem2(t *testing.T) {
d, d2, prec, q, r) d, d2, prec, q, r)
} }
} }
}
// this is the old Div method from decimal
// Div returns d / d2. If it doesn't divide exactly, the result will have
// DivisionPrecision digits after the decimal point.
func (d Decimal) DivOld(d2 Decimal, prec int) Decimal {
// NOTE(vadim): division is hard, use Rat to do it
ratNum := d.Rat()
ratDenom := d2.Rat()
quoRat := big.NewRat(0, 1).Quo(ratNum, ratDenom)
// HACK(vadim): converting from Rat to Decimal inefficiently for now
ret, err := NewFromString(quoRat.FloatString(prec))
if err != nil {
panic(err) // this should never happen
}
return ret
}
func Benchmark_DivideOriginal(b *testing.B) {
tcs := createDivTestCases()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, tc := range tcs {
d := tc.d
if sign(tc.d2) == 0 {
continue
}
d2 := tc.d2
prec := tc.prec
a := d.DivOld(d2, int(prec))
if sign(a) > 2 {
panic("dummy panic")
} }
} }
} }
}
func Benchmark_DivideNew(b *testing.B) {
tcs := createDivTestCases()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, tc := range tcs {
d := tc.d
if sign(tc.d2) == 0 {
continue
}
d2 := tc.d2
prec := tc.prec
a := d.DivRound(d2, prec)
if sign(a) > 2 {
panic("dummy panic")
} }
} }
} }
@ -845,6 +919,28 @@ func TestDecimal_DivRound(t *testing.T) {
} }
} }
func TestDecimal_DivRound2(t *testing.T) {
for _, tc := range createDivTestCases() {
d := tc.d
if sign(tc.d2) == 0 {
continue
}
d2 := tc.d2
prec := tc.prec
q := d.DivRound(d2, prec)
if sign(q)*sign(d)*sign(d2) < 0 {
t.Errorf("sign of quotient wrong, got: %v/%v is about %v", d, d2, q)
}
x := q.Mul(d2).Abs().Sub(d.Abs()).Mul(New(2, 0))
if x.Cmp(d2.Abs().Mul(New(1, -prec))) > 0 {
t.Errorf("wrong rounding, got: %v/%v prec=%d is about %v", d, d2, prec, q)
}
if x.Cmp(d2.Abs().Mul(New(-1, -prec))) <= 0 {
t.Errorf("wrong rounding, got: %v/%v prec=%d is about %v", d, d2, prec, q)
}
}
}
func TestDecimal_Overflow(t *testing.T) { func TestDecimal_Overflow(t *testing.T) {
if !didPanic(func() { New(1, math.MinInt32).Mul(New(1, math.MinInt32)) }) { if !didPanic(func() { New(1, math.MinInt32).Mul(New(1, math.MinInt32)) }) {
t.Fatalf("should have gotten an overflow panic") t.Fatalf("should have gotten an overflow panic")