Title
std::format and negative zeroes
Status
new
Section
[format.string]
Submitter
Richard Smith

Created on 2019-07-31.00:00:00, last changed 2019-08-03.17:54:18.

Messages

Date: 2019-08-03.17:54:18

Proposed resolution:

This wording is relative to the Post Cologne working draft.

  1. Modify the sign option table in [format.string] as indicated:

    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, and a minus sign for negative numbers and negative zero.
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);

with

"{:{}} {}"

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.

History
Date User Action Args
2019-08-03 17:54:18adminsetmessages: + msg10534
2019-07-31 00:00:00admincreate