admin管理员组文章数量:1391944
Compile
if ((int)MathF.Floor(float.PositiveInfinity) > 0)
Console.WriteLine("This is .NET 9.0");
else
Console.WriteLine("This is .NET 8.0");
using either of these project files:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
or
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
On my computer, the program will correctly identify which project file you used, regardless of whether you compile it in debug or release mode.
The Watch window in VS 2022 (17.13.3) reports that (int)MathF.Floor(float.PositiveInfinity)
is -2147483648
regardless of which runtime was targeted, but despite that, the > 0
branch is taken on .NET 9.
Dumping the two Release DLLs in ildasm shows no meaningful differences.
Environment.Version
is 8.0.14 or 9.0.3.
Edits after the question was answered:
Yes, my computer is x86-64.
The VS Watch window does not (yet?) perform saturating conversions when debugging a .NET 9 application.
Compile
if ((int)MathF.Floor(float.PositiveInfinity) > 0)
Console.WriteLine("This is .NET 9.0");
else
Console.WriteLine("This is .NET 8.0");
using either of these project files:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
or
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
On my computer, the program will correctly identify which project file you used, regardless of whether you compile it in debug or release mode.
The Watch window in VS 2022 (17.13.3) reports that (int)MathF.Floor(float.PositiveInfinity)
is -2147483648
regardless of which runtime was targeted, but despite that, the > 0
branch is taken on .NET 9.
Dumping the two Release DLLs in ildasm shows no meaningful differences.
Environment.Version
is 8.0.14 or 9.0.3.
Edits after the question was answered:
Yes, my computer is x86-64.
The VS Watch window does not (yet?) perform saturating conversions when debugging a .NET 9 application.
1 Answer
Reset to default 25This is related to this change, where all the floating-point-to-integer conversions now have saturating behaviour on x86/x64 machines.
Before the change, the result of converting a floating point number to int
will always be int.MinValue
as long as the value is outside of the range of int
. After the change, the result will be int.MaxValue
when the value is larger than int.MaxValue
, and int.MinValue
when the value is smaller than int.MinValue
.
The reason for this change is
This change was made to standardize all floating point-to-integer conversions to have saturating behavior and to make the behavior deterministic.
Case in point: I cannot actually reproduce the behaviour in your code on an Apple M4 MacBook.
See also a similar issue reported on the GitHub repo.
本文标签: cIs this value conversion behavior change an expected change between NET 8 and NET 9Stack Overflow
版权声明:本文标题:c# - Is this value conversion behavior change an expected change between .NET 8 and .NET 9? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744715111a2621355.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
(int)MathF.Floor(float.PositiveInfinity)
to be 2147483647 not -2147483648 as you have stated above – phuzi Commented Mar 13 at 9:08float.PositiveInfinity
? Is there a float operation that wasn't checked for overflow until after the cast? – Panagiotis Kanavos Commented Mar 13 at 9:21