Skip to content

Fix hexadecimal float sign round-tripping - #135132

Merged
tannergooding merged 6 commits into
dotnet:mainfrom
tannergooding:tannergooding-numeric-inconsistency
Oct 6, 2026
Merged

tannergooding merged 6 commits into
dotnet:mainfrom
tannergooding:tannergooding-numeric-inconsistency

Conversation

@tannergooding

@tannergooding tannergooding commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

Summary

Hex-float and ordinary numeric parsing now select the longest matching custom sign, preferring PositiveSign on equal-length matches. This makes custom prefix-overlapping signs round-trip consistently for hexadecimal floats, scientific (E) formats, and integers. Regression coverage includes double, float, Half, BFloat16, and Int32.

Validation

  • Checked CoreLib build — passed.
  • System.Runtime.Tests — 78,557 total, 0 failed, 87 skipped (78,470 passed).

Resolves #135039

Note

This PR description was generated with GitHub Copilot.

Use culture-aware exponent signs and the standard numeric parser sign precedence for hexadecimal floating-point values.

Co-authored-by: Copilot App <[email protected]>
@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

🟡 Changes recommended

The documentation remains contradictory and the affected public BFloat16 type lacks equivalent regression coverage.

Review effort: Balanced
Findings: 2 Low severity

Open (2)
What changed in this PR

Fixes hexadecimal floating-point round-tripping with culture-specific signs.

Changes:

  • Uses NumberFormatInfo signs when formatting and parsing exponents.
  • Adds custom-sign documentation and regression tests.
  • Aligns sign matching with ordinary numeric parsing.
File Description
Number.Formatting.cs Formats exponent signs using culture settings.
Number.Parsing.cs Applies positive-first culture-aware sign matching.
NumberStyles.cs Documents provider-dependent hex-float syntax.
DoubleTests.cs Adds double round-trip coverage.
SingleTests.cs Adds float round-trip coverage.
HalfTests.cs Adds Half round-trip coverage.

Document the provider-specific signs and decimal separator, and cover BFloat16 round-trips with custom signs.

Co-authored-by: Copilot App <[email protected]>

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

🔵 Needs a closer look

Positive-first parsing breaks round-tripping when a custom positive sign prefixes the negative sign.

Review effort: Balanced
Findings: None

Resolved since last review (2)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Disambiguate overlapping positive and negative sign prefixes

src/​libraries/​System.Private.CoreLib/​src/​System/​Number.Parsing.cs:1246

Checking PositiveSign first regresses round-tripping when it is a prefix of NegativeSign. For example, with PositiveSign = "-" and NegativeSign = "-+", formatting -3.0 now produces -+0x1.8p-1, but this branch consumes only - as a positive sign and then fails at + before the 0x; a negative exponent has the same problem. The reverse prefix direction is covered by the new tests, but this direction is not. Please disambiguate overlapping signs (for example, prefer the longer match while retaining positive preference for equal signs) and cover both prefix orientations.

tannergooding and others added 2 commits October 2, 2026 15:28
Prefer the longer custom sign during hex-float parsing so formatted values round-trip when one sign prefixes the other.

Co-authored-by: Copilot App <[email protected]>
Select the longer matching custom sign consistently across hexadecimal floats, decimal numbers, and integers, while preferring positive signs on ties.

Co-authored-by: Copilot App <[email protected]>
@tannergooding

Copy link
Copy Markdown
Member Author

Addressed in dde4478485d. Sign parsing now chooses the longer matching sign across hexadecimal floats, ordinary decimal parsing (including E exponents and trailing signs), and integers; equal-length matches continue to prefer PositiveSign. Added round-trip coverage for custom prefix-overlapping signs across double, float, Half, BFloat16, and Int32.

The checked CoreLib build passed. System.Runtime.Tests reported 78,557 total, 0 failed, and 87 skipped (78,470 passed).

Note

This response was generated with GitHub Copilot.

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

🟡 Changes recommended

UTF-8 parsing can select a different overlapping sign than UTF-16 when equivalent space characters have different encoded lengths.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)

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

🔵 Needs a closer look

The shared parsing change alters sign precedence across many public numeric APIs and warrants final compatibility review.

Review effort: Balanced
Findings: None

Resolved since last review (1)

@tannergooding

Copy link
Copy Markdown
Member Author

/ba-g unrelated failures

@tannergooding
tannergooding merged commit 4a6440c into dotnet:main Oct 6, 2026
141 of 143 checks passed
@tannergooding
tannergooding deleted the tannergooding-numeric-inconsistency branch October 6, 2026 14:57
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 8, 2026
tannergooding added a commit that referenced this pull request Oct 8, 2026
Fixes Issue #135039

main PR #135132

# Description

Backport the fix for overlapping custom signs in numeric parsing and
hexadecimal floating-point formatting. Parsing now prefers the longer
matching sign, with `PositiveSign` preferred when the signs have equal
length.

# Customer Impact

With custom signs that share a prefix, parsing could select the wrong
sign and produce an incorrect value or fail to round-trip formatted
text.

# Regression

No. Hexadecimal floating-point formatting and parsing are new in .NET
11; this fixes a bug in the new functionality rather than a regression
from an earlier release.

# Testing

- `dotnet build /t:test
src\libraries\System.Runtime\tests\System.Runtime.Tests\System.Runtime.Tests.csproj
--configuration Debug` — passed; 77,838 total, 0 failed, 87 skipped.
- `dotnet build /t:test
src\libraries\System.Runtime.Numerics\tests\System.Runtime.Numerics.Tests\System.Runtime.Numerics.Tests.csproj
--configuration Debug` — passed; 8,427 total, 0 failed, 0 skipped.

# Risk

Low. The change is limited to choosing between matching numeric signs,
and regression coverage exercises custom signs across numeric types and
parsing paths.

# Package authoring no longer needed in .NET 9

IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet
package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older
versions.

> [!NOTE]
> This PR description was generated with GitHub Copilot.

Co-authored-by: Copilot App <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 8, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hex float "X" format doesn't round-trip in built-in cultures with directional marks in their sign strings

3 participants