Physics I / Data representation and basic types
Practice question · Multiple choice

In Python, 0.1 + 0.2 == 0.3 is False. What is the root cause?

Hints
  1. How does 1/3 look in decimal? 0.1 = 1/10 has the same problem in base 2.
  2. Floats store binary fractions: 1/10 in binary is 0.000110011001100… forever.
Show the answer

A. 0.1 and 0.2 have no exact binary representation

Why

Binary floats can only finitely represent sums of powers of 1/2; one-tenth repeats forever, so it is stored rounded. The sum lands at 0.30000000000000004. That is why numerical code compares with a tolerance (abs(a-b) < 1e-9), a lesson every physics simulation learns eventually.

Read the lesson: Data representation and basic types →

Practise Data representation and basic types

The app has 8 more questions on this lesson, and keeps your place in the course. Physics I is free to start.

More questions on Data representation and basic types