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
5 changes: 4 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -1308,8 +1308,11 @@ func (c *Context) Reduce(d, x *Decimal) (int, Condition, error) {
_, n := d.Reduce(x)
d.Negative = neg
res := c.round(d, d)
// Rounding up can carry into new trailing zeros, as in 9999 at precision 3.
_, m := d.Reduce(d)
d.Negative = neg
res, err := c.goError(res)
return n, res, err
return n + m, res, err
}

// exp10 returns x, 10^x. An error is returned if x is too large.
Expand Down
33 changes: 33 additions & 0 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,39 @@ func TestReduce(t *testing.T) {
}
}

// TestContextReduceRounding checks that Context.Reduce removes the trailing
// zeros that rounding to the precision can produce.
func TestContextReduceRounding(t *testing.T) {
c := &Context{Precision: 3, MaxExponent: 999, MinExponent: -999, Rounding: RoundHalfUp}
tests := []struct {
x, want string
n int
}{
{x: "9999", want: "1E+4", n: 2},
{x: "-99.99", want: "-1E+2", n: 2},
{x: "1.2300", want: "1.23", n: 2},
{x: "-0.0001E-999", want: "-0", n: 0},
{x: "0.999E-999", want: "1E-999", n: 2},
{x: "0.099E-999", want: "1E-1000", n: 1},
}
for _, tc := range tests {
t.Run(tc.x, func(t *testing.T) {
x, _, err := NewFromString(tc.x)
if err != nil {
t.Fatal(err)
}
var d Decimal
n, _, err := c.Reduce(&d, x)
if err != nil {
t.Fatal(err)
}
if got := d.String(); got != tc.want || n != tc.n {
t.Fatalf("got %s (n=%d), expected %s (n=%d)", got, n, tc.want, tc.n)
}
})
}
}

// TestRootZeroTraps checks that a root of zero whose exponent is clamped
// into the context's range returns an error when Clamped is trapped. The
// GDA vectors cover the exponent and condition but cannot set Traps.
Expand Down