C# 中的浮点数学是否一致?是真的吗? [英] Is floating-point math consistent in C#? Can it be?

查看:20
本文介绍了C# 中的浮点数学是否一致?是真的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不,这不是另一个为什么是 (1/3.0)*3 != 1" 问题.

No, this is not another "Why is (1/3.0)*3 != 1" question.

我最近阅读了很多关于浮点的文章;具体来说,相同的计算可能如何在不同的架构或优化设置下给出不同的结果.

I've been reading about floating-points a lot lately; specifically, how the same calculation might give different results on different architectures or optimization settings.

这是存储重播或点对点联网(与服务器-客户端相反),它依赖于所有客户端在每次运行程序时生成完全相同的结果——一个浮点计算中的小差异可能导致不同机器上的游戏状态截然不同(甚至在同一台机器上!)

This is a problem for video games which store replays, or are peer-to-peer networked (as opposed to server-client), which rely on all clients generating exactly the same results every time they run the program - a small discrepancy in one floating-point calculation can lead to a drastically different game-state on different machines (or even on the same machine!)

即使在遵循"IEEE-754的处理器中也会发生这种情况,主要是因为一些处理器(即 x86)使用 双扩展精度.也就是说,它们使用 80 位寄存器进行所有计算,然后截断为 64 位或 32 位,从而导致舍入结果与使用 64 位或 32 位进行计算的机器不同.

This happens even amongst processors that "follow" IEEE-754, primarily because some processors (namely x86) use double extended precision. That is, they use 80-bit registers to do all the calculations, then truncate to 64- or 32-bits, leading to different rounding results than machines which use 64- or 32- bits for the calculations.

我在网上看到了几个解决这个问题的方法,但都是针对 C++,而不是 C#:

I've seen several solutions to this problem online, but all for C++, not C#:

  • 使用 _controlfp_s (Windows)、_FPU_SETCW (Linux?) 或 fpsetprec (BSD).
  • 始终使用相同的优化设置运行相同的编译器,并要求所有用户具有相同的 CPU 架构(无跨平台播放).因为我的编译器"实际上是 JIT,它每次运行程序时可能会以不同的方式优化,我认为这是不可能的.
  • 使用定点算法,避免floatdouble.decimal 可以用于此目的,但速度会慢得多,而且 System.Math 库函数都不支持它.
  • Disable double extended-precision mode (so that all double calculations use IEEE-754 64-bits) using _controlfp_s (Windows), _FPU_SETCW (Linux?), or fpsetprec (BSD).
  • Always run the same compiler with the same optimization settings, and require all users to have the same CPU architecture (no cross-platform play). Because my "compiler" is actually the JIT, which may optimize differently every time the program is run, I don't think this is possible.
  • Use fixed-point arithmetic, and avoid float and double altogether. decimal would work for this purpose, but would be much slower, and none of the System.Math library functions support it.

那么,这在 C# 中是一个问题吗?如果我只打算支持 Windows(而不是 Mono)怎么办?

So, is this even a problem in C#? What if I only intend to support Windows (not Mono)?

如果是,有没有办法强制我的程序以正常的双精度运行?

如果没有,是否有任何库可以帮助保持浮点计算的一致性?

If not, are there any libraries that would help keep floating-point calculations consistent?

推荐答案

我知道没有办法在 .net 中使普通浮点具有确定性.允许 JITter 创建在不同平台(或不同版本的 .net 之间)表现不同的代码.因此,在确定性 .net 代码中使用普通 float 是不可能的.

I know of no way to way to make normal floating points deterministic in .net. The JITter is allowed to create code that behaves differently on different platforms(or between different versions of .net). So using normal floats in deterministic .net code is not possible.

我考虑的解决方法:

  1. 在 C# 中实现 FixedPoint32.虽然这并不太难(我已经完成了一半),但非常小的值范围使其使用起来很烦人.你必须时刻小心,这样既不会溢出,也不会失去太多的精度.最后我发现这并不比直接使用整数容易.
  2. 在 C# 中实现 FixedPoint64.我发现这很难做到.对于某些操作,128 位的中间整数会很有用.但是 .net 不提供这种类型.
  3. 实现自定义 32 位浮点.在实现这一点时,缺少 BitScanReverse 内在函数会导致一些烦恼.但目前我认为这是最有希望的路径.
  4. 使用本机代码进行数学运算.在每个数学运算中都会产生委托调用的开销.

我刚刚开始了 32 位浮点数学的软件实现.它可以在我的 2.66GHz i3 上每秒执行大约 7000 万次加法/乘法.https://github.com/CodesInChaos/SoftFloat.显然它仍然非常不完整和错误.

I've just started a software implementation of 32 bit floating point math. It can do about 70million additions/multiplications per second on my 2.66GHz i3. https://github.com/CodesInChaos/SoftFloat . Obviously it's still very incomplete and buggy.

这篇关于C# 中的浮点数学是否一致?是真的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