Physics engine: use double or single precision?

c++, floating-accuracy, floating-point, opengl, simulation

Solution

This is all going to depend on your application. You pretty much already understand the tradeoffs between the two:

Single-precision

- Less accurate

- Faster computations even on todays hardware. Take up less memory and operations are faster. Get more out of cache optimizations, etc.

Double-precision

- More accurate

- Slower computations.

Typically in graphics applications the precision for floats is plenty given the number of pixels on the screen and scaling of the scene. In scientific settings or smaller scale simulation you may need the extra precision. It also may depend on your hardware. For instance, I coded a physically based simulation for rigid bodies on a netbook and switching to float gained on average 10-15 FPS which almost doubled the FPS at that point in my implementation.

My recommendation is that if this is an educational activity use floats and target the graphics application. If you find in your studies and timing and personal experience you need double-precision then head in that direction.

Problem

I am making a rigid body physics engine from scratch (for educational purposes), and I'm wondering if I should choose single or double precision floats for it. I will be using OpenGL to visualize it and the glm library to calculate stuff internally in the engine as well as for the visualization. The convention seems to be to use floats for OpenGL pretty much everywhere and `glm::vec3` and `glm::vec4` seem to be using `float` internally. I also noticed that there is `glm::dvec3` and `glm::dvec4` though but nobody seems to be using it. How do I decide which on to use? `double` seems to make sense as it has more precision and pretty much the same performance on today's hardware (as far as I know), but everything else seems to use `float` except for some of GLu's functions and some of GLFW's.

Original source

Related problems