Skip to content

Instantly share code, notes, and snippets.

@ufcpp
Created November 13, 2024 10:16
Show Gist options
  • Save ufcpp/6af1e3e55d4821cb300c3aa2d42c086a to your computer and use it in GitHub Desktop.
Save ufcpp/6af1e3e55d4821cb300c3aa2d42c086a to your computer and use it in GitHub Desktop.
(int)Math.Floor(float.MaxValue) の挙動違うらしい…
// (int)Math.Floor(float.MaxValue) は、
// .NET 8 だとなぜか int.MinValue
// .NET 9 だと int.MaxValue になってそう。
// .NET 9 でだけ永久ループ…
M((int)Math.Floor(float.MaxValue), (int)Math.Floor(float.MaxValue));
Console.WriteLine("done.");
static void M(int x, int y)
{
// y が int.MaxValue だと、i++ がオーバーフローして永久ループになる。
for (int i = x; i <= y; i++) ;
}
@ufcpp
Copy link
Author

ufcpp commented Nov 13, 2024

↓だけでも再現。

var x = int.MaxValue;
var y = (float)x;
var z = (int)y;
Console.WriteLine(z);

float → int の変換のバグかな。

@ufcpp
Copy link
Author

ufcpp commented Nov 13, 2024

https://learn.microsoft.com/ja-jp/dotnet/core/compatibility/jit/9.0/fp-to-integer

バグ修正ってわけではなく、「.NET 8 以前はそういう仕様」らしい…

浮動小数点数から整数への変換にAVX命令使いたいというのが目的の破壊的変更らしい。
runtime 97529

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