We have already learned how to store both positive and negative integers. Now we are going to look at fractional numbers.
Fractional numbers, or as we commonly call them “numbers with decimals”, are those that do not correspond to whole parts (for example, 0.5, 2.31, 7.353 are fractional numbers).
Representing fractional numbers is more complicated than representing positive or negative integers. So we have different representation methods, with floating-point being the most common.
Fixed-Point Representation
One of the simplest ways to represent numbers with decimals in binary is the fixed-point method. In this approach, a fixed number of bits is assigned for the integer part and another fixed number for the fractional part of the number.
For example, in an 8-bit system with 4 bits for the integer part and 4 for the decimal part, the number 5.75 would be represented as 0101.1100.
This technique is straightforward and easy to implement, but it has many limitations. The precision is limited by the number of bits dedicated to the fractional part.
Furthermore, it is not dynamic and cannot handle numbers that exceed the range defined by the assigned bits.
Floating-Point Representation
The floating-point method is the de facto standard for representing numbers with decimals in most modern computers. This method allows greater flexibility and precision when handling numbers of different magnitudes.
The IEEE 754 standard is the most widely used for representing floating-point numbers.
In floating-point representation, a number is divided into three parts: the sign, the exponent, and the mantissa.
- The sign indicates whether the number is positive or negative.
- The exponent determines where the decimal point is.
- The mantissa is the fractional part of the number.
For example, the number 5.75 in 32-bit floating-point format would be:
0 10000001 01110000000000000000000
- Sign:
0(positive) - Exponent:
10000001(129 in decimal) - Mantissa:
01110000000000000000000
This method allows handling numbers of very different magnitudes by adjusting the exponent. But it also has its limitations, especially in terms of precision for very small or very large numbers.
The representation is more complex than fixed-point representation and requires greater computational cost. But, in return, it allows us to cover a huge range of numbers.
Other Representations
There are other much less common, but equally interesting techniques for representing numbers with decimals in binary. Some of these include:
Midpoint notation: In this technique, a number is represented as the sum of two fixed-point numbers. This can be useful in situations where high precision is required and range is not a primary concern.
Fixed-comma method: Similar to fixed-point, but with a variable number of bits for the fractional part. This can allow for greater precision for certain numbers, but at the cost of flexibility in range.
Normalized floating-point: A variant of floating-point that guarantees the most significant bit of the mantissa is always
1, which improves precision and range compared to standard floating-point.
