diff --git a/context.go b/context.go index 260e092..0d3125d 100644 --- a/context.go +++ b/context.go @@ -1220,8 +1220,8 @@ func (c *Context) quantize(d, v *Decimal, exp int32) Condition { // target eliminates this problem. d.Exponent = -diff - // Avoid the c.Precision == 0 check. - res = nc.Rounding.Round(nc, d, d) + // Round even if nc.Precision == 0. + res = nc.Rounding.Round(nc, d, d, false /* disableIfPrecisionZero */) // Adjust for 0.9 -> 1.0 rollover. if d.Exponent > 0 { d.Coeff.Mul(&d.Coeff, bigTen) diff --git a/round.go b/round.go index 92998c5..c9741a6 100644 --- a/round.go +++ b/round.go @@ -21,13 +21,9 @@ func (c *Context) Round(d, x *Decimal) (Condition, error) { return c.goError(c.round(d, x)) } +//gcassert:inline func (c *Context) round(d, x *Decimal) Condition { - if c.Precision == 0 { - d.Set(x) - return d.setExponent(c, unknownNumDigits, 0, int64(d.Exponent)) - } - res := c.Rounding.Round(c, d, x) - return res + return c.Rounding.Round(c, d, x, true /* disableIfPrecisionZero */) } // Rounder specifies the behavior of rounding. @@ -63,12 +59,17 @@ func (r Rounder) ShouldAddOne(result *BigInt, neg bool, half int) bool { } // Round sets d to rounded x. -func (r Rounder) Round(c *Context, d, x *Decimal) Condition { +func (r Rounder) Round(c *Context, d, x *Decimal, disableIfPrecisionZero bool) Condition { d.Set(x) nd := x.NumDigits() xs := x.Sign() var res Condition + if disableIfPrecisionZero && c.Precision == 0 { + // Rounding has been disabled. + return d.setExponent(c, nd, res, int64(d.Exponent)) + } + // adj is the adjusted exponent: exponent + clength - 1 if adj := int64(x.Exponent) + nd - 1; xs != 0 && adj < int64(c.MinExponent) { // Subnormal is defined before rounding.