Practice question · Multiple choice
Floating-point arithmetic gives 0.1 + 0.2 ≠ 0.3 on essentially every computer. Why does a well-designed system get simple decimal arithmetic wrong?
Hints
- Write 1/3 in decimal. Now write 1/10 in binary.
- Ask whether the error is in the addition or already in the operands.
Show the answer
A. Because 0.1 has no finite binary expansion, as 1/3 has no decimal one.
Why
The problem exists before any arithmetic: 0.1 in binary is 0.0001100110011… forever, exactly as 1/3 is 0.333… in decimal, so what gets stored is already slightly off. Carry three consequences, never compare floats with ==, never store money as a float, and be wary of accumulating many small values where the errors compound.
Practise Searching and Sorting
The app has 6 more questions on this lesson, and keeps your place in the course. Mathematics I is free to start.
More questions on Searching and Sorting
- Summing a million floating-point numbers from smallest to largest is more accurate than from largest to…
- Complete the reason binary search is fast.
- A sorted list holds 1,048,576 items, which is 2 to the power 20. How many halvings does binary search need to…
- A sorted list holds 64 items. Binary search halves the range at each comparison. In the worst case, how many…
- Order the steps of merge sort on a list.
- Sort each situation by the search method it calls for.