Floating-point rounding error — what does this code print?
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)- A.0.30000000000000004 True
- B.0.3 False
- C.0.30000000000000004 False
- D.0.3000000000000000444 False
Answer: C
It prints 0.30000000000000004 then False. Floats in Python are IEEE-754 64-bit binary floats; 0.1 and 0.2 cannot be represented exactly in binary, so the sum is slightly off. Therefore comparing with == against 0.3 gives False. Compare floats using a tolerance, e.g. math.isclose(a, b).