ProgrammingVery hardGATE CS · Product-company placement

What is printed (Python 3.12 or later)?

What is printed (Python 3.12 or later)?
from functools import reduce
xs = [0.1] * 10
print(sum(xs) == 1.0, reduce(lambda a, b: a + b, xs) == 1.0, sum(xs[:3]) == 0.3)
  1. A. True False False
  2. B. False False False
  3. C. True True True
  4. D. False False True

Correct answer: A. True False False

Since Python 3.12, sum() of floats uses compensated (Neumaier) summation, so ten 0.1s give exactly 1.0, while a plain left-to-right reduce drifts to 0.9999999999999999. For three 0.1s even the correctly rounded sum is 0.30000000000000004, not 0.3.

More Python Programming questions

Exams that ask this topic