Floating Point Types Usage and Limitations

In this lesson, you will learn how to choose an appropriate type of the floating point and some limitations of these types.

Which floating type to use #

Unless there is a specific reason to choose otherwise, you can choose double for floating point values. float has lower precision than double and real, but it takes less number of bits and can be useful when we have limited memory. On the other hand, since the precision of real is higher than double on some hardware, it would be preferable for high precision calculations.

Limitations #

Let’s discuss a few limitations that we have with the floating point types.

Cannot represent all values #

It is impossible to represent some values in our daily life. In the decimal system that we use daily the digits before the decimal point represent ones, tens, hundreds, etc., and the digits after the decimal point represent tenths, hundredths, thousandths, etc. If a value is created from a combination of these values, it can be represented exactly. This is not true for recurring decimal values.

Example #

As the value 0.23 consists of 2 tenths and 3 hundredths, it is represented exactly. On the other hand, the value 1/3 cannot be exactly represented in the decimal system because the number of digits is always insufficient, no matter how many are specified: 0.33333…
For such cases, the floating point types cannot represent the value beyond a certain precision because they have a limited number of bits.

Difference with the binary system #

The difference between the decimal system and the binary system that computers use is that the digits before the decimal point are ones, twos, fours, etc., and the digits after the decimal point are halves, quarters, eighths, etc. Only the values that are exact combinations of those digits can be represented accurately.
A value that cannot be represented exactly in the binary system used by computers is 0.1, as in 10 cents. Although this value can be represented exactly in the decimal system, its binary representation never ends and continuously repeats four digits: 0.0001100110011…

Note: Observe that the value is written in a binary system, not decimal.

It is always inaccurate at some level depending on the precision of the floating point type that is used.

The following program demonstrates this problem. The value of a variable is being incremented by 0.001 a thousand times in a loop. Surprisingly, the result is not 1:

Get hands-on with 1200+ tech skills courses.