To do computations involving real numbers using integer arithmetic
Represent real numbers as integers, scaled by an appropriate negative power of 2.
Assume you are using 8-bits to represent real numbers. You would like to devote 4 bits to the whole part, and 4 bits for the fractional part.
| 1
|
| 1
|
| 1
|
1.5 + 2.25 is implemented as 2-4 *24 + 2-4*36 . Of course, the hardware simply adds 24+36 to yield 60 which is interpreted with a scale factor of 2-4 as 0011.1100, which is 3.75.
2.25 - 1.5 is implemented as 2-4 *36 - 2-4*24 . The hardware simply performs 36-24 = 12, which is interpreted with a scale factor of 2 -4 as 0000.1100, which is 0.75.
2.25 * 1.5 is implemented as 36*24 = 864. The result, 0000001101100000, is a 16-bit number with a scale factor of 2- 8 (Why ?). The scale factor is adjusted to 2 -4 by right shifting the result by 4 bits, and the result is stored in an 8-bit field as 00110110, which is interpreted as 3.375.
| 2.25
|
| 36
|
Precision can be improved by shifting the dividend to the left before division. Since integer division is implemented by using a 2n bit dividend and an n bit divisor, the division can be implemented by pre-shifting the dividend 4-bits to the left. Thus, the dividend has a value of 36* 2 4 with a scale factor of 2 -8 , and is represented using 16-bits. The division is performed as 36* 2 4 divided by 24, which is 576/24 which results in the value 24. Since the result has a scale factor of 2-4 , it can be intepreted as 0001.1000, which is 1.5.
| 1
|
\protect |
Consider computing
| 1
|
| 16
|
Can you gain precision by preshifting the dividend on your own ?
Assume you preshift the dividend left by 2 bits. The dividend is now 64
with a scale factor of 2-6 . The divisor
is still 96 with a scale factor of 2-4
. The hardware left shifts the dividend four bits (as before) before performing
the division as
| 64*24
|
We proceed exactly as before and the quotient is 10 with a scale factor
of 2-6 . Since the result is to be
stored in a 16-bit field with an 8-8 format, the scale factor needs to be
2-8 . Therefore we adjust the scale
factor by shifting the quotient left by 2 bits to yield 00101000 with
a scale factor of 2-8 . This is interpreted
as 0.00101000 which represents 0.15625 and is closer to the actual value
of
| 1
|