Fixed point math
is a quick-and-dirty
way to speed up any program that uses floating point
. You define what precision you'll be using, i.e. 16.16 (16 digits on the right side of the decimal and 16 on the left) Essentially, you take a floating point number
, and shift the decimal point over to the left until you're left with an integer.
This is usually done with #defines:
#define INT_TO_FIXED(x) ((x) << 16)
#define DOUBLE_TO_FIXED(x) ((long)((x) * 65536.0 + 0.5))
#define FIXED_TO_INT(x) ((x) >> 16)
#define FIXED_TO_DOUBLE(x) (((double)(x)) / 65536.0)
#define ROUND_FIXED_TO_INT(x (((x) + 0x8000) >> 16)
Fixed point math is/was primarily used in 3D graphics engines such as Doom, because working with integers is always faster than doing floating point, even with a FPU.
If anyone with a better knowledge of the subject would like to add more information, I'd appreciate it- my 3D graphics programming background is dubious at best.