It seems every time I implement fixed point math, I have to relearn it. In my latest project, I’m using the notation below and it has gone the easiest I can remember.
This won’t go over fixed point math – Wikipedia and graphics sites have great tutorials on the ins and outs.
The notation I now use is: ‘# of bits’ : ‘# of integer bits’ . ‘place of least fraction bit (LFB)’
* 28:12.16 means the number has 28 bits in it with 12 bits of integer and the LFB is 1/2^16.
* 21:0.40 means the number has 21 bits in it with 0 bits of integer and the LFB is 1/2^40
One of the calculations I have to is forward velocity * gain. Forward velocity is a 19 bit number with 11 integer bits and eight fraction bits. The gain is an 11 bit number with three integer bits and eight fraction bits. This is notated as:
Velocity: 19:11.8 * Gain: 11:3.8 = Output: 30:14.16.
It is easy to cross check:
* Total number of bits: 19 + 11 = 30
* Number of integer bits: 11 + 3 = 14
* Place of LFB: 8 + 8 = 16
I’ve noticed that doing this through out my code/notebook, I’ve been able to make sure I don’t have overflows and know when to shift variables to make them fit and when I have to make a change, I can quickly see the impact.