mirror of
https://github.com/golang/go
synced 2024-11-19 04:44:41 -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:
parent
d8292e2a38
commit
a5baa859ae
@ -610,12 +610,12 @@ func BinaryOp(x Value, op token.Token, y Value) Value {
|
|||||||
im.Add(&bc, &ad)
|
im.Add(&bc, &ad)
|
||||||
case token.QUO:
|
case token.QUO:
|
||||||
// (ac+bd)/s + i(bc-ad)/s, with s = cc + dd
|
// (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)
|
ac.Mul(a, c)
|
||||||
bd.Mul(b, d)
|
bd.Mul(b, d)
|
||||||
bc.Mul(b, c)
|
bc.Mul(b, c)
|
||||||
ad.Mul(a, d)
|
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.Add(&ac, &bd)
|
||||||
re.Quo(&re, &s)
|
re.Quo(&re, &s)
|
||||||
im.Sub(&bc, &ad)
|
im.Sub(&bc, &ad)
|
||||||
|
@ -46,6 +46,7 @@ var tests = []string{
|
|||||||
`0 / 0 = "division_by_zero"`,
|
`0 / 0 = "division_by_zero"`,
|
||||||
`10 / 2 = 5`,
|
`10 / 2 = 5`,
|
||||||
`5 / 3 = 5/3`,
|
`5 / 3 = 5/3`,
|
||||||
|
`5i / 3i = 5/3`,
|
||||||
|
|
||||||
`0 % 0 = "runtime_error:_integer_divide_by_zero"`, // TODO(gri) should be the same as for /
|
`0 % 0 = "runtime_error:_integer_divide_by_zero"`, // TODO(gri) should be the same as for /
|
||||||
`10 % 3 = 1`,
|
`10 % 3 = 1`,
|
||||||
@ -85,11 +86,23 @@ func TestOps(t *testing.T) {
|
|||||||
|
|
||||||
switch a := strings.Split(test, " "); len(a) {
|
switch a := strings.Split(test, " "); len(a) {
|
||||||
case 4:
|
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])
|
want = val(a[3])
|
||||||
|
if !Compare(x, token.EQL, val(a[1])) {
|
||||||
|
t.Errorf("%s failed: x changed to %s", test, x)
|
||||||
|
}
|
||||||
case 5:
|
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])
|
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:
|
default:
|
||||||
t.Errorf("invalid test case: %s", test)
|
t.Errorf("invalid test case: %s", test)
|
||||||
continue
|
continue
|
||||||
|
Loading…
Reference in New Issue
Block a user