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
2 changes: 1 addition & 1 deletion decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func (d *Decimal) setExponent(c *Context, nd int64, res Condition, xs ...int64)
frac.Abs(&frac)
if !frac.IsZero() {
res |= Inexact
if c.Rounding.ShouldAddOne(&integ.Coeff, integ.Negative, frac.Cmp(decimalHalf)) {
if c.Rounding.ShouldAddOne(&integ.Coeff, d.Negative, frac.Cmp(decimalHalf)) {
integ.Coeff.Add(&integ.Coeff, bigOne)
}
}
Expand Down
30 changes: 30 additions & 0 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,36 @@ func TestQuantizeRounding(t *testing.T) {
}
}

func TestSubnormalDirectedRounding(t *testing.T) {
tests := []struct {
s string
rnd Rounder
expect string
}{
{s: "-3.06E-8", rnd: RoundCeiling, expect: "-0.000000"},
{s: "-3.06E-8", rnd: RoundFloor, expect: "-0.000001"},
{s: "3.06E-8", rnd: RoundCeiling, expect: "0.000001"},
{s: "3.06E-8", rnd: RoundFloor, expect: "0.000000"},
}
one := New(1, 0)
for _, tc := range tests {
t.Run(fmt.Sprintf("%s %s", tc.s, tc.rnd), func(t *testing.T) {
c := Context{Precision: 2, MaxExponent: 999, MinExponent: -5, Rounding: tc.rnd, Traps: DefaultTraps &^ Subnormal &^ Underflow}
x, _, err := NewFromString(tc.s)
if err != nil {
t.Fatal(err)
}
var got Decimal
if _, err := c.Mul(&got, x, one); err != nil {
t.Fatal(err)
}
if s := got.String(); s != tc.expect {
t.Fatalf("expected: %s, got: %s", tc.expect, s)
}
})
}
}

func TestRoundToIntegralRounding(t *testing.T) {
tests := []struct {
s string
Expand Down