Commit 00c73f5c authored by Robert Griesemer's avatar Robert Griesemer

math/big: cleaner handling of exponent under/overflow

Fixed several corner-case bugs and added corresponding tests.

Change-Id: I23096b9caeeff0956f65ab59fa91e168d0e47bb8
Reviewed-on: https://go-review.googlesource.com/7001Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
parent 2e7f0a00
......@@ -187,7 +187,12 @@ func (bits Bits) Float() *Float {
// create corresponding float
z := new(Float).SetInt(x) // normalized
z.setExp(int64(z.exp) + int64(min))
if e := int64(z.exp) + int64(min); MinExp <= e && e <= MaxExp {
z.exp = int32(e)
} else {
// this should never happen for our test cases
panic("exponent out of range")
}
return z
}
......
This diff is collapsed.
......@@ -1389,6 +1389,68 @@ func TestFloatArithmeticSpecialValues(t *testing.T) {
}
}
func TestFloatArithmeticOverflow(t *testing.T) {
for _, test := range []struct {
prec uint
mode RoundingMode
op byte
x, y, want string
acc Accuracy
}{
{4, ToNearestEven, '+', "0", "0", "0", Exact}, // smoke test
{4, ToNearestEven, '+', "0x.8p0", "0x.8p0", "0x.8p1", Exact}, // smoke test
{4, ToNearestEven, '+', "0", "0x.8p2147483647", "0x.8p2147483647", Exact},
{4, ToNearestEven, '+', "0x.8p2147483500", "0x.8p2147483647", "0x.8p2147483647", Below}, // rounded to zero
{4, ToNearestEven, '+', "0x.8p2147483647", "0x.8p2147483647", "+Inf", Above}, // exponent overflow in +
{4, ToNearestEven, '+', "-0x.8p2147483647", "-0x.8p2147483647", "-Inf", Below}, // exponent overflow in +
{4, ToNearestEven, '-', "-0x.8p2147483647", "0x.8p2147483647", "-Inf", Below}, // exponent overflow in -
{4, ToZero, '+', "0x.fp2147483647", "0x.8p2147483643", "0x.fp2147483647", Below}, // rounded to zero
{4, ToNearestEven, '+', "0x.fp2147483647", "0x.8p2147483643", "+Inf", Above}, // exponent overflow in rounding
{4, AwayFromZero, '+', "0x.fp2147483647", "0x.8p2147483643", "+Inf", Above}, // exponent overflow in rounding
{4, AwayFromZero, '-', "-0x.fp2147483647", "0x.8p2147483644", "-Inf", Below}, // exponent overflow in rounding
{4, ToNearestEven, '-', "-0x.fp2147483647", "0x.8p2147483643", "-Inf", Below}, // exponent overflow in rounding
{4, ToZero, '-', "-0x.fp2147483647", "0x.8p2147483643", "-0x.fp2147483647", Above}, // rounded to zero
{4, ToNearestEven, '+', "0", "0x.8p-2147483648", "0x.8p-2147483648", Exact},
{4, ToNearestEven, '+', "0x.8p-2147483648", "0x.8p-2147483648", "0x.8p-2147483647", Exact},
{4, ToNearestEven, '*', "1", "0x.8p2147483647", "0x.8p2147483647", Exact},
{4, ToNearestEven, '*', "2", "0x.8p2147483647", "+Inf", Above}, // exponent overflow in *
{4, ToNearestEven, '*', "-2", "0x.8p2147483647", "-Inf", Below}, // exponent overflow in *
{4, ToNearestEven, '/', "0.5", "0x.8p2147483647", "0x.8p-2147483646", Exact},
{4, ToNearestEven, '/', "0x.8p0", "0x.8p2147483647", "0x.8p-2147483646", Exact},
{4, ToNearestEven, '/', "0x.8p-1", "0x.8p2147483647", "0x.8p-2147483647", Exact},
{4, ToNearestEven, '/', "0x.8p-2", "0x.8p2147483647", "0x.8p-2147483648", Exact},
{4, ToNearestEven, '/', "0x.8p-3", "0x.8p2147483647", "0", Below}, // exponent underflow in /
} {
x := makeFloat(test.x)
y := makeFloat(test.y)
z := new(Float).SetPrec(test.prec).SetMode(test.mode)
switch test.op {
case '+':
z.Add(x, y)
case '-':
z.Sub(x, y)
case '*':
z.Mul(x, y)
case '/':
z.Quo(x, y)
default:
panic("unreachable")
}
if got := z.Format('p', 0); got != test.want || z.Acc() != test.acc {
t.Errorf(
"prec = %d (%s): %s %c %s = %s (%s); want %s (%s)",
test.prec, test.mode, x.Format('p', 0), test.op, y.Format('p', 0), got, z.Acc(), test.want, test.acc,
)
}
}
}
// TODO(gri) Add tests that check correctness in the presence of aliasing.
// For rounding modes ToNegativeInf and ToPositiveInf, rounding is affected
......
......@@ -101,8 +101,6 @@ func (z *Float) Scan(r io.ByteScanner, base int) (f *Float, b int, err error) {
}
// len(z.mant) > 0
z.form = finite
// The mantissa may have a decimal point (fcount <= 0) and there
// may be a nonzero exponent exp. The decimal point amounts to a
// division by b**(-fcount). An exponent means multiplication by
......@@ -142,7 +140,14 @@ func (z *Float) Scan(r io.ByteScanner, base int) (f *Float, b int, err error) {
// we don't need exp anymore
// apply 2**exp2
z.setExp(exp2)
if MinExp <= exp2 && exp2 <= MaxExp {
z.form = finite
z.exp = int32(exp2)
} else {
f = nil
err = fmt.Errorf("exponent overflow")
return
}
if exp10 == 0 {
// no decimal exponent to consider
......@@ -160,7 +165,6 @@ func (z *Float) Scan(r io.ByteScanner, base int) (f *Float, b int, err error) {
fpowTen := new(Float).SetInt(new(Int).SetBits(powTen))
// apply 10**exp10
// (uquo and umul do the rounding)
if exp10 < 0 {
z.uquo(z, fpowTen)
} else {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment