Rounding Errors

Because floating-point numbers have a limited number of digits, they cannot represent all real numbers accurately: when there are more digits than the format allows, the leftover ones are omitted - the number is rounded. There are three reasons why this can be necessary:

Rounding modes

There are different methods to do rounding, and this can be very important in programming, because rounding can cause different problems in various contexts that can be addressed by using a better rounding mode. The most common rounding modes are:

Examples in base 10:

  Towards zero Half away from zero Half to even
1.4 1 1 1
1.5 1 2 2
-1.6 -1 -2 -2
2.6 2 3 3
2.5 2 3 2
-2.4 -2 -2 -2

More rounding methods can be found at Wikipedia.

© Published at under the Creative Commons Attribution License (BY)

Fork me on GitHub