Title
`format`: `a` and `A` should insert the `0x` or `0X` prefix
Status
new
Section
[format.string.std]
Submitter
Tianying Chen

Created on 2026-01-30.00:00:00 last changed 1 week ago

Messages

Date: 2026-01-31.14:37:05

Proposed resolution:

This wording is relative to N5032.

  1. Modify Table [tab:format.type.float] in [format.string.std] as indicated:

    Table 110 — Meaning of type options for floating-point types [tab:format.type.float]
    Type Meaning
    a If precision is specified, equivalent to
    to_chars(first, last, value, chars_format::hex, precision)
    
    where precision is the specified formatting precision; equivalent to
    to_chars(first, last, value, chars_format::hex)
    
    otherwise. In either case, when value is finite, the base prefix
    0x is inserted after the sign character (possibly space) if there is one,
    or before the output of to_chars otherwise.
    A The same as a, except that it uses uppercase letters for digits above 9 and P to indicate
    the exponent, and that the base prefix is 0X.
    […]
Date: 2026-01-30.00:00:00

In `format`, the floating-point presentation types `a` and `A` are specified in terms of `to_chars` in `chars_format::hex` format. However, `to_chars` explicitly specifies that there's no leading `0x` in the result if `fmt` is `chars_format::hex`. This causes inconsistency in the presence of `0x` or `0X` prefix between `format`/`print` and `printf`/`iostream`:

std::println("{:a} {:A}", std::numbers::e, -std::numbers::phi);
std::printf("%a %A\n", std::numbers::e, -std::numbers::phi);

may produce

1.5bf0a8b145769p+1 -1.9E3779B97F4A8P+0
0x1.5bf0a8b145769p+1 -0X1.9E3779B97F4A8P+0

We should fix the behavior of `a` and `A` in `format` to include the `0x` and `0X` prefix respectively.

Victor Zverovich noted that `format` intended to print hexfloat with `0x` like other text formatting facilities, and that an unintentional change of behavior was introduced in P0645R4 when switching to `to_chars`-based wording.

History
Date User Action Args
2026-01-31 14:37:05adminsetmessages: + msg15915
2026-01-30 00:00:00admincreate