Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions big_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
61 changes: 61 additions & 0 deletions quorem_scale_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
})
}
}