diff --git a/big_ctx.go b/big_ctx.go index 3535589..2e4d353 100644 --- a/big_ctx.go +++ b/big_ctx.go @@ -2496,6 +2496,7 @@ func (c Context) QuoRem(z, x, y, r *Big) (*Big, *Big) { sign := (x.form & signbit) ^ (y.form & signbit) if x.IsFinite() && y.IsFinite() { + exp := min(x.exp, y.exp) if y.isZero() { if x.isZero() { // 0 / 0 @@ -2510,10 +2511,13 @@ func (c Context) QuoRem(z, x, y, r *Big) (*Big, *Big) { if x.isZero() { // 0 / y z.setZero((x.form^y.form)&signbit, 0) - r.setZero(x.form, y.exp-x.exp) + r.setZero(x.form, exp) return c.fix(z), c.fix(r) } - return c.quorem(z, r, x, y) + z, r = c.quorem(z, r, x, y) + z.exp = 0 + r.exp = exp + return z, r } // NaN / NaN diff --git a/quorem_scale_test.go b/quorem_scale_test.go new file mode 100644 index 0000000..847009e --- /dev/null +++ b/quorem_scale_test.go @@ -0,0 +1,61 @@ +package decimal_test + +import ( + "math/big" + "testing" + + "github.com/ericlagergren/decimal" +) + +func TestQuoRemScale(t *testing.T) { + for _, tc := range []struct{ x, y string }{ + {"1.2", "1"}, {"12", "1.1"}, {"-1.2", "1"}, {"1.2", "-1"}, + {"123456789012345678901.2", "10"}, {"12", "100000000000000000000.1"}, + {"1E+30", "3"}, {"1", "3E+30"}, {"1.20", "0.10"}, {"0.00", "1.0"}, + } { + t.Run(tc.x+"/"+tc.y, func(t *testing.T) { + for _, alias := range []string{"fresh", "big-method", "reused", "quotient-x", "remainder-x", "quotient-y", "remainder-y"} { + t.Run(alias, func(t *testing.T) { + x, _ := new(decimal.Big).SetString(tc.x) + y, _ := new(decimal.Big).SetString(tc.y) + xr, yr := x.Rat(nil), y.Rat(nil) + wantScale := x.Scale() + if y.Scale() > wantScale { + wantScale = y.Scale() + } + ratio := new(big.Rat).Quo(xr, yr) + qi := new(big.Int).Quo(ratio.Num(), ratio.Denom()) + wantQ := new(big.Rat).SetInt(qi) + wantR := new(big.Rat).Sub(xr, new(big.Rat).Mul(wantQ, yr)) + q, r := new(decimal.Big), new(decimal.Big) + switch alias { + case "reused": + q.SetString("0.001") + r.SetString("0.00001") + case "quotient-x": + q = x + case "remainder-x": + r = x + case "quotient-y": + q = y + case "remainder-y": + r = y + } + ctx := decimal.Context{Precision: 100} + if alias == "big-method" { + q.Context = ctx + q.QuoRem(x, y, r) + } else { + ctx.QuoRem(q, x, y, r) + } + if q.Scale() != 0 || r.Scale() != wantScale { + t.Errorf("scales = (%d, %d), want (0, %d)", q.Scale(), r.Scale(), wantScale) + } + if q.Rat(nil).Cmp(wantQ) != 0 || r.Rat(nil).Cmp(wantR) != 0 { + t.Fatalf("QuoRem = (%s, %s), want (%s, %s)", q, r, wantQ, wantR) + } + }) + } + }) + } +}