为什么在相同的输入、相同的硬件下,Math.Exp 在 32 位和 64 位之间给出不同的结果

发布于 2024-09-29 04:49:15 字数 1889 浏览 0 评论 0原文

我正在将 .NET 2.0 与 PlatformTarget x64 和 x86 一起使用。我给 Math.Exp 提供相同的输入数字,但它在任一平台上返回不同的结果。

MSDN 说你不能依赖文字/解析的 Double 来表示平台之间的相同数字,但我认为下面使用 Int64BitsToDouble 可以避免这个问题,并保证两个平台上 Math.Exp 的输入相同。

我的问题是为什么结果不同?我本以为:

  • 输入以相同的方式存储(双精度/64位精度),
  • 无论处理器的位数如何,FPU都会执行相同的计算
  • 输出以相同的方式存储

我知道我不应该比较浮点一般来说,第 15/17 位数字之后的数字,但我对这里的不一致感到困惑,因为看起来在相同的硬件上执行相同的操作。

有人知道幕后发生了什么吗?

double d = BitConverter.Int64BitsToDouble(-4648784593573222648L); // same as Double.Parse("-0.0068846153846153849") but with no concern about losing digits in conversion
Debug.Assert(d.ToString("G17") == "-0.0068846153846153849"
    && BitConverter.DoubleToInt64Bits(d) == -4648784593573222648L); // true on both 32 & 64 bit

double exp = Math.Exp(d);

Console.WriteLine("{0:G17} = {1}", exp, BitConverter.DoubleToInt64Bits(exp));
// 64-bit: 0.99313902928727449 = 4607120620669726947
// 32-bit: 0.9931390292872746  = 4607120620669726948

打开或关闭 JIT 的结果在两个平台上都是一致的。

[编辑]

我对下面的答案并不完全满意,因此这里有一些来自我搜索的更多详细信息。

http://www.manicai.net/comp/debugging/fpudiff/ 说:

所以32位使用80位FPU寄存器,64位使用128位SSE寄存器。

CLI 标准表示,如果硬件支持,则可以以更高的精度表示双精度数:

[基本原理:此设计允许 CLI 选择特定于平台的高性能表示 浮点数,直到它们被放入存储位置。例如,它可能可以离开 硬件寄存器中的浮点变量提供比用户要求的更高的精度。在 分区 I 69 同时,CIL 生成器可以通过以下方式强制操作遵守特定于语言的表示规则: 使用转换指令。结束理由]

http://www.ecma-international。 org/publications/files/ECMA-ST/Ecma-335.pdf(12.1.3 浮点数据类型的处理)

我认为这就是这里发生的情况,因为在 Double 的标准 15 位数字之后结果不同精确。 64 位 Math.Exp 结果更精确(它有一个额外的数字),因为 64 位 .NET 内部使用的 FPU 寄存器比 32 位 .NET 使用的 FPU 寄存器精度更高。

I am using .NET 2.0 with PlatformTarget x64 and x86. I am giving Math.Exp the same input number, and it returns different results in either platform.

MSDN says you can't rely on a literal/parsed Double to represent the same number between platforms, but I think my use of Int64BitsToDouble below avoids this problem and guarantees the same input to Math.Exp on both platforms.

My question is why are the results different? I would have thought that:

  • the input is stored in the same way (double/64-bit precision)
  • the FPU would do the same calculations regardless of processor's bitness
  • the output is stored in the same way

I know I should not compare floating-point numbers after the 15/17th digit in general, but I am confused about the inconsistency here with what looks like the same operation on the same hardware.

Any one know what's going on under the hood?

double d = BitConverter.Int64BitsToDouble(-4648784593573222648L); // same as Double.Parse("-0.0068846153846153849") but with no concern about losing digits in conversion
Debug.Assert(d.ToString("G17") == "-0.0068846153846153849"
    && BitConverter.DoubleToInt64Bits(d) == -4648784593573222648L); // true on both 32 & 64 bit

double exp = Math.Exp(d);

Console.WriteLine("{0:G17} = {1}", exp, BitConverter.DoubleToInt64Bits(exp));
// 64-bit: 0.99313902928727449 = 4607120620669726947
// 32-bit: 0.9931390292872746  = 4607120620669726948

The results are consistent on both platforms with JIT turned on or off.

[Edit]

I'm not completely satisfied with the answers below so here are some more details from my searching.

http://www.manicai.net/comp/debugging/fpudiff/ says that:

So 32-bit is using the 80-bit FPU registers, 64-bit is using the 128-bit SSE registers.

And the CLI Standard says that doubles can be represented with higher precision if the hardware supports it:

[Rationale: This design allows the CLI to choose a platform-specific high-performance representation for
floating-point numbers until they are placed in storage locations. For example, it might be able to leave
floating-point variables in hardware registers that provide more precision than a user has requested. At the
Partition I 69
same time, CIL generators can force operations to respect language-specific rules for representations through
the use of conversion instructions. end rationale]

http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-335.pdf (12.1.3 Handling of floating-point data types)

I think this is what is happening here, because the results different after Double's standard 15 digits of precision. The 64-bit Math.Exp result is more precise (it has an extra digit) because internally 64-bit .NET is using an FPU register with more precision than the FPU register used by 32-bit .NET.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见你 2024-10-06 04:49:15

是的,舍入错误,并且它实际上不是相同的硬件。 32 位版本针对不同的指令集和寄存器大小。

Yes rounding errors, and it is effectively NOT the same hardware. The 32 bit version is targeting a different set of instructions and register sizes.

丑疤怪 2024-10-06 04:49:15

使用 Double 类型时,您会遇到舍入错误,因为二进制中的分数很快就会变得非常大。如果您使用 Decimal 类型可能会有所帮助。

With the Double type you will get rounding errors, as fractions in binary get very large very quickly. It would possibly help if you used the Decimal type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文