Skip to content

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

Closed

Description

@sw23

Description

This bug was identified by comparing floating point number conversion results across a few open-source implementations. Converting a decimal to Half or BFloat16 sometimes gives the neighbor on the wrong side of a midpoint, because the value is rounded twice. Feel free to close if this is expected/intended.

Below is the full AI-generated summary:

#130566 made decimal → double/float correctly rounded and changed decimal → Half/BFloat16 to go through double instead of float, with this reasoning (Half.cs; BFloat16.cs has the same with 2 * 8 + 2):

// Round through double, not float: decimal -> double is correctly rounded and double (53-bit significand)
// -> Half is an innocuous double rounding (53 >= 2 * 11 + 2), so the result is correctly rounded. Going
// through float (24-bit significand) is not, because 24 only meets the 2 * 11 + 2 bound with no margin.
public static explicit operator Half(decimal value) => (Half)(double)value;

The p' >= 2p + 2 bound makes double rounding innocuous for +, -, *, / and sqrt of operands that are already p-bit numbers: their exact result can't sit within a p'-bit ulp of a p-bit midpoint without being on it. A decimal is not a p-bit number; it has up to 96 significant bits. A decimal within half a double ulp of a Half (or BFloat16) midpoint is rounded to exactly that midpoint by the first step, and the second step then breaks the "tie" to even, which is the wrong side half of the time. For example 1.0004882812500000000000000001m is above the midpoint 1 + 2^-11 of 0x3C00 and 0x3C01, (double) of it is exactly 1.00048828125, and (Half) of that is 0x3C00. Half.Parse of the same digits rounds once and gives 0x3C01. Near the bottom of the range this turns a positive value above Half.Epsilon / 2 into zero.

Scope: for every Half midpoint (normal and subnormal), the decimal one unit in the 28th place above it and the one below it: 15,872 of 31,744 wrong each way, exactly the ones on the side away from the even neighbour. BFloat16: 7,424 of 14,848 each way (the midpoints a decimal can hold to 28 places). The midpoints written to 17 significant digits: 2,041 Half and 4,121 BFloat16 wrong. (double)decimal and (float)decimal themselves are correct.

Suggested fix: round the first step to odd, as #130565 does for BigInteger ("A round-to-odd intermediate is always innocuous when re-rounded to a narrower type"): OR the sticky bit (any nonzero remainder) into the last bit of the 53-bit result in DecimalToFloatingPointExact, and make the Clinger and Eisel-Lemire fast paths in DecimalToFloatingPoint do the same (or skip them for Half/BFloat16). Modelled over all the inputs above, round-to-odd to 53 bits followed by the existing (Half)/(BFloat16) narrowing gives 0 wrong.

Use the following links to see the expected conversion results:

Reproduction Steps

Save as repro.cs and run dotnet run repro.cs with the .NET 11 SDK:

using System.Globalization;
using System.Numerics;
using System.Runtime.CompilerServices;

var inv = CultureInfo.InvariantCulture;
decimal[] values = { 1.0004882812500000000000000001m, 1.0014648437499999999999999999m, 0.0000000298023223876953125001m };
foreach (decimal m in values)
    Console.WriteLine($"(Half){m}m = 0x{BitConverter.HalfToUInt16Bits((Half)m):X4}   Half.Parse = 0x{BitConverter.HalfToUInt16Bits(Half.Parse(m.ToString(inv), inv)):X4}   (double)m = {(double)m:R}");

decimal[] bvalues = { 1.0039062500000000000000000001m, 3.0234374999999999999999999999m };
foreach (decimal m in bvalues)
    Console.WriteLine($"(BFloat16){m}m = 0x{Unsafe.BitCast<BFloat16, ushort>((BFloat16)m):X4}   BFloat16.Parse = 0x{Unsafe.BitCast<BFloat16, ushort>(BFloat16.Parse(m.ToString(inv), inv)):X4}   (double)m = {(double)m:R}");

Expected behavior

Each decimal is a hair above or below a Half or BFloat16 midpoint and should round to the nearer neighbor, as Half.Parse/BFloat16.Parse of the same digits do: 0x3C01, 0x3C01, 0x0001, 0x3F81, 0x4041.

Actual behavior

.NET 11.0.0-rc.1.26425.128, linux-arm64; (double)m shows the first rounding landing exactly on the midpoint:

(Half)1.0014648437499999999999999999m = 0x3C02   Half.Parse = 0x3C01   (double)m = 1.00146484375
(Half)0.0000000298023223876953125001m = 0x0000   Half.Parse = 0x0001   (double)m = 2.9802322387695312E-08
(BFloat16)1.0039062500000000000000000001m = 0x3F80   BFloat16.Parse = 0x3F81   (double)m = 1.00390625
(BFloat16)3.0234374999999999999999999999m = 0x4042   BFloat16.Parse = 0x4041   (double)m = 3.0234375

Regression?

No. In .NET 10 these operators went through float ((Half)(float)value) and give the same wrong Half results for these inputs (BFloat16 is new in .NET 11). #130566 meant to fix this and fixed it for double and float but not for the 16-bit types.

Known Workarounds

Half.Parse(value.ToString(CultureInfo.InvariantCulture), CultureInfo.InvariantCulture) (and the same for BFloat16) rounds once and is correct, since a decimal always prints exactly (apart from exact Half ties in [2⁻¹¹, 2⁻¹⁰), which Half.Parse currently rounds up).

Configuration

.NET 11.0.0-rc.1.26425.128 and 11.0.0-rc.2.26473.112, linux-arm64 (Docker mcr.microsoft.com/dotnet/sdk:11.0 and nightly/sdk:11.0). Not architecture-specific: managed conversion code.

Other information

No response

Activity

  1. added
    untriaged
    on Oct 13, 2026
  2. dotnet-policy-service commented on Oct 13, 2026

    @dotnet-policy-service
    Contributor

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

  3. added a commit that references this issue on Oct 13, 2026
    e23621e
  4. removed
    untriaged
    on Oct 13, 2026
  5. locked and limited conversation to collaborators on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions