ProgrammingVery hardProduct-company MCQ pattern

What is printed?

What is printed?
from decimal import Decimal
print(Decimal('0.1') + Decimal('0.2') == Decimal('0.3'), Decimal(0.1) == Decimal('0.1'), float(Decimal('0.1') + Decimal('0.2')))
  1. A. False False 0.3
  2. B. True False 0.3
  3. C. True True 0.3
  4. D. True False 0.30000000000000004

Correct answer: B. True False 0.3

Decimal arithmetic is exact in base 10, so the sum equals 0.3 exactly. Decimal(0.1) captures the binary value of the float, which is not exactly 0.1.

More Python Programming questions

Exams that ask this topic