ProgrammingVery hardProduct-company MCQ pattern

What is printed?

What is printed?
print(sum([0.1] * 10) == 1.0, sum([0.1] * 10), sum([0.1] * 10, 0) == 1.0)
  1. A. False 0.9999999999999999 True
  2. B. True 1.0 True
  3. C. True 1.0 False
  4. D. False 0.9999999999999999 False

Correct answer: B. True 1.0 True

Since Python 3.12, sum() of floats uses compensated (Neumaier) summation, so the rounding error is recovered and the result is exactly 1.0. A plain for-loop with += still gives 0.9999999999999999.

More Python Programming questions

Exams that ask this topic