mirror of
https://github.com/golang/go
synced 2024-11-19 02:54:42 -07:00
big: fix panic and round correctly in Rat.FloatString
R=gri, rsc CC=golang-dev https://golang.org/cl/2212044
This commit is contained in:
parent
1f6f563900
commit
5781a00e00
@ -294,25 +294,34 @@ func (z *Rat) FloatString(prec int) string {
|
||||
|
||||
q, r := nat{}.div(nat{}, z.a.abs, z.b)
|
||||
|
||||
p := natOne
|
||||
if prec > 0 {
|
||||
p = nat{}.expNN(natTen, nat{}.setUint64(uint64(prec)), nil)
|
||||
}
|
||||
|
||||
r = r.mul(r, p)
|
||||
r, r2 := r.div(nat{}, r, z.b)
|
||||
|
||||
// see if we need to round up
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
if z.b.cmp(r2) <= 0 {
|
||||
r = r.add(r, natOne)
|
||||
if prec > 0 {
|
||||
rs := r.string(10)
|
||||
leadingZeros := prec - len(rs)
|
||||
s += "." + strings.Repeat("0", leadingZeros) + rs
|
||||
}
|
||||
|
||||
rs := r.string(10)
|
||||
leadingZeros := prec - len(rs)
|
||||
s += "." + strings.Repeat("0", leadingZeros) + rs
|
||||
s = strings.TrimRight(s, "0")
|
||||
|
||||
return s
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user