1
0
mirror of https://github.com/golang/go synced 2024-11-19 02:04:42 -07:00

go.tools/go/exact: Fix for exact.BinaryOp. It was modifying its argument.

R=golang-dev, gri, mail
CC=golang-dev
https://golang.org/cl/24570043
This commit is contained in:
Richard Musiol 2013-11-12 10:06:24 -08:00 committed by Robert Griesemer
parent d8292e2a38
commit a5baa859ae
2 changed files with 17 additions and 4 deletions

View File

@ -610,12 +610,12 @@ func BinaryOp(x Value, op token.Token, y Value) Value {
im.Add(&bc, &ad)
case token.QUO:
// (ac+bd)/s + i(bc-ad)/s, with s = cc + dd
var ac, bd, bc, ad, s big.Rat
var ac, bd, bc, ad, s, cc, dd big.Rat
ac.Mul(a, c)
bd.Mul(b, d)
bc.Mul(b, c)
ad.Mul(a, d)
s.Add(c.Mul(c, c), d.Mul(d, d))
s.Add(cc.Mul(c, c), dd.Mul(d, d))
re.Add(&ac, &bd)
re.Quo(&re, &s)
im.Sub(&bc, &ad)

View File

@ -46,6 +46,7 @@ var tests = []string{
`0 / 0 = "division_by_zero"`,
`10 / 2 = 5`,
`5 / 3 = 5/3`,
`5i / 3i = 5/3`,
`0 % 0 = "runtime_error:_integer_divide_by_zero"`, // TODO(gri) should be the same as for /
`10 % 3 = 1`,
@ -85,11 +86,23 @@ func TestOps(t *testing.T) {
switch a := strings.Split(test, " "); len(a) {
case 4:
got = doOp(nil, op[a[0]], val(a[1]))
x := val(a[1])
got = doOp(nil, op[a[0]], x)
want = val(a[3])
if !Compare(x, token.EQL, val(a[1])) {
t.Errorf("%s failed: x changed to %s", test, x)
}
case 5:
got = doOp(val(a[0]), op[a[1]], val(a[2]))
x := val(a[0])
y := val(a[2])
got = doOp(x, op[a[1]], y)
want = val(a[4])
if !Compare(x, token.EQL, val(a[0])) {
t.Errorf("%s failed: x changed to %s", test, x)
}
if !Compare(y, token.EQL, val(a[2])) {
t.Errorf("%s failed: y changed to %s", test, y)
}
default:
t.Errorf("invalid test case: %s", test)
continue