210 Notes on Fixed Point Numbers
The electronic circuitry used for adding unsigned integers can also be
used to add fractional numbers. As with signed integers, we need to
impose a new interpretation on the bit patterns to achieve this.
One part of the computer word is chosen as the fractional part, and the
other part is the integer part.
Example: 8-bit integer. Decide to treat last 4 bits as fraction part:
1011.0110 = 1*2^3 + 0*2^2 + 1*2^1 + 1*2^0
+ 0*2^-1 + 1*2^-2 + 1*2^-3 + 0*2^-4
I.e. the number is 11.375
Powers of 2:
3 8
2 4
1 2
0 1
-1 0.5
-2 0.25
-3 0.125
-4 0.0625
Adding fixed-point numbers
1011.0110 11.375 [ 182
+ 0011.1010 + 3.625 + 58
= 1111.0000 = 15.000 = 240 ]
Great! Ordinary unsigned arithmetic works.
[ Can you make signed fixed-point numbers? ]
Multiplication
Need to right-shift answer 4 places (or shift numbers before multiply)
0011.1010 3.625
x 0000.0100 x 0.250
1110.1000 (requires right shift 4 places)
= 0000.1110 = 0.875
Actual answer is 0.90625; we lost a low-order bit in the right shift.
Advantage
- fixed point arithmetic is as fast as integer operations
- no special hardware required (although a multiply-and-shift
instruction would be useful)
Disadvantages
- limited range of values (e.g. 32-bit fixed point with 8-bit
fraction gives MAXFIXED of about 8 million)
- massive loss of precision if intermediate result exceeds MAXINT
- most languages do not support fixed point (programmer must
normalise results manually)