Skip to content

Fix high-precision floating-point formatting - #4960

Merged
vitaut merged 3 commits into
fmtlib:mainfrom
Boulea7:ln-cx/fmt-high-precision-trailing-zeros
Oct 7, 2026
Merged

vitaut merged 3 commits into
fmtlib:mainfrom
Boulea7:ln-cx/fmt-high-precision-trailing-zeros

Conversation

@Boulea7

@Boulea7 Boulea7 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Fix high-precision floating-point formatting that drops requested trailing zeros or truncates digits in the Dragon fallback. For example, formatting 0.0625 with {:.768f} now preserves all 768 fractional places.

The fallback bounds digit generation using the value's exponent and significand, while the fast double path keeps its existing limit. Fixed and alternate-general formatting then append the requested zeros. Constant-evaluated buffer growth allocates usable storage and starts element lifetimes before writing to it.

Regression tests cover trailing zeros, bounded output, long double, and high-precision constexpr formatting. The affected C++20 format and compile suites pass on Linux. Additional GCC/Clang checks cover constexpr output and unchanged runtime link requirements; x87 and binary128 results were checked against an integer-arithmetic oracle.

MSVC and module builds have not been run locally.

@Boulea7
Boulea7 requested a review from vitaut as a code owner September 29, 2026 17:34
Copilot AI balanced review requested due to automatic review settings September 29, 2026 17:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Boulea7 Boulea7 changed the title Fix trailing zeros in high-precision floating-point formatting Fix high-precision floating-point formatting Oct 1, 2026

@vitaut vitaut left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

Comment thread include/fmt/format.h Outdated
#if FMT_USE_CONSTEVAL
// Use the builtin directly to avoid C++ runtime dependencies at -O0.
if (__builtin_is_constant_evaluated())
return std::allocator<T>().allocate(n);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use new/delete and avoid conditional dependency on <memory>.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched constexpr allocation to new[]/delete[] and removed the added <memory> include. The growth loop still starts element lifetimes for standard and custom allocators; local GCC and Clang constexpr checks pass.

Comment thread include/fmt/format.h
Comment thread test/format-test.cc
EXPECT_EQ(fmt::format("{0:.6}", "123456\xad"), "123456");
}

TEST(format_test, high_precision_trailing_zeros) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we trim these tests a bit? There seems to be quite a bit of overlapping coverage, especially around sign variants, nearby precisions, and simple zero/0.5 cases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trimmed the nearby precision and sign variants, plus the repeated long double zero/0.5 cases. The remaining tests cover trailing zeros, padding, bounded output, and extreme exponents.

@vitaut
vitaut merged commit c4927e6 into fmtlib:main Oct 7, 2026
47 checks passed
@vitaut

vitaut commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Thanks for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants