Can I assume that Flash and PHP will generate the same floating point numbers?
In a multiplayer game I'm developing, we have multiple values ββthat are floating point numbers. The backend (in PHP) and the front-end (in Flash) sometimes do the same calculations on these numbers to minimize communication.
Currently I'm pretty sure both sides are using 64-bit doubles, but can I assume that all calculations will be the same?
For example, what about converting a string to float - should I be concerned that Flash has a potentially different implementation as PHP? (If this happens, our game will crash - the client will think that it is in one state, and the server is in another)
With some testing it appears to be the same, but I'm just not sure. Can anyone please clarify this for me?
a source to share
You cannot, because different compilers can generate different codes even for the same expression, but we are comparing different interpreters compiled by god who know what.
More often than not, you will be fine depending on the precision required, but edge cases will occur when calculating numbers that are mathematically assumed to be the same, but in practice not due to the different order of operations, like trying to balance the needle to its tip. Regular updates do not change the fact that the numbers will appear in a slightly differently significant way.
You have to design numerical algorithms with some error field in mind if you want to use floats. Alternatively, you can use fixed point arithmetic only for state where performance is most likely network limited, but uses floats elsewhere.
a source to share