From 06311ed1ced891f0b360a25b352cefafa0d98c46 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 3 Feb 2024 09:31:36 -0800 Subject: [PATCH] Fix fixed rounding around zero in Dragon --- include/fmt/format.h | 7 +++++-- test/format-test.cc | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index ef63f43bf06a..b7169280b375 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3104,8 +3104,11 @@ FMT_CONSTEXPR20 inline void format_dragon(basic_fp value, // Generate the given number of digits. exp10 -= num_digits - 1; if (num_digits <= 0) { - denominator *= 10; - auto digit = add_compare(numerator, numerator, denominator) > 0 ? '1' : '0'; + auto digit = '0'; + if (num_digits == 0) { + denominator *= 10; + digit = add_compare(numerator, numerator, denominator) > 0 ? '1' : '0'; + } buf.push_back(digit); return; } diff --git a/test/format-test.cc b/test/format-test.cc index 8406d86a1e19..67d0631fc2fa 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -953,6 +953,9 @@ TEST(format_test, precision) { EXPECT_EQ(fmt::format("{0:.3}", 1.1), "1.1"); EXPECT_EQ(fmt::format("{:.0e}", 1.0L), "1e+00"); EXPECT_EQ(fmt::format("{:9.1e}", 0.0), " 0.0e+00"); + EXPECT_EQ(fmt::format("{:.7f}", 0.0000000000000071054273576010018587L), + "0.0000000"); + EXPECT_EQ( fmt::format("{:.494}", 4.9406564584124654E-324), "4.9406564584124654417656879286822137236505980261432476442558568250067550"