Are floating point operations in Delphi deterministic?

喜欢而已 提交于 2019-12-23 08:56:38

问题


Are floating point operations in Delphi deterministic?

I.E. will I get the same result from an identical floating point mathematical operation on the same executable compiled with Delphi Win32 compiler as I would with the Win64 compiler, or the OS X compiler, or the iOS compiler, or the Android compiler?

This is a crucial question as I'm implementing multiplayer support in my game engine, and I'm concerned that the predictive results on the client side could very often differ from the definitive (and authoritative) determination of the server.

The consequence of this would be the appearance of "lag" or "jerkiness" on the client side when the authoritative game state data overrules the predictive state on the client side.

Since I don't realistically have the ability to test dozens of different devices on different platforms compiled with the different compilers in anything resembling a "controlled condition", I figure it's best to put this question to the Delphi developers out there to see if anyone has an internal understanding of floating point determinism at a low-level on the compilers.


回答1:


I think there is no simple answer. Similar task was discussed here. In general, there are two standards for presentation of floating point numbers: IEEE 754-1985 and EEE 754-2008. All modern (and quite old actually) CPUs follow the standards and it guarantees some things:

  • Binary presentation of same standard floating type will be equal
  • Result of some operations (not all, only basic operations!) is guaranteed to be equal, but only if compiler will use same type of the command, i am not sure it is true.

But if you use some extended operations, such as square root, result may vary even for different models of desktop CPUs. You can read this article for some details: http://randomascii.wordpress.com/2013/07/16/floating-point-determinism/

P.S. As tmyklebu mentioned, square root is also defined by IEEE 754, so it is possible to guarantee same result for same input for Add, Subtract, Multiply, Divide and Square root. Few other operations are also defined by IEEE, but for all details it is better to read IEEE.




回答2:


Putting aside the standards for floating point calculations for a moment, consider that the 32 and 64 bit compilers compile to use the old FPU vs the newer SSE instructions. I would find it difficult to trust that every calculation would always come out exactly the same on different hardware implementations. Better go the safe route and assume that if its within a small delta pf difference, you evaluate as equal.




回答3:


From experience I can tell that the results are different: 32-bit works with Extended precision by default, while 64-bit works with double precision by default.

Consider the statement

x,y,z: double; x := y * z;

in Win32 this will execute as "x := double(Extended(y)*Extended(z)); in Win64 this will execute as "x := double(double(y)*double(z));

you put a lot of effort into ensuring that you use the same precision and mode. However whenever you call 3rd party libraries, you need to consider that they may internally change these flags.



来源:https://stackoverflow.com/questions/24352053/are-floating-point-operations-in-delphi-deterministic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!