Skip to content

Right-align inf and nan with a bare width - #4895

Merged
vitaut merged 1 commit into
fmtlib:mainfrom
advitrocks9:nonfinite-default-align
Aug 27, 2026
Merged

Right-align inf and nan with a bare width#4895
vitaut merged 1 commit into
fmtlib:mainfrom
advitrocks9:nonfinite-default-align

Conversation

@advitrocks9

Copy link
Copy Markdown
Contributor

Fixes #4894.

The nonfinite path in write_float takes the string default alignment rather than the numeric one, so inf and nan come out left-aligned while finite values with the same spec come out right-aligned.

fmt::print("[{:10}]\n", 1.5);   // [       1.5]
fmt::print("[{:10}]\n", inf);   // [inf       ]

After the change both are right-aligned, matching std::format and the documented default. An explicit alignment is unaffected.

format-test.cc gains cases for inf and nan under a bare width. The assertion next to them already expected the new string for a signed nonfinite, so it was passing for the wrong reason.

fmt::format("{:6}", nan) gave "nan   " where std::format and printf("%6f")
give "   nan". write_nonfinite was the only numeric write path taking
write_padded's align::left default, so a width with no explicit align
left-aligned inf and nan while every finite value right-aligned. That
default only reaches align::none, leaving explicit <, > and ^ and the 0
flag unchanged.
@advitrocks9
advitrocks9 requested a review from vitaut as a code owner August 24, 2026 09:18
@vitaut

vitaut commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Overall looks good but some CI jobs failed to start and are in a weird state. Could you rebase to rerun them?

@advitrocks9

Copy link
Copy Markdown
Contributor Author

Rebased.

@fallenmi fallenmi 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.

Approved at exact head 7449a2b after independent validation.

I reproduced the bug on base/current main e27cc20: a bare width left-aligns nan and inf, unlike finite numeric values. The same seven-case oracle passes on this head, covering NaN, positive and negative infinity, explicit left and center alignment, zero-fill normalization, and finite-value parity. The full format-test binary passes (139 tests) with AppleClang, and git diff --check is clean. The write_padded<Char, align::right> default is correctly overridden by explicit alignment.

Reviewed with OpenAI Codex.

@vitaut

vitaut commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

@fallenmi, please don't spam the issues with AI-generated comments.

@vitaut
vitaut merged commit e76a952 into fmtlib:main Aug 27, 2026
17 checks passed
@vitaut

vitaut commented Aug 27, 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.

inf and nan are left-aligned with a bare width

3 participants