Commit 5dc053b9 authored by Martin Möhrmann's avatar Martin Möhrmann Committed by Rob Pike

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/19723Reviewed-by: default avatarRob Pike <r@golang.org>
parent aa5b44ae
......@@ -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")},
......
......@@ -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
......
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