What should I know when using float / doubles between different machines?

I heard that there are many problems with float / doubles on different CPUs.

If I want to create a game that uses float for everything, how can I be sure that the float calculations are exactly the same on every machine, so that my simulation will look the same on every machine?

I am also concerned about writing / reading files or sending / receiving float values ​​on different computers. What transformations should be made, if any?

I need to be 100% sure that my float values ​​are calculated exactly the same, because even a small difference in the calculations will lead to a completely different future. Is it possible?

+4
source share
3 answers

The C ++ standard does not specify any details about floating point types other than range limits, and it is possible that some of the math functions (e.g., sine and exponential) must be correct to a certain level of accuracy.

Also, at this level of generality, you really can't rely on anything else!

However, it is entirely possible that you will not really need bidirectional calculations on each platform, and that the accuracy and precision of the float or double types will actually be sufficient for modeling purposes.

Note that you cannot even create a reliable algebraic expression result inside your own program when you change the order of evaluation of subexpressions, so the request for the required reproducibility may be a little unrealistic anyway. If you want real precision and floating point precision, you might be better off with an arbitrary precision library with the right rounding, like MPFR - but that seems unrealistic for the game.

Serializing floats is a completely different story, and you will need to have an idea of ​​the representations used by your target platforms. If IEEE 754 floats of 32 or 64 bit size were used on all platforms, you could simply exchange the binary representation directly (modulo endianness). If you have other platforms, you will have to come up with your own serialization scheme.

+2
source
+1
source

All Articles