Commit 57c1485f authored by Robert Griesemer's avatar Robert Griesemer

math/big: simplified formatting logic

Change-Id: I4329c44b829fcd77e4f1a1d45904f0f8a280a595
Reviewed-on: https://go-review.googlesource.com/3940Reviewed-by: 's avatarAlan Donovan <adonovan@google.com>
parent 01718225
...@@ -111,9 +111,8 @@ func fmtE(buf []byte, fmt byte, prec int, neg bool, d decimal) []byte { ...@@ -111,9 +111,8 @@ func fmtE(buf []byte, fmt byte, prec int, neg bool, d decimal) []byte {
// .moredigits // .moredigits
if prec > 0 { if prec > 0 {
buf = append(buf, '.') buf = append(buf, '.')
// TODO(gri) clean up logic below
i := 1 i := 1
m := len(d.mant) + prec + 1 - max(len(d.mant), prec+1) m := min(len(d.mant), prec+1)
if i < m { if i < m {
buf = append(buf, d.mant[i:m]...) buf = append(buf, d.mant[i:m]...)
i = m i = m
...@@ -151,14 +150,11 @@ func fmtF(buf []byte, prec int, neg bool, d decimal) []byte { ...@@ -151,14 +150,11 @@ func fmtF(buf []byte, prec int, neg bool, d decimal) []byte {
buf = append(buf, '-') buf = append(buf, '-')
} }
// integer, padded with zeros as needed. // integer, padded with zeros as needed
if d.exp > 0 { if d.exp > 0 {
// TODO(gri) fuse loops below and/or cleanup m := min(len(d.mant), d.exp)
var i int buf = append(buf, d.mant[:m]...)
for i = 0; i < int(d.exp) && i < len(d.mant); i++ { for ; m < d.exp; m++ {
buf = append(buf, d.mant[i])
}
for ; i < d.exp; i++ {
buf = append(buf, '0') buf = append(buf, '0')
} }
} else { } else {
...@@ -179,3 +175,10 @@ func fmtF(buf []byte, prec int, neg bool, d decimal) []byte { ...@@ -179,3 +175,10 @@ func fmtF(buf []byte, prec int, neg bool, d decimal) []byte {
return buf return buf
} }
func min(x, y int) int {
if x < y {
return x
}
return y
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment