How to represent fractions in computers
How to represent fractions in computers
1. Fixed point representation
The first way to represent fractions is so-called “fixed point representation”, which can be written like this:
[sign] xxxx.xxx
For example, 11111.000
is -15 in decimal.
However, this representation method has two imitations:
Wastes space: In the above example,
11111.000
is -15, while the space after the decimal mark(.000
)is wasted.Trade Off between precision and range: As we only have three bits to represent the fractional part, we can not represent some numbers in precise. For example, we can not have
11111.0001
. And if we want to extend the fractional part using[sign]xxx.xxxx
, the range of numbers we can represent is,however, reduced. Therefore, increasing in precision is at the expense of range.
2. Floating point representation
The other method is floating point representation
, which makes use of scientific notation. Consider an 8-bit word, the fractions can be represented as follow:
[sign bit][sign of exponent][3-bit exponent][3-bit (fractional) mantissa]
For example:
Note: In floating point, 01000000
represents 0, and 11000000
is -0. Actually, $2^(-0)$ means nothing, but we intepret it as 0.