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?

+2


a source to share


3 answers


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.

+2


a source


I would work on the assumption that this is indeed the same floating point code. Also, you are sending periodic checkpoints (frames with full data calculated instead of deltas), right? This will fix any small bugs.



0


a source


It's okay if you can limit the number of significant decimal places, so you don't run into rounding errors or roll your own version of the string for floating point conversion, or any other methods you suspect might have different impact on platforms. i think you are good.

0


a source







All Articles