aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Martin <ality@pbrane.org>2010-10-07 16:10:48 +0200
committerRobert Griesemer <gri@golang.org>2010-10-07 16:10:48 +0200
commit5781a00e007fb10815e298095fc65b7798278607 (patch)
treeaa23ee07ba2fe06021c2ab7a32c240350494f273
parent1f6f56390098d0bc922c505dadbc8ee7cc396fa2 (diff)
downloadgo-5781a00e007fb10815e298095fc65b7798278607.tar.gz
go-5781a00e007fb10815e298095fc65b7798278607.zip
big: fix panic and round correctly in Rat.FloatString
R=gri, rsc CC=golang-dev https://golang.org/cl/2212044
-rw-r--r--src/pkg/big/rat.go27
-rw-r--r--src/pkg/big/rat_test.go5
2 files changed, 23 insertions, 9 deletions
diff --git a/src/pkg/big/rat.go b/src/pkg/big/rat.go
index 22ae8d2d8b..40c6ef5bd6 100644
--- a/src/pkg/big/rat.go
+++ b/src/pkg/big/rat.go
@@ -294,25 +294,34 @@ func (z *Rat) FloatString(prec int) string {
q, r := nat{}.div(nat{}, z.a.abs, z.b)
- s := q.string(10)
- if z.a.neg {
- s = "-" + s
+ p := natOne
+ if prec > 0 {
+ p = nat{}.expNN(natTen, nat{}.setUint64(uint64(prec)), nil)
}
- p := nat{}.expNN(natTen, nat{Word(prec)}, nil)
r = r.mul(r, p)
r, r2 := r.div(nat{}, r, z.b)
// see if we need to round up
- r2 = r2.mul(r2, natTwo)
+ r2 = r2.add(r2, r2)
if z.b.cmp(r2) <= 0 {
r = r.add(r, natOne)
+ if r.cmp(p) >= 0 {
+ q = nat{}.add(q, natOne)
+ r = nat{}.sub(r, p)
+ }
+ }
+
+ s := q.string(10)
+ if z.a.neg {
+ s = "-" + s
}
- rs := r.string(10)
- leadingZeros := prec - len(rs)
- s += "." + strings.Repeat("0", leadingZeros) + rs
- s = strings.TrimRight(s, "0")
+ if prec > 0 {
+ rs := r.string(10)
+ leadingZeros := prec - len(rs)
+ s += "." + strings.Repeat("0", leadingZeros) + rs
+ }
return s
}
diff --git a/src/pkg/big/rat_test.go b/src/pkg/big/rat_test.go
index ccc9cf6056..ff2bb9978b 100644
--- a/src/pkg/big/rat_test.go
+++ b/src/pkg/big/rat_test.go
@@ -71,6 +71,11 @@ var floatStringTests = []floatStringTest{
floatStringTest{".25", 1, "0.3"},
floatStringTest{"-1/3", 3, "-0.333"},
floatStringTest{"-2/3", 4, "-0.6667"},
+ floatStringTest{"0.96", 1, "1.0"},
+ floatStringTest{"0.999", 2, "1.00"},
+ floatStringTest{"0.9", 0, "1"},
+ floatStringTest{".25", -1, "0"},
+ floatStringTest{".55", -1, "1"},
}
func TestFloatString(t *testing.T) {