Practice question · Multiple choice
In Python, 0.1 + 0.2 == 0.3 is False. What is the root cause?
Hints
- How does 1/3 look in decimal? 0.1 = 1/10 has the same problem in base 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.
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
- Order the steps to convert the decimal number 13 to binary.
- A sensor stores readings as 8-bit unsigned integers. How many distinct values can one reading take? Set the…
- In Python, the string '42' and the integer 42 are the same type.
- In floating point, 0.1 + 0.2 == 0.3 is false. What does that reveal about how numbers are stored?