Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[SOLVED] Why do different systems yield different floating point results for the same calculations?

Discussion in 'Scripting' started by Deleted User, Jan 20, 2015.

  1. Deleted User

    Deleted User

    Guest

    I've been doing a bit of reading lately about making a multiplayer game, and I've read that the client should be responsible for their own simulations, but also that it can sometimes be problematic because different machines will yield slightly different results which could compile to big differences over the course of the game.

    Why does this happen?

    Shouldn't a float store the same amount of data no matter the machine?
    And if it can, then math is math so a calculation is always performed the same way.
     
  2. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    Well, I guess the main problem for those issues is the Floating-Point Unit. As far as I know some of those FPUs are calculating with a higher precision than the IEEE 754-1985 standard has defined for floats and doubles. So the result stored in variables (which will stick to the IEEE standard) might change depending on the machine.

    On the other hand, even if the FPU is the same on every machine: The implementation for certain calculations might differ slightly on several platforms which in turn can create other floating point imprecisions/discrepancies.
     
    Last edited: Jan 20, 2015
  3. Deleted User

    Deleted User

    Guest

    How can one insure the calculations will always yield the same results?
     
  4. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    I have no experience with floating point issues in multiplayer based games, so I can't help you much. Depending on the language I would suggest to look into libraries like GMP or CLN (C++). The general topic is called "Robust Computing" although it also contains several other aspects. Since you ask in a Unity forum you might want to know solutions for C#, Boo or UnityScript, but here I have really no idea. You might need to synchronise the data of your clients with the data of your server every now and then.
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    In short, don't use floats if this matters. Use a data type with perfect precision, such as an int.
     
  7. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Fundamentally, because brains are good at providing the illusion of self. Due to this, we have the concept of being able to design something for a single user, and computers have always been designed for this illusory entity. At best, the design has assumed a set of users in the same location.
     
  8. Deleted User

    Deleted User

    Guest

  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Deterministic systems are impractical. That's why there are so few games that use them. About the only genre that really makes use of this is RTS. Its required to prevent the massive data transfer that would be required to update the position and status of every unit ten times a second. Everything else either uses an authoritative server, or simply corrects inconsistencies as they arrive.