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

Created on 2019-07-31.00:00:00 last changed 46 months ago

Messages

Date: 2020-02-14.11:24:43

Proposed resolution:

This wording is relative to N4849.

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

    Table 58: 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 only (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: 2020-02-14.11:24:43

[ 2020-02 Status to Immediate on Thursday night in Prague. ]

Date: 2020-02-15.00:00:00

[ 2020-02-13, Prague ]

Rebased and some wording finetuning by LWG.

Date: 2019-08-17.00:00:00

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

Previous resolution [SUPERSEDED]:

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-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
2021-02-25 10:48:01adminsetstatus: wp -> c++20
2020-02-24 16:02:59adminsetstatus: immediate -> wp
2020-02-14 11:24:43adminsetmessages: + msg11116
2020-02-14 11:24:43adminsetstatus: new -> immediate
2020-02-13 19:29:26adminsetmessages: + msg11082
2019-08-17 10:48:27adminsetmessages: + msg10555
2019-08-03 17:54:18adminsetmessages: + msg10534
2019-07-31 00:00:00admincreate