Round hexfloat ties to even like printf (#5002)

This commit is contained in:
Eminy 2026-11-06 23:52:29 +03:00 committed by GitHub
parent b1397ab7bc
commit 9e7aaa3f27
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 1 deletions

View File

@ -3354,8 +3354,11 @@ FMT_CONSTEXPR20 void format_hexfloat(Float value, format_specs specs,
const int shift = ((print_xdigits - specs.precision - 1) * 4); const int shift = ((print_xdigits - specs.precision - 1) * 4);
const auto mask = carrier_uint(0xF) << shift; const auto mask = carrier_uint(0xF) << shift;
const auto v = static_cast<uint32_t>((f.f & mask) >> shift); const auto v = static_cast<uint32_t>((f.f & mask) >> shift);
// Round half to even like printf.
const bool odd = ((f.f >> (shift + 4)) & 1) != 0;
const bool rest_is_zero = (f.f & ((carrier_uint(1) << shift) - 1)) == 0;
if (v >= 8) { if (v > 8 || (v == 8 && (odd || !rest_is_zero))) {
const auto inc = carrier_uint(1) << (shift + 4); const auto inc = carrier_uint(1) << (shift + 4);
f.f += inc; f.f += inc;
f.f &= ~(inc - 1); f.f &= ~(inc - 1);

View File

@ -1749,6 +1749,11 @@ TEST(format_test, format_double) {
} }
EXPECT_EQ(fmt::format("{:.10a}", 4.2), "0x1.0ccccccccdp+2"); EXPECT_EQ(fmt::format("{:.10a}", 4.2), "0x1.0ccccccccdp+2");
// Ties round to even like printf.
EXPECT_EQ(fmt::format("{:.1a}", 1.03125), "0x1.0p+0");
EXPECT_EQ(fmt::format("{:.1a}", 1.09375), "0x1.2p+0");
EXPECT_EQ(fmt::format("{:.1a}", 1.0322265625), "0x1.1p+0");
EXPECT_EQ(fmt::format("{:.1A}", 1.03125f), "0X1.0P+0");
EXPECT_EQ(fmt::format("{:a}", -42.0), "-0x1.5p+5"); EXPECT_EQ(fmt::format("{:a}", -42.0), "-0x1.5p+5");
EXPECT_EQ(fmt::format("{:A}", -42.0), "-0X1.5P+5"); EXPECT_EQ(fmt::format("{:A}", -42.0), "-0X1.5P+5");