Describe the bug
Casting a double floating-point literal to decimal in a compile-time constant expression
produces an incorrect, overly-precise decimal value when compiled with the C# compiler
(Roslyn 5.10.0) bundled in the .NET 11 Preview 7 SDK. The same expression compiles correctly
with the Roslyn 5.9.0 compiler bundled in the .NET 10 GA SDK.
This is a compile-time issue, not a runtime one. With both the .NET 10 GA SDK and the .NET 11
Preview SDK installed side by side:
| TargetFramework |
Active SDK (compiler) |
Runtime that executes it |
Result |
| net11.0 |
11.0.100-preview.7 (unpinned) |
.NET 11 preview |
wrong (123.45669999999999788542481838) |
| net10.0 |
11.0.100-preview.7 (unpinned, still resolves highest) |
.NET 10.0.1 |
wrong (123.45669999999999788542481838) |
| net10.0 |
10.0.400 (pinned via global.json) |
.NET 10.0.1 |
correct (123.4567) |
Only pinning the compiling SDK via global.json fixes the output. Changing only the
TargetFramework (while the .NET 11 preview SDK remains the highest-installed/active SDK)
has no effect — confirming the miscompilation happens at the (decimal) cast's constant-folding
step in the compiler, before the TFM-specific reference assemblies or runtime are ever involved.
Steps to reproduce
Minimal repro
decimal fromCast = (decimal) 123.4567; // 123.4567 is a double literal
decimal fromLiteral = 123.4567m; // true decimal literal, for comparison
Console.WriteLine(fromCast); // expected: 123.4567
Console.WriteLine(fromLiteral); // 123.4567
Console.WriteLine(decimal.GetBits(fromCast).SequenceEqual(decimal.GetBits(fromLiteral)));
Expected behavior
Actual output (compiled with .NET 11.0.100-preview.7.26381.103, Roslyn 5.10.0-1.26381.103):
123.45669999999999788542481838
123.4567
False
Actual behavior
Expected output (and actual output when compiled with .NET 10.0.400 GA, Roslyn 5.9.0-1.26379.115):
123.4567
123.4567
True
Is this a regression?
Yes, it worked in .NET 10.x
Are there any workarounds?
Specify decimal literals as decimal literals (e.g. 123.4567m) instead of double literals (123.4567)
dotnet --info output
.NET SDK:
Version: 11.0.100-preview.7.26381.103
Commit: e2c1e00b3d
Workload version: 11.0.100-manifests.2ce71a7f
MSBuild version: 18.10.0-1.26381.103+e2c1e00b3
Runtime Environment:
OS Name: Windows
OS Version: 10.0.26200
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\11.0.100-preview.7.26381.103
.NET workloads installed:
There are no installed workloads to display.
Configured to use workload sets when installing new manifests.
No workload sets are installed. Run "dotnet workload restore" to install a workload set.
Host:
Version: 11.0.0-preview.7.26381.103
Architecture: x64
Commit: e2c1e00b3d
.NET SDKs installed:
10.0.400 [C:\Program Files\dotnet\sdk]
11.0.100-preview.7.26381.103 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 10.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 11.0.0-preview.7.26381.103 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.19 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 11.0.0-preview.7.26381.103 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.30 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.19 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 10.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 11.0.0-preview.7.26381.103 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
IDE version
No response
Other details
No response
Describe the bug
Casting a
doublefloating-point literal todecimalin a compile-time constant expressionproduces an incorrect, overly-precise
decimalvalue when compiled with the C# compiler(Roslyn 5.10.0) bundled in the .NET 11 Preview 7 SDK. The same expression compiles correctly
with the Roslyn 5.9.0 compiler bundled in the .NET 10 GA SDK.
This is a compile-time issue, not a runtime one. With both the .NET 10 GA SDK and the .NET 11
Preview SDK installed side by side:
Only pinning the compiling SDK via global.json fixes the output. Changing only the
TargetFramework (while the .NET 11 preview SDK remains the highest-installed/active SDK)
has no effect — confirming the miscompilation happens at the (decimal) cast's constant-folding
step in the compiler, before the TFM-specific reference assemblies or runtime are ever involved.
Steps to reproduce
Minimal repro
Expected behavior
Actual output (compiled with .NET 11.0.100-preview.7.26381.103, Roslyn 5.10.0-1.26381.103):
123.45669999999999788542481838
123.4567
False
Actual behavior
Expected output (and actual output when compiled with .NET 10.0.400 GA, Roslyn 5.9.0-1.26379.115):
123.4567
123.4567
True
Is this a regression?
Yes, it worked in .NET 10.x
Are there any workarounds?
Specify decimal literals as decimal literals (e.g. 123.4567m) instead of double literals (123.4567)
dotnet --info output
IDE version
No response
Other details
No response