Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are floating point operations in Delphi deterministic?

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.

like image 390
LaKraven Avatar asked Jun 22 '14 14:06

LaKraven


3 Answers

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.

like image 135
Andrei Galatyn Avatar answered Nov 17 '22 17:11

Andrei Galatyn


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.

like image 24
NexusDB Expert Avatar answered Nov 17 '22 15:11

NexusDB Expert


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.

like image 38
Daniel Avatar answered Nov 17 '22 16:11

Daniel