确定性浮点和.NET [英] Deterministic floating point and .NET
问题描述
如何保证.NET应用程序(例如C#)中的浮点计算始终产生相同的位精确结果?特别是在使用不同版本的.NET并在不同平台(x86与x86_64)上运行时.浮点运算的错误并不重要.
How can I guarantee that floating point calculations in a .NET application (say in C#) always produce the same bit-exact result? Especially when using different versions of .NET and running on different platforms (x86 vs x86_64). Inaccuracies of floating point operations do not matter.
在Java中,我将使用strictfp.在C/C ++和其他低级语言中,此问题基本上是通过访问FPU/SSE控制寄存器来解决的,但在.NET中可能是不可能的.
In Java I'd use strictfp. In C/C++ and other low level languages this problem is essentially solved by accessing the FPU / SSE control registers but that's probably not possible in .NET.
即使通过FPU控制寄存器的控制,.NET的JIT也会在不同的平台上生成不同的代码.在这种情况下,像HotSpot之类的东西甚至会更糟...
Even with control of the FPU control register the JIT of .NET will generate different code on different platforms. Something like HotSpot would be even worse in this case...
我为什么需要它?我正在考虑编写一个实时策略(RTS)游戏,该游戏在很大程度上取决于快速浮点数学运算以及锁定步进仿真.本质上,我只会在网络上传输用户输入.这也适用于通过存储用户输入来实现重播的其他游戏.
Why do I need it? I'm thinking about writing a real-time strategy (RTS) game which heavily depends on fast floating point math together with a lock stepped simulation. Essentially I will only transmit user input across the network. This also applies to other games which implement replays by storing the user input.
不能选择的是:
- 小数(太慢)
- 定点值(使用sqrt,sin,cos,tan,atan ...时太慢且麻烦)
- 像FPS一样通过网络更新状态:不能发送数百或数千个位置信息
有什么想法吗?
推荐答案
我不确定您的问题的确切答案,但是您可以使用C ++并在c ++ dll中完成所有float运算,然后将结果返回到.net通过互调.
I'm not sure of the exact answer for your question but you could use C++ and do all your float work in a c++ dll and then return the result to .Net through an interopt.
这篇关于确定性浮点和.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!