]> Cypherpunks repositories - gostls13.git/commitdiff
big: bug fix for Quo aliasing problem
authorRobert Griesemer <gri@golang.org>
Tue, 1 Jun 2010 21:37:11 +0000 (14:37 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 1 Jun 2010 21:37:11 +0000 (14:37 -0700)
Fixes #820.

R=rsc
CC=golang-dev
https://golang.org/cl/1453041

src/pkg/big/rat.go
src/pkg/big/rat_test.go

index f35df4b4619b392760e3e6b92b1f17998f302569..ddd858d5ce9ea0517981ad9d7a538fcf3ef7aed0 100644 (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()
 }
 
index 0a779761309e34ba1ebdaf77d80e9a1557993eeb..2379cc0d56acfa11fc3ab5dcf9250b7b746c49c7 100644 (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)
+       }
+}