# Use floats or doubles when writing mobile games

I know for shaders, floats should be used because they are the optimal choice for performance. Doubles are slow and use more memory which isn't ideal for shaders. While floats are less accurate and are prone to float overlflows, the tradeoff for speed is ideal on mobile hardware.

But what about everywhere else in the code base not related to the shader pipeline? I'm writing an engine and use doubles for the delta update tick and try to keep all units relative to that data type so there's lots of places using doubles. Should I be using floats on mobile in the engine as well?

The engine is written in C++ language

• I'd imagine it largely depends on which language you're using. – DJ Spicy Deluxe Sep 28 '19 at 19:22
• Added C++ in the description – Dan Sep 28 '19 at 22:22

You'd be hard pressed to find a mobile GPU that could use doubles even if you wanted to, so that part of the choice is pretty much made for you.

There are only a few places where you might benefit from switching from single-precision floats in your game:

These would be the two most common examples of things you shouldn't try to store in a single-precision float, because they lose significant precision over the range you need to cover (in the first second of your game, you can represent the elapsed time to within $$\2^{-24}\$$ seconds, or tens of nanoseconds, but after 2.5 hours, you start to lose millisecond accuracy)

But that doesn't mean you need to use doubles necessarily. Integer types can handle these values just fine. (A 32-bit int can give you millisecond accuracy for 12 days of continuous play. 64-bit ints will last millions of years)

A delta time value will usually be on the order of 16 milliseconds, so storing it in a single precision float gives you precision to about $$\2^{-29}\$$s, which is still down to the level of nanoseconds — ie. probably well beyond the precision with which you can reliably time your code, signal your threads, or coordiante your graphics refresh anyway. Adding more precision here with a double is unlikely to improve your game in any measurable way.

So you can store your total elapsed time at high precision or using an integer type, compute your delta and convert to single-precision float once per frame, then use floats happily from there on.

Similarly for positions. Even with a huge world, if you break it up into chunks several km across or less, then single precision floats can locate a position relative to its parent chunk to a precision of less than a millimetre.

Relative offsets between objects (for computing headings and the like) are generally just fine to store in a float even out to immense distances. Your relative error, as compared to the total length of the vector, stays in a controlled range.

So, there's not much reason to turn to doubles as a default. I'd consider that only if you have a very particular need, where you've computed the precision of floats won't cut it, and keep your use of doubles contained to only the part of your game that actively needs it.