GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
libstdc++

libstdc++: Format std::float16_t and std::bfloat16_t correctly

Formatting of float16_t and bfloat16_t now uses their dedicated to_chars overloads, fixing incorrect formatting that cast to float.

The libstdc++ formatting library has been updated to correctly format std::float16_t and std::bfloat16_t types. Previously, these types were incorrectly formatted by casting them to float, leading to potential precision loss and incorrect output. This patch removes the specialized formatters for these types, allowing the generic floating-point specialization to utilize their dedicated std::to_chars overloads, ensuring accurate representation.

In Details

This patch removes the explicit std::formatter specializations for float16_t and __format::__bflt16_t which previously cast the values to float before formatting. This was a workaround until dedicated std::to_chars overloads for these types were added. Now that the to_chars overloads exist and are correctly constrained by __formattable_float, the generic floating-point formatter can be used, which correctly invokes these overloads. This ensures that the shortest possible string representation is generated, as per the original design of the to_chars overloads.

For Context
std::float16_t
A C++ standard type representing a 16-bit floating-point number, conforming to the IEEE 754 half-precision format.
std::bfloat16_t
A C++ standard type representing a 16-bit floating-point number, conforming to the Brain Floating Point format, which offers a wider dynamic range than half-precision at the cost of less precision.
std::to_chars
A C++ standard library function for converting numerical values to character sequences, offering locale-independent and efficient formatting.
Formatter
In C++'s <format> library, a specialization of std::formatter that defines how to format a specific type into a character sequence.
Filed Under: libstdc++formattingfloat16bfloat16