Does anyone use fixed point math anymore?

Started by
28 comments, last by Hodgman 11 years, 1 month ago

...

Assuming you're working in metres and need 1mm of accuracy, this isn't much of a problem unless your game world is a few thousand kilometers across. Fixed point grants you a few more orders of magnitude, but at those scales you'd soon have to switch to a hierarchy of coordinate systems anyway.

Assuming 1mm of accuracy, and taking into account rounding and such, the slightly-over-6 digits of precision in a float, this allows for a 1km range. If that's enough for all times -- fine, no issue. If you might ever want 10km or 20km, or maybe 100km at some time in the future, you have a problem.

Using a 32bit fixed point number with 1/1024m (0,97mm) resolution, you can go for a range of 4194km, which is enough for pretty much everyone who doesn't want to simulate planetary systems. In other words, it just works and you (probably) never need to think about it.

Of course one could just use double, and that'll work fine for anything reasonably sized (though Tom Forsynth's OffedOMatic might classify you as "idiot"). Unless of course, you want to simulate a single contiguous thing that is bigger than 100 million kilometers, but that isn't very likely.

I'm generally OK with "doing the wrong thing" as long as it works reliably. However, doubles are twice the size necessary, which somewhat puts me off. But I guess it's just a matter of taste.

Advertisement

Of course one could just use double, and that'll work fine for anything reasonably sized (though Tom Forsynth's OffedOMatic might classify you as "idiot")

Yeah I mostly agree with Tom. The only use that I automatically condone doubles for is storing absolute time values that have sub-millisecond precision (such as you need in any decent game loop). 32-bit integer or float isn't accurate enough if you want to allow for more than a day of up-time, so 64-bit tick-counters are required, and at that point, just converting ticks to seconds and storing them in a double is more convenient (because it avoids keeping around a separate ticksPerSecond integer that the gameplay code has to use when performing absolute-time based calculations).

On that note -- all our high precision timers are basically fixed point, counting in arbitrary "ticks", and providing this "ticks per second" that can be used to convert to floating point if required (as above, double-precision for absolute-time and single-precision for delta-times).

this allows for a 1km range

Oh, apparently I dropped a few zeros in my napkin math! I figured about 1000km unsure.png

This topic is closed to new replies.

Advertisement