aboutsummaryrefslogtreecommitdiff
path: root/src/math/big/float_test.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2015-03-05 17:32:57 -0800
committerRobert Griesemer <gri@golang.org>2015-03-17 16:09:34 +0000
commit00c73f5c6e4b80a24eb19218c006c8a3f08e1ed8 (patch)
treeddd5b8097f043e85da7bc63cfcad6af61fa069a1 /src/math/big/float_test.go
parent2e7f0a00c337c0a536fafc1d5cb831cb4c76efad (diff)
downloadgo-00c73f5c6e4b80a24eb19218c006c8a3f08e1ed8.tar.xz
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/7001 Reviewed-by: Alan Donovan <adonovan@google.com>
Diffstat (limited to 'src/math/big/float_test.go')
-rw-r--r--src/math/big/float_test.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/math/big/float_test.go b/src/math/big/float_test.go
index 683809bf56..379352c886 100644
--- a/src/math/big/float_test.go
+++ b/src/math/big/float_test.go
@@ -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