summaryrefslogtreecommitdiff
path: root/src/pkg/big
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2010-06-01 14:37:11 -0700
committerRobert Griesemer <gri@golang.org>2010-06-01 14:37:11 -0700
commit751ec5e92b3872bd752eac82f0feb96cdc1a3b8a (patch)
treefa228914cfe6a80dc78f13ad9cdf2b38f2f77179 /src/pkg/big
parent2fd81e73dce7b5aac0825d3e92d36400e07e4117 (diff)
downloadgolang-751ec5e92b3872bd752eac82f0feb96cdc1a3b8a.tar.gz
big: bug fix for Quo aliasing problem
Fixes issue 820. R=rsc CC=golang-dev http://codereview.appspot.com/1453041
Diffstat (limited to 'src/pkg/big')
-rw-r--r--src/pkg/big/rat.go8
-rw-r--r--src/pkg/big/rat_test.go26
2 files changed, 31 insertions, 3 deletions
diff --git a/src/pkg/big/rat.go b/src/pkg/big/rat.go
index f35df4b46..ddd858d5c 100644
--- a/src/pkg/big/rat.go
+++ b/src/pkg/big/rat.go
@@ -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()
}
diff --git a/src/pkg/big/rat_test.go b/src/pkg/big/rat_test.go
index 0a7797613..2379cc0d5 100644
--- a/src/pkg/big/rat_test.go
+++ b/src/pkg/big/rat_test.go
@@ -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)
+ }
+}