1
0
mirror of https://github.com/golang/go synced 2024-11-11 23:20:24 -07:00

big: fix (*Rat) SetFrac64(a, b) when b < 0.

R=gri
CC=golang-dev
https://golang.org/cl/3352041
This commit is contained in:
Eoghan Sherry 2010-11-30 10:23:27 -08:00 committed by Robert Griesemer
parent e1149aa08d
commit 0dc24603eb
2 changed files with 24 additions and 2 deletions

View File

@ -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()

View File

@ -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)
}
}
}