Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

The problem with float and double is that they are not exact. If you are to do something like store replays, the values would have to be exact.

The problems with decimal is that they are approximately 16x slower (confirmed by searching and personal testing) than floats and doubles.

Couldn't Vector2s be another problem because they use floats internally for all the components?

How do other games solve this problem? I'm sure they must use floats and doubles but aren't they not deterministic across platforms and different architecture?

The replay files for games like SC2 run in a linear fashion so you cannot skip ahead so how do they solve the determinism issue with floating point numbers?

share|improve this question
Probably the imperfection on exactness is so small that you wouldn't even notice in real life. That's why by default XNA Vectors use floats. Also, doubles would take too much ram, decimals would be too slow. –  Gustavo Maciel Nov 18 '12 at 17:45
Floating point number operations are deterministic. What issue could there be? –  snake5 Nov 18 '12 at 17:45
@snake5 Floating point operations are not consistently deterministic across platforms - for instance, in things like rounding modes, precise square root approximations, etc. - and for games that need precise cross-platform determinism it is an issue. –  Steven Stadnicki Nov 18 '12 at 17:47
@StevenStadnicki Appears to me that they can be configured to be deterministic. gafferongames.com/networking-for-game-programmers/… –  snake5 Nov 18 '12 at 17:49
@snake5 'Can, mostly, be configured to be with a whole lot of effort as long as you're on the same platform' is a long way from 'floating point ops are deterministic'. The very link you provide (which is a good resource for OP, and may deserve to be expanded into an answer!) even says "It is incredibly naive to write arbitrary floating point code in C or C++ and expect it to give exactly the same result across different compilers or architectures." –  Steven Stadnicki Nov 18 '12 at 18:00
show 2 more comments

1 Answer

Ideal replay systems don't depend on the exactness of floating point numbers.

All of the actual logic and events can be recorded. That is, you don't just record "fired bullet at pos 3.4,5.2 at vel 1.2,-0.8" and then expect the client to re-execute the bullet collision logic. You record both that the bullet was fired and that the bullet collided with something (and where) and what the results of that collision where. At appropriate time points, the replay data will "correct" any values (that is, after an object bounces, the replay records the new position and velocity, rather than expecting the client to recalculate those).

This is the exact same as network synchronization. A lot of games with replay systems in fact reuse their network infrastructure, since replay data can be the exact same as the data sent to spectator clients.

Thus your replays might at worst have some very slight discrepancies visually (highly unlikely to be noticeable), but the replay will still play out mechanically the exact same. Those discrepancies will not be vital to the game itself, and hence you can completely ignore the potential inaccuracies of floating point numbers.

Also keep in mind that those discrepancies will never really happen on most real hardware, because a replay of SC2 is always played back on the same CPU architecture. That is, you aren't going to be recording a session on an x86 CPU and then playing it back on an ARM CPU, because there is only an x86 SC2 client. Same goes for most other games.

The only "hard" part is going to be synchronizing time steps so that events are firing in the proper sequence and at the proper speed, but that's mostly solved by just using fixed time steps.

share|improve this answer
A quote from Gas Powered Games: "I work at Gas Powered Games and i can tell you first hand that floating point math is deterministic. You just need the same instruction set and compiler and of course the user’s processor adheres to the IEEE754 standard, which includes all of our PC and 360 customers. The engine that runs DemiGod, Supreme Commander 1 and 2 rely upon the IEEE754 standard. Not to mention probably all other RTS peer to peer games in the market. " –  Roy T. Nov 18 '12 at 21:10
@RoyT.: I almost put that exact argument into what I wrote, thanks for deciding to bring it up. If you rely on compatible hardware models then you get determinism; there are still other ways to introduce inaccuracies that come up in real-world situations, but your replay system and engine take those into account, which is why you don't rely on having two clients simulate something exactly identically, and "fix" things where and when you can so inaccuracies don't matter. (That quote is from Elijah Emerson, btw; we worked together at GPG.) –  Sean Middleditch Nov 18 '12 at 21:48
sorry, in hindsight it wasn't meant as critique I just thought it would be good to have a quote from a working product. It doesn't contradict your answer, though I thought it did at first. I should've read better :) –  Roy T. Nov 18 '12 at 22:21
No worries, it's good that you posted that to clarify it further. –  Sean Middleditch Nov 18 '12 at 23:54
I'm a little uncomfortable with the "well-written replay systems" line. I've been involved in console games which could not afford the space in memory to store the sort of data that you're suggesting here, and were forced to merely record user inputs per frame and replay the game deterministically. I would never go so far as to declare that those systems were not "well-written". But I'll completely agree that that situation probably doesn't come up in the modern world any more, and most modern replay systems have no good reason to be implemented that (painful!) way. –  Trevor Powell Nov 19 '12 at 7:08
show 1 more comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.