https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126731

            Bug ID: 126731
           Summary: `std::format("{}", std::float16_t)` does not agree
                    with `std::to_chars`
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 121539739 at qq dot com
  Target Milestone: ---

For a floating-point argument with no presentation type and no precision,
[format.string.std] specifies the output as:

> equivalent to `to_chars(first, last, value)`

so `std::format("{}", v)` and `std::to_chars(first, last, v)` have to produce
the same characters. For `std::float16_t` and `std::bfloat16_t` they do not.

```cpp
#include <cassert>
#include <charconv>
#include <format>
#include <iterator>
#include <stdfloat>
#include <string>

int main() {
    std::float16_t h = 0.1f16;

    char buf[32];
    auto [ptr, ec] = std::to_chars(buf, std::end(buf), h);
    assert(ec == std::errc{}); // ok

    assert(std::format("{}", h) == std::string(buf, ptr));
    //     "0.099975586"           "0.1"                   fail
}
```

detailed: https://godbolt.org/z/WeYd3o5fh

Reply via email to