Skip to content

decimal % returns a wrong remainder for some divisors with more than 64 significant bits (and throws OverflowException on x64 since .NET 10) #137248

Description

@slang25

Description

For some operand pairs decimal % decimal does not produce the exact remainder, although that remainder is representable:

  • .NET Core 3.1 to .NET 9 on x64 (tested 3.1, 6.0, 8.0, 9.0), and ARM64 on every version tested: a wrong value is returned silently.
  • .NET 10 and 11 on x64: OverflowException.

The same inputs fail in both forms. A divisor that differs only in trailing zeros (1m against 1.0000000000000000000000000000m) is enough to go from correct to failing.

Found by differential fuzzing of decimal arithmetic against a BigInteger model.

Reproduction Steps

Console.WriteLine(Rem(4294967295m, 1m));                                    // 0 (correct everywhere)
Console.WriteLine(Rem(4294967295m, 1.0000000000000000000000000000m));       // exact: 0.0000000000000000000000000000
Console.WriteLine(Rem(4294967296m, 1m / 7m));                               // exact: 0.1428571428571428558543669541
Console.WriteLine(Rem(decimal.MaxValue, 1.0000000000000000000000000000m));  // exact: 0.0000000000000000000000000000

// Operands are passed through a method so that the C# compiler does not constant-fold the %.
static string Rem(decimal a, decimal b)
{
    try { return (a % b).ToString(); }
    catch (Exception e) { return e.GetType().Name; }
}

Written with two literals (4294967295m % 1.0000000000000000000000000000m), the expression is folded by the C# compiler, which then hits the same bug: SDK 11.0.100-rc.3 on x64 reports error CS0463: Evaluation of the decimal constant expression failed.

Expected behavior

The exact remainders shown in the comments.

Actual behavior

Columns are lines 2 to 4 of the repro.

Runtime 4294967295m % 1.000…0m 4294967296m % (1m / 7m) decimal.MaxValue % 1.000…0m
x64: .NET 10.0.9, 10.0.14, 11.0.0-rc.1, 11.0.0-rc.3 OverflowException OverflowException OverflowException
x64: .NET Core 3.1.32, .NET 6.0.36, 8.0.31, 9.0.19 0.9232812099469197934124335104 0.0625361661149233508289347584 0.2774645161159622109968728064
ARM64: .NET 8.0.33, 10.0.14, 11.0.0-rc.3 same wrong values same wrong values same wrong values
x64: .NET 10.0.14 with DOTNET_EnableHWIntrinsic=0 same wrong values same wrong values same wrong values
System.OverflowException: Arithmetic operation resulted in an overflow.
   at System.Decimal.DecCalc.Div128By96(Buf16& bufNum, Buf12& bufDen)
   at System.Decimal.DecCalc.VarDecModFull(DecCalc& d1, DecCalc& d2, Int32 scale)
   at System.Decimal.DecCalc.VarDecMod(DecCalc& d1, DecCalc& d2)
   at System.Decimal.op_Modulus(Decimal d1, Decimal d2)

Regression?

Yes, from .NET Core 2.1, which returns 0.0000000000000000000, 0.1428571428571428559 and 0 for the three inputs: right to the digits the old algorithm kept. The wrong results are present in 3.1 (3.0 was not tested); VarDecModFull was added in 2018 by dotnet/coreclr#20305. The exception on x64 starts with .NET 10: #99212 routed Div64By32 through X86Base.DivRem, so the overflowing divide now faults instead of truncating. .NET Framework was not tested.

Known Workarounds

None that is general. If the divisor only carries trailing zeros, removing them first can avoid the failing path (compare lines 1 and 2 of the repro). That does not help for divisors such as 1m / 7m.

