Back to questions
JuniorTheoryCommon
What does the program print?
total = 0.1 + 0.2
print(total == 0.3, round(total, 2) == 0.3)False True
- Expecting
0.1 + 0.2to equal0.3exactly: both are stored in binary with a tiny error, so the sum is0.30000000000000004. - Expecting
roundnot to help:round(total, 2)gives the float closest to0.30, the same float the literal0.3gives, so they are equal.
- What does
print(0.1 + 0.2)show? - How would you compare two computed floats without
round?