]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: fix zero padding for NaN
authorMartin Möhrmann <martisch@uos.de>
Sun, 21 Feb 2016 09:46:59 +0000 (10:46 +0100)
committerRob Pike <r@golang.org>
Sun, 21 Feb 2016 12:04:21 +0000 (12:04 +0000)
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 <r@golang.org>
src/fmt/fmt_test.go
src/fmt/format.go

index 8d7c36ceb1432ef76774cf798b2a32d646f3c17a..1d9d015f4ab426e1612da5cc4d8d24502611b1e1 100644 (file)
@@ -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")},
index bf9d00bbc03b3ae39959a78901a6a2b56687bae6..c811cc6a3d9d1b2a109ce7c91229da95259bf3b5 100644 (file)
@@ -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