Skip to content

Commit

Permalink
Fix fixed rounding around zero in Dragon
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Feb 3, 2024
1 parent e5bab8d commit 06311ed
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 06311ed

Please sign in to comment.