6

We can use integer for game physics (or without physics, simply object representation): mass, position and rotation, where the integers represent, for example, the number of milligrams, millimeters or (1/56000) of a degree. However, almost all game code I've seen recently use floating points.

Is it slower to use integers for game physics calculations? Are there any other advantages and disadvantages from the developer's point of view for using integers? Or it is because all our hardwares

Ming-Tang
  • 1,040
  • 1
  • 12
  • 20

3 Answers3

8

The days where floating point computations were (significantly) slower than integer calculations are long gone. If I'm not mistaken it was the Pentium CPU that first leveled the playing field of floats vs integers.

So there's actually no reason whatsoever to not use float (or even double) for any calculation that benefits from it. The use of integers to represent floating points was just a crook in order to avoid the performance impact of floating point calculations on older hardware.

In fact modern (desktop) CPUs don't even make much of a difference between float and double, especially on 64bit CPUs running a 64bit OS.

CodeSmile
  • 1,734
  • 2
  • 15
  • 16
  • 2
    So you want to say that DDA and Bresenham drawing algorithms which use integers only, are no longer useful optimizations? I'd bet these will still be faster than naive float + round solution. – Tomas Jan 04 '14 at 13:58
  • Anyway, I think it makes sense to use integers since everything - both time and space - in the game are discrete (pixels, time steps...) – Tomas Jan 04 '14 at 14:01
  • 1
    @Tomas Betting is not knowing. ;) Besides to draw lines I can usually rely on glDrawLine and similar. And not everything is discrete, delta time for example. Also: subpixel rendering, anti-aliasing, and similar effects. Point being: even if there are optimized integer-based algorithms that may even to this day be faster, they typically aren't meaningful to most real world development being done today. – CodeSmile Jan 04 '14 at 14:16
  • anti-aliasing is for sure integer thing - it's the remainder in the DDA/Bresenham algorithms 2) and how is glDrawLine implemented? I'd bet (again :) its based on integer algorithms :-)
  • – Tomas Jan 04 '14 at 14:44
  • @Engineer Integer operations are very consistent across platforms. Fixed-point operations are implemented using integer operations (with scaling applied by the developer, who presumably would not scale differently on each platform). – John B. Lambe Dec 10 '21 at 19:54
  • @JohnB.Lambe Thank you for replying to my comment of nearly 8 years ago. Today, libfixmath would be my preferred solution when writing native, deterministic code. – Engineer Dec 10 '21 at 20:46
  • Both my own tests and those reported by others (e.g. https://stackoverflow.com/questions/2550281/floating-point-vs-integer-calculations-on-modern-hardware ) show integers of any size (up to 64 bits on a 64-bit processor) being faster than 32-bit (and 64-bit) floating point on modern CPUs. (On most GPUs, I think 32-bit floating point would be faster than integers or 64-bit floating point, so that may be a reason for using it for graphics or GPU-based physics. For CPU-based physics, I think the performance gain would far outweigh the conversion to float for rendering.) – John B. Lambe Dec 10 '21 at 21:05