Skip to content

Correct decimal conversions to Half and BFloat16 - #135668

Merged
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-investigate-issue-135659
Oct 13, 2026
Merged

tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-investigate-issue-135659

Conversation

@tannergooding

@tannergooding tannergooding commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Description

Decimal-to-Half and decimal-to-BFloat16 conversions can round to the wrong neighbor when the decimal lies just to one side of a target-format midpoint but first rounds to the midpoint as double. Use target-format fast paths where valid, with an exact target-precision fallback for the remaining values. Also correct Half's MinExponentRoundToEven boundary from -21 to -22; this fixes affected Half parsing ties as well.

Testing

  • .\build.cmd clr.corelib+clr.nativecorelib+libs.pretest -rc checked — passed, 0 warnings and 0 errors.
  • dotnet build /t:test src\libraries\System.Runtime\tests\System.Runtime.Tests\System.Runtime.Tests.csproj — passed, 78,595 tests, 0 failed, 87 skipped.
  • git diff --check — passed.

Performance

A focused local BFloat16 comparison for 123.456m measured 6.368 ns for the decimal cast versus 4.096 ns for (BFloat16)(double)value. The run had notable variance and was not a separate baseline-runtime build, so treat this as an observed cost to assess rather than a conclusive regression measurement.

Risk

The behavioral change is limited to decimal conversions to the 16-bit formats and Half parsing at the corrected tie boundary. Regression tests cover midpoint-adjacent values, ties, and subnormal cases for both formats.

Resolves #135659

Note

This PR description was prepared with GitHub Copilot.

Use target-format fast paths and exact fallback rounding to avoid double-rounding.

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.

@tannergooding
tannergooding merged commit e23621e into dotnet:main Oct 13, 2026
137 of 139 checks passed
@tannergooding
tannergooding deleted the tannergooding-investigate-issue-135659 branch October 13, 2026 22:31
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 14, 2026
tannergooding added a commit that referenced this pull request Oct 15, 2026
)

Fixes Issue #135659 <!-- Issue Number -->

main PR #135668 <!-- Link to PR if
any that fixed this in the main branch. -->

# Description

<!-- Give a brief summary of the issue and how the pull request is
fixing it. -->
This backports the fix for double-rounding in decimal-to-Half and
decimal-to-BFloat16 conversions. The exact fallback now rounds directly
to the destination format, and Half parsing uses the corrected
tie-to-even exponent boundary.

# Customer Impact

<!-- What is the impact to customers of not taking this fix? -->
Applications converting decimal values close to Half or BFloat16
midpoints can receive the adjacent representable value on the wrong side
of the midpoint. The error also affects some Half subnormal values.

# Regression

<!-- Is this fixing a problem that was introduced in the most recent
release, ie., fixing a regression? -->
No. The issue reproduces in .NET 11 RC builds; Half had the same
double-rounding issue in .NET 10, and BFloat16 is new in .NET 11.

# Testing

<!-- What kind of testing has been done with the fix. -->
- `.\build.cmd clr+libs -rc checked` — passed (checked baseline).
- `.\build.cmd clr.corelib+clr.nativecorelib+libs.pretest -rc checked` —
passed, 0 warnings and 0 errors.
- `dotnet build /t:test
src\libraries\System.Runtime\tests\System.Runtime.Tests\System.Runtime.Tests.csproj`
— passed: 77,859 total, 0 failed, 87 skipped.

# Risk

<!-- Please assess the risk of taking this fix. Provide details backing
up your assessment. -->
Low. The change is limited to decimal floating-point rounding and Half's
parser tie boundary, with regression cases for midpoint-adjacent values,
ties, and subnormals. The mainline PR's focused comparison reported
6.368 ns for a BFloat16 decimal cast versus 4.096 ns for casting through
double; the run had notable variance and was not a separate
baseline-runtime build, so this is an observed cost rather than
conclusive regression evidence. No separate release-branch benchmark was
run.

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

Resolves #135659

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

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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.

(Half)decimal and (BFloat16)decimal are not correctly rounded: double rounding through double

2 participants