1
0
mirror of https://github.com/golang/go synced 2024-11-12 06:30:21 -07:00

big: bug fix for Quo aliasing problem

Fixes #820.

R=rsc
CC=golang-dev
https://golang.org/cl/1453041
This commit is contained in:
Robert Griesemer 2010-06-01 14:37:11 -07:00
parent 0c77ba96da
commit ab215f73fc
2 changed files with 31 additions and 3 deletions

View File

@ -160,9 +160,11 @@ func (z *Rat) Quo(x, y *Rat) *Rat {
if len(y.a.abs) == 0 {
panic("division by zero")
}
z.a.abs = z.a.abs.mul(x.a.abs, y.b)
z.b = z.b.mul(x.b, y.a.abs)
z.a.neg = x.a.neg != y.a.neg
a := mulNat(&x.a, y.b)
b := mulNat(&y.a, x.b)
z.a.abs = a.abs
z.b = b.abs
z.a.neg = a.neg != b.neg
return z.norm()
}

View File

@ -175,3 +175,29 @@ func TestRatBin(t *testing.T) {
}
}
}
func TestIssue820(t *testing.T) {
x := NewRat(3, 1)
y := NewRat(2, 1)
z := y.Quo(x, y)
q := NewRat(3, 2)
if z.Cmp(q) != 0 {
t.Errorf("got %s want %s", z, q)
}
y = NewRat(3, 1)
x = NewRat(2, 1)
z = y.Quo(x, y)
q = NewRat(2, 3)
if z.Cmp(q) != 0 {
t.Errorf("got %s want %s", z, q)
}
x = NewRat(3, 1)
z = x.Quo(x, x)
q = NewRat(3, 3)
if z.Cmp(q) != 0 {
t.Errorf("got %s want %s", z, q)
}
}