Skip to main content

Section 4.2 Rounding Errors

Most floating-point numbers are only approximately correct. Some numbers, like reasonably sized integers, can be represented exactly. But repeating fractions, like \(1/3\text{,}\) and irrational numbers, like \(\pi\text{,}\) cannot. To represent these numbers, computers have to round off to the nearest floating-point number.
The difference between the number we want and the floating-point number we get is called rounding error. For example, the following two statements should produce equivalent results, but do not:
Listing 4.2.1.
The problem is that 0.1 is a repeating fraction when converted into binary. So its floating-point representation stored in memory is only approximate. When we add up the approximations, the rounding errors accumulate.
The values are very close. To see the difference, we had to add the code on line 7 to say β€œprint to 20 places”. If you remove that line, the numbers will be only displayed to 6 digits. When rounded at that point, they both will look like 2.00000 and just display as 2.
Why is this the case? Well, doubles have a fixed number of bits to store their answer. Imagine trying to store \(\pi\) as a decimal but only getting to use at most 6 digits. The closest you could come to the value of \(\pi\) would be 3.14159. The same thing happens with doubles, only the values that are difficult to represent accurately are not always what we expect (0.1 is easy to represent in our base-10 number system, but difficult to represent in the base-2 system used by doubles).
The answer is close to correct - doubles provide accuracy to ~15 significant digits. For many applications (like computer graphics, statistical analysis, and multimedia rendering), floating-point arithmetic has benefits that outweigh the costs.
But if you need absolute precision, use integers instead. For example, if you were storing financial data for a bank, representing a penny as 0.01 dollars would be a bad idea. Over time, errors would accumulate and balances would no longer be correct. Instead, it would be better to represent the amount in cents - to represent $1.25 as 125. Any time you did a calculation that resulted in a partial cent you would figure out whether to round it up or down to the nearest cent. (Indeed there are established rules for how to round financial calculations).

Checkpoint 4.2.1.

You have attempted of activities on this page.