What is the output of the following Python code? print(0.1 + 0.2 == 0.3)
Correct answer B. False
Explanation
Floats are stored in binary, and 0.1, 0.2 and 0.3 cannot be represented exactly, so 0.1 + 0.2 produces 0.30000000000000004, which is not equal to the stored 0.3. Use math.isclose() or the decimal module when comparing or handling decimal fractions.
Answer checked How we write and check questions
Discussion