Predict the output
What does this print?
from decimal import Decimal
print(0.1 + 0.2 == 0.3)
print(Decimal("0.1") + Decimal("0.2") == Decimal("0.3"))
print(float(2**53 + 1) == 2**53 + 1)Hint 1 of 1
What's the largest exact integer in a double?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A False, True, False
Correct. Binary floats miss 0.3, Decimal is exact, and 2^53 + 1 can't be held in a double.
Binary floats can't store 0.1 or 0.2 exactly, so their sum misses 0.3. Decimal from strings does exact decimal arithmetic, and a double has 53 bits of precision, so 2^53 + 1 rounds to 2^53.
Why not the others?
- B
True,True,True. Floats can't represent 0.1 exactly. - C
False,True,True. 2^53 + 1 rounds to 2^53 as a float. - D
False,False,False. Decimal built from strings is exact.