Fix fixed rounding around zero in Dragon

This commit is contained in:
Victor Zverovich 2024-02-03 09:31:36 -08:00
parent e5bab8dab4
commit 06311ed1ce
2 changed files with 8 additions and 2 deletions

View File

@ -3104,8 +3104,11 @@ FMT_CONSTEXPR20 inline void format_dragon(basic_fp<uint128_t> 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;
}

View File

@ -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"