Skip to content

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

Open
advitrocks9 wants to merge 1 commit into
fmtlib:mainfrom
advitrocks9:nonfinite-default-align
Open

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

Conversation

@advitrocks9

Copy link
Copy Markdown

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
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

1 participant