Skip to content

Commit

Permalink
Fix fixed formatting of small long doubles
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jul 20, 2023
1 parent 95e1ea5 commit 388bc29
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
9 changes: 4 additions & 5 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3060,6 +3060,7 @@ FMT_CONSTEXPR20 inline void format_dragon(basic_fp<uint128_t> value,
}
int even = static_cast<int>((value.f & 1) == 0);
if (!upper) upper = &lower;
bool shortest = num_digits < 0;
if ((flags & dragon::fixup) != 0) {
if (add_compare(numerator, *upper, denominator) + even <= 0) {
--exp10;
Expand All @@ -3072,7 +3073,7 @@ FMT_CONSTEXPR20 inline void format_dragon(basic_fp<uint128_t> value,
if ((flags & dragon::fixed) != 0) adjust_precision(num_digits, exp10 + 1);
}
// Invariant: value == (numerator / denominator) * pow(10, exp10).
if (num_digits < 0) {
if (shortest) {
// Generate the shortest representation.
num_digits = 0;
char* data = buf.data();
Expand Down Expand Up @@ -3102,7 +3103,7 @@ 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) {
if (num_digits <= 0) {
denominator *= 10;
auto digit = add_compare(numerator, numerator, denominator) > 0 ? '1' : '0';
buf.push_back(digit);
Expand Down Expand Up @@ -3323,9 +3324,7 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs,
}

// Compute the actual number of decimal digits to print.
if (fixed) {
adjust_precision(precision, exp + digits_in_the_first_segment);
}
if (fixed) adjust_precision(precision, exp + digits_in_the_first_segment);

// Use Dragon4 only when there might be not enough digits in the first
// segment.
Expand Down
1 change: 1 addition & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,7 @@ TEST(format_test, format_infinity) {
TEST(format_test, format_long_double) {
EXPECT_EQ("0", fmt::format("{0:}", 0.0l));
EXPECT_EQ("0.000000", fmt::format("{0:f}", 0.0l));
EXPECT_EQ("0.0", fmt::format("{:.1f}", 0.000000001l));
EXPECT_EQ("392.65", fmt::format("{0:}", 392.65l));
EXPECT_EQ("392.65", fmt::format("{0:g}", 392.65l));
EXPECT_EQ("392.65", fmt::format("{0:G}", 392.65l));
Expand Down

0 comments on commit 388bc29

Please sign in to comment.