std::format and negative zeroes
Richard Smith

Created on 2019-07-31.00:00:00, last changed 2019-08-17.10:48:27.


Date: 2019-08-17.15:29:07

Proposed resolution:

This wording is relative to N4830.

  1. Modify the sign options Table [tab:format.sign] in [format.string.std] as indicated:

    Table 59: Meaning of sign options [tab:format.sign]
    Option Meaning
    '+' Indicates that a sign should be used for both non-negative and negative numbers.
    '-' Indicates that a sign should be used only for negative numbers and negative zero (this is the default behavior).
    space Indicates that a leading space should be used for non-negative numbers other than negative zero, and a minus sign for negative numbers and negative zero.
Date: 2019-08-17.00:00:00

[ 2019-08-17 Priority set to 2 based on reflector discussion ]

Date: 2019-07-31.00:00:00

What are these:

std::format("{}", -0.0);
std::format("{:+}", -0.0);
std::format("{:-}", -0.0);
std::format("{: }", -0.0);


"{:{}} {}"

A negative zero is not a negative number, so I think the answer for an implementation that supports signed zeroes is "0", "-0", "0", " 0". Is that the intent? (Note that this doesn't satisfy to_chars' round-trip guarantee.)

Or should the behavior actually be that "+" means "insert a leading + if to_chars' output does not start with -" and " " actually means "insert a leading space if to_chars' output does not start with -" (that is, the same as "%+f" and "% f") — so that the result of all four calls is "-0"?

Victor Zverovich:

The latter. std::format is specified in terms of to_chars and the intent is to have similar round trip guarantee here, so the result should be "-0", "-0", "-0", "-0". This has also been extensively discussed in LEWG and the outcome of that discussion was to print '-' for negative zeros.

So I think it should be clarified that '-' and space apply to negative numbers and negative zero.

Date User Action Args
2019-08-17 10:48:27adminsetmessages: + msg10555
2019-08-03 17:54:18adminsetmessages: + msg10534
2019-07-31 00:00:00admincreate