Sunday, September 22, 2019 | Toby Opferman

## Fixed Point Math

```Toby Opferman
http://www.opferman.net
programming@opferman.net

Fixed Point Math

This is a tutor on how to do Floating point math with
integers.  First, it is vaulable to use 32-bit integers,
for better accuracy (64-bit even better).   For this,
we use the "long" data type in C.

unsigned long Fixed_X, Fixed_Y;

Now, we have the number 43.23   If we want 16:16 (16 bit Integer,
16 bit Mantessa) we simply multiply the number by 65536.

Fixed_X = 43.23*65536;
Fixed_Y = 30.23*65536;

Now, we have 2 Numbers.  We can perform the following operatoins on them:

Fixed_X + Fixed_Y
Subtraction:
Fixed_X - Fixed_Y

Those two operations can be performed with almost (Actually,
most of the time complete accuracy) perfect accuracy.

Multiplication & Division you will get accuracy loss.  But, hopefully
the application you are making and the reason that brought you to use
Fixed Point will allow for a little accuracy loss.

Multiplication:
((Fixed_X>>8) * (Fixed_Y>>8))
or
((Fixed_X * Fixed_Y)>>16)

There are a variation of ways to perform this depending on how
your compiler will generate the code.

There is an overflow that you need to adjust so shift both multiplicands
left by 8 bits.

((Fixed_X<<8) / Fixed_Y)<<8
or
(Fixed_X<<16) / Fixed_Y
You may want to increase the Dividend by 8 Bits.
Then Increase the Quoient by 8 bits.

To view the integer part:
Fixed_X>>16

To Get back the fractional part:
(float)Fixed_X/65536.0

This will not work on 16-Bit compilers that well.  Some of the time,
if you are using small numbers the above will work in 16 bit compilers
since they at MAX can do 32-bit Math.  But, you need 64-bit math meaning
32-bit overflow. A good TXT to get would be FPT1_RT1.TXT which can be
found on x2ftp.oulu.fi.  This text has some good Assembly functions
for Fixed Point math including: Square Roots, Multiplication, and Division.
```

Professional software engineer with over 15 years...