Mathematics I / Searching and Sorting
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
  1. Write 1/3 in decimal. Now write 1/10 in binary.
  2. 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.

Read the lesson: Searching and Sorting →

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