mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 02:37:36 +02:00
Fix fixed formatting of small long doubles
This commit is contained in:
@ -3060,6 +3060,7 @@ FMT_CONSTEXPR20 inline void format_dragon(basic_fp<uint128_t> value,
|
|||||||
}
|
}
|
||||||
int even = static_cast<int>((value.f & 1) == 0);
|
int even = static_cast<int>((value.f & 1) == 0);
|
||||||
if (!upper) upper = &lower;
|
if (!upper) upper = &lower;
|
||||||
|
bool shortest = num_digits < 0;
|
||||||
if ((flags & dragon::fixup) != 0) {
|
if ((flags & dragon::fixup) != 0) {
|
||||||
if (add_compare(numerator, *upper, denominator) + even <= 0) {
|
if (add_compare(numerator, *upper, denominator) + even <= 0) {
|
||||||
--exp10;
|
--exp10;
|
||||||
@ -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);
|
if ((flags & dragon::fixed) != 0) adjust_precision(num_digits, exp10 + 1);
|
||||||
}
|
}
|
||||||
// Invariant: value == (numerator / denominator) * pow(10, exp10).
|
// Invariant: value == (numerator / denominator) * pow(10, exp10).
|
||||||
if (num_digits < 0) {
|
if (shortest) {
|
||||||
// Generate the shortest representation.
|
// Generate the shortest representation.
|
||||||
num_digits = 0;
|
num_digits = 0;
|
||||||
char* data = buf.data();
|
char* data = buf.data();
|
||||||
@ -3102,7 +3103,7 @@ FMT_CONSTEXPR20 inline void format_dragon(basic_fp<uint128_t> value,
|
|||||||
}
|
}
|
||||||
// Generate the given number of digits.
|
// Generate the given number of digits.
|
||||||
exp10 -= num_digits - 1;
|
exp10 -= num_digits - 1;
|
||||||
if (num_digits == 0) {
|
if (num_digits <= 0) {
|
||||||
denominator *= 10;
|
denominator *= 10;
|
||||||
auto digit = add_compare(numerator, numerator, denominator) > 0 ? '1' : '0';
|
auto digit = add_compare(numerator, numerator, denominator) > 0 ? '1' : '0';
|
||||||
buf.push_back(digit);
|
buf.push_back(digit);
|
||||||
@ -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.
|
// Compute the actual number of decimal digits to print.
|
||||||
if (fixed) {
|
if (fixed) adjust_precision(precision, exp + digits_in_the_first_segment);
|
||||||
adjust_precision(precision, exp + digits_in_the_first_segment);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use Dragon4 only when there might be not enough digits in the first
|
// Use Dragon4 only when there might be not enough digits in the first
|
||||||
// segment.
|
// segment.
|
||||||
|
@ -1472,6 +1472,7 @@ TEST(format_test, format_infinity) {
|
|||||||
TEST(format_test, format_long_double) {
|
TEST(format_test, format_long_double) {
|
||||||
EXPECT_EQ("0", fmt::format("{0:}", 0.0l));
|
EXPECT_EQ("0", fmt::format("{0:}", 0.0l));
|
||||||
EXPECT_EQ("0.000000", fmt::format("{0:f}", 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:}", 392.65l));
|
||||||
EXPECT_EQ("392.65", fmt::format("{0:g}", 392.65l));
|
EXPECT_EQ("392.65", fmt::format("{0:g}", 392.65l));
|
||||||
EXPECT_EQ("392.65", fmt::format("{0:G}", 392.65l));
|
EXPECT_EQ("392.65", fmt::format("{0:G}", 392.65l));
|
||||||
|
Reference in New Issue
Block a user