Skip to content

Fix round-trip assertion for fractional floating-point formats - #134106

Merged
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-float-formatting-assertion
Sep 22, 2026
Merged

tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-float-formatting-assertion

Conversation

@tannergooding

Copy link
Copy Markdown
Member

Number.FormatFloat checks that its intermediate decimal representation round-trips when the requested precision reaches MaxRoundTripDigits. That threshold counts significant digits, but F, N, C, and P request fractional digits. Leading fractional zeros can consume the requested precision, so correctly rounded output can fail the assertion in debug/checked builds.

Restrict the round-trip assertion to significant-digit modes, retaining the check for shortest-round-trip formatting and sufficiently precise significant-digit formatting. Formatting output and release behavior are unchanged.

Add 48 regression cases for double, float, Half, and BFloat16, covering fractional formats, precision boundaries, negative values, subnormals, and signed zero. Each case checks ToString and UTF-16/UTF-8 TryFormat, including insufficient destination space.

Validation on Windows x64: reproduced the original assertion with the new tests, rebuilt checked CoreLib, and passed all 6,367 tests in the four affected numeric test classes, including all 48 new cases.

Resolves #134101

Note

This PR description and changes were drafted with GitHub Copilot.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

Copilot AI 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.

Copilot review overview

🟢 Approval recommended

The assertion is corrected and comprehensive regression coverage is included.

Review effort: Lite
Findings: None

What changed in this PR

Fixes an incorrect checked/debug round-trip assertion for fractional floating-point formatting without changing release output.

Changes:

  • Restricts assertions to significant-digit formats.
  • Adds 48 regression cases across four floating-point types.
  • Covers ToString, UTF-16/UTF-8 TryFormat, and insufficient buffers.
File Description
src/​libraries/​System.Runtime/​tests/​System.Runtime.Tests/​System/​SingleTests.cs Adds float regression cases.
src/​libraries/​System.Runtime/​tests/​System.Runtime.Tests/​System/​Numerics/​BFloat16Tests.cs Adds BFloat16 regression cases.
src/​libraries/​System.Runtime/​tests/​System.Runtime.Tests/​System/​HalfTests.cs Adds Half regression cases.
src/​libraries/​System.Runtime/​tests/​System.Runtime.Tests/​System/​DoubleTests.cs Adds double regression cases.
src/​libraries/​System.Private.CoreLib/​src/​System/​Number.Formatting.cs Corrects assertion applicability.

@tannergooding

Copy link
Copy Markdown
Member Author

/ba-g unrelated timeouts

@tannergooding
tannergooding merged commit 08b5d08 into dotnet:main Sep 22, 2026
137 of 140 checks passed
@tannergooding

Copy link
Copy Markdown
Member Author

This was just an invalid assert, not backporting.

@tannergooding
tannergooding deleted the tannergooding-float-formatting-assertion branch September 22, 2026 15:30
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Libs: (bug) Debug.Assert in Number.FormatFloat fires for F/N/C/P formats with a large precision specifier

3 participants