From: Martin Möhrmann Date: Sun, 21 Feb 2016 09:46:59 +0000 (+0100) Subject: fmt: fix zero padding for NaN X-Git-Tag: go1.7beta1~1798 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=5dc053b9dec4dc25fac195065ad32462ac28a543;p=gostls13.git fmt: fix zero padding for NaN Makes zero padding of NaN and infinities consistent by using spaces instead of zeroes to pad NaN. Adds more tests for NaN formatting. Fixes #14421 Change-Id: Ia20f8e878cc81ac72a744ec10d65e84b94e09c6a Reviewed-on: https://go-review.googlesource.com/19723 Reviewed-by: Rob Pike --- diff --git a/src/fmt/fmt_test.go b/src/fmt/fmt_test.go index 8d7c36ceb1..1d9d015f4a 100644 --- a/src/fmt/fmt_test.go +++ b/src/fmt/fmt_test.go @@ -386,6 +386,9 @@ var fmtTests = []struct { {"%20e", math.Inf(1), " +Inf"}, {"%-20f", math.Inf(-1), "-Inf "}, {"%20g", math.NaN(), " NaN"}, + {"%+20f", math.NaN(), " +NaN"}, + {"% -20f", math.NaN(), " NaN "}, + {"%+-20f", math.NaN(), "+NaN "}, // arrays {"%v", array, "[1 2 3 4 5]"}, @@ -654,13 +657,19 @@ var fmtTests = []struct { // Complex numbers: exhaustively tested in TestComplexFormatting. {"%7.2f", 1 + 2i, "( 1.00 +2.00i)"}, {"%+07.2f", -1 - 2i, "(-001.00-002.00i)"}, - // Zero padding does not apply to infinities. + // Zero padding does not apply to infinities and NaN. {"%020f", math.Inf(-1), " -Inf"}, {"%020f", math.Inf(+1), " +Inf"}, + {"%020f", math.NaN(), " NaN"}, {"% 020f", math.Inf(-1), " -Inf"}, {"% 020f", math.Inf(+1), " Inf"}, + {"% 020f", math.NaN(), " NaN"}, {"%+020f", math.Inf(-1), " -Inf"}, {"%+020f", math.Inf(+1), " +Inf"}, + {"%+020f", math.NaN(), " +NaN"}, + {"%-020f", math.Inf(-1), "-Inf "}, + {"%-020f", math.Inf(+1), "+Inf "}, + {"%-020f", math.NaN(), "NaN "}, {"%20f", -1.0, " -1.000000"}, // Make sure we can handle very large widths. {"%0100f", -1.0, zeroFill("-", 99, "1.000000")}, diff --git a/src/fmt/format.go b/src/fmt/format.go index bf9d00bbc0..c811cc6a3d 100644 --- a/src/fmt/format.go +++ b/src/fmt/format.go @@ -414,11 +414,15 @@ func (f *fmt) formatFloat(v float64, verb byte, prec, n int) { if f.space && num[0] == '+' { num[0] = ' ' } - // Special handling for "+Inf" and "-Inf", + // Special handling for infinities and NaN, // which don't look like a number so shouldn't be padded with zeros. - if num[1] == 'I' { + if num[1] == 'I' || num[1] == 'N' { oldZero := f.zero f.zero = false + // Remove sign before NaN if not asked for. + if num[1] == 'N' && !f.space && !f.plus { + num = num[1:] + } f.pad(num) f.zero = oldZero return