aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEoghan Sherry <ejsherry@gmail.com>2010-11-30 10:23:27 -0800
committerRobert Griesemer <gri@golang.org>2010-11-30 10:23:27 -0800
commit0dc24603ebe5d8d51690c6af4e2b94ff761ae9b6 (patch)
treefb7c46d1106a92e4de91a856d524697e50c8cc6f
parente1149aa08ddbbbf2cb761f96d6acd04304ed8008 (diff)
downloadgo-0dc24603ebe5d8d51690c6af4e2b94ff761ae9b6.tar.gz
go-0dc24603ebe5d8d51690c6af4e2b94ff761ae9b6.zip
big: fix (*Rat) SetFrac64(a, b) when b < 0.
R=gri CC=golang-dev https://golang.org/cl/3352041
-rw-r--r--src/pkg/big/rat.go3
-rw-r--r--src/pkg/big/rat_test.go23
2 files changed, 24 insertions, 2 deletions
diff --git a/src/pkg/big/rat.go b/src/pkg/big/rat.go
index 40c6ef5bd6..e70673a1cb 100644
--- a/src/pkg/big/rat.go
+++ b/src/pkg/big/rat.go
@@ -35,9 +35,8 @@ func (z *Rat) SetFrac(a, b *Int) *Rat {
func (z *Rat) SetFrac64(a, b int64) *Rat {
z.a.SetInt64(a)
if b < 0 {
- z.b.setUint64(uint64(-b))
+ b = -b
z.a.neg = !z.a.neg
- return z.norm()
}
z.b = z.b.setUint64(uint64(b))
return z.norm()
diff --git a/src/pkg/big/rat_test.go b/src/pkg/big/rat_test.go
index 460ed409e0..8f42949b08 100644
--- a/src/pkg/big/rat_test.go
+++ b/src/pkg/big/rat_test.go
@@ -257,3 +257,26 @@ func TestIssue820(t *testing.T) {
t.Errorf("got %s want %s", z, q)
}
}
+
+
+var setFrac64Tests = []struct {
+ a, b int64
+ out string
+}{
+ {0, 1, "0"},
+ {0, -1, "0"},
+ {1, 1, "1"},
+ {-1, 1, "-1"},
+ {1, -1, "-1"},
+ {-1, -1, "1"},
+ {-9223372036854775808, -9223372036854775808, "1"},
+}
+
+func TestRatSetFrac64Rat(t *testing.T) {
+ for i, test := range setFrac64Tests {
+ x := new(Rat).SetFrac64(test.a, test.b)
+ if x.RatString() != test.out {
+ t.Errorf("#%d got %s want %s", i, x.RatString(), test.out)
+ }
+ }
+}