Configuration

  • x64: Ubuntu 26.04, runtimes as listed above (2.1, 3.1 and 6.0 in the official mcr.microsoft.com/dotnet runtime images).
  • ARM64: official mcr.microsoft.com/dotnet/runtime images run under QEMU 10.2.1 user-mode emulation, not on ARM64 hardware.
  • Not specific to OS. The failure mode depends on whether X86Base.DivRem is used.

Other information

Cause. Links are pinned to main at 6a8c4ed.

VarDecModFull does base-2^32 long division of the scaled-up dividend (up to 7 uints) by the normalised 96-bit divisor, calling Div128By96 once per 4-uint window.

Div128By96 requires bufDen.U2 > bufNum.U3. Its doc comment justifies that for VarDecDiv, where the remainder is multiplied by at most 10^9 between calls. In VarDecModFull the window moves down one uint, so the previous remainder is in effect multiplied by 2^32, and the only guarantee is remainder < divisor, that is bufNum.U3 <= bufDen.U2. When they are equal, High64 / den is 2^32 or more and does not fit the 32-bit quotient that Div64By32 returns:

  • with X86Base.DivRem, the div instruction faults, which surfaces as OverflowException;
  • otherwise (uint)(dividend / den) truncates, and the remainder comes out wrong.

For decimal.MaxValue % 1.0000000000000000000000000000m: the shift is 2, bufDivisor is 813F3978 F8940984 40000000, and the dividend scaled by 10^28 is (buf[6] to buf[0])

00000000 813F3978 F8940984 3FFFFFFF 7EC0C687 076BF67B C0000000

The window at index 3 gives quotient 0 and leaves the remainder 813F3978 F8940984 3FFFFFFF (divisor minus 1). The window at index 2 then has U3 = 813F3978 = bufDen.U2, and High64 / den is 0x1_0000_0001. The true quotient digit is 0xFFFFFFFF.

Possible fix. Div96By64 already handles this case on its non-intrinsic path ("Divide would overflow. Assume a quotient of 2^32", L399-L419). Div128By96 has no equivalent. Adding the same guard for bufNum.U3 == bufDen.U2 (cap the quotient estimate and add the divisor back, as in Knuth's algorithm D step D3) and relaxing the assert to >= should fix both forms.

How often. All figures use the released runtimes above and a BigInteger oracle.

  • 3,000,000 pairs from a mix biased towards edge values (mantissas near decimal.MaxValue, powers of ten, random bit lengths, scales 0, 28 and random): 1,375 failures (0.046%). The failing set is identical on .NET 8.0.31 (wrong value), 10.0.14 and 11.0.0-rc.3 (exception).
  • 3,000,000 pairs with uniformly random bit lengths (0 to 96) and scales (0 to 28): no failures.
  • In all 1,375 failures the integer quotient floor(|a| / |b|) has a 32-bit limb equal to 0xFFFFFFFF.

Measured and inferred.

  • Measured: every result in the table and the frequency figures. A word-level simulation of the truncating path reproduces the wrong values exactly (0.2774645161159622109968728064 and 0.9232812099469197934124335104).
  • Inferred from the source, not run: main at the pinned commit is affected (it has the same Div128By96 as release/10.0 and release/11.0 and an equivalent sequence of calls, but was not built); a Checked build would hit the Debug.Assert in Div128By96 on these inputs; other targets without X86Base (ARM32, WASM and so on) behave like ARM64.
  • Derived from the source: failure needs a divisor whose mantissa, after scale alignment, is 2^64 or more, and a 32-bit limb of the integer quotient equal to 0xFFFFFFFE or 0xFFFFFFFF. The quotient is therefore at least 2^32 - 2, so small everyday values do not reach it.

I found no existing issue or PR for this (searched for Div128By96, VarDecModFull, and decimal remainder/modulo overflow and wrong-result reports). #8472 is the earlier Remainder issue that dotnet/coreclr#20305 fixed.

Activity

  1. added
    untriaged
    on Nov 10, 2026
  2. dotnet-policy-service commented on Nov 10, 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Numericsuntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions