Computer Arithmetic

In many programming languages, why is the double-precision comparison 0.1 + 0.2 == 0.3 false?

  1. AThe add instruction sometimes cannot add fractions correctly
  2. B0.3 is a special value that floating point cannot represent, so comparisons are always false
  3. CEach is a rounded approximation, so the values differ slightly
Show answer and explanation

Answer: C. Each is a rounded approximation, so the values differ slightly

None of 0.1, 0.2 and 0.3 is exact in binary; each is stored rounded to the nearest representable value. Adding the approximations of 0.1 and 0.2 and rounding gives 0.30000000000000004, which differs in the last digit from 0.3's approximation, 0.29999999999999998…. Floating-point values are therefore usually compared by checking whether the absolute difference is within a small tolerance. The add instruction rounds correctly per the standard, and 0.3 is representable as an approximation.

  • A. The add instruction rounds correctly as the standard requires.
  • B. 0.3 can be represented as a nearby value.
  • C. Correct. Each value's rounding error makes the results differ slightly.
Practice this topic Back to topic

More Computer Arithmetic questions

  1. What is the machine epsilon (the gap between 1 and the next larger representable number) in IEEE 754 single precision?
  2. Which gives the approximate number of reliable decimal digits for single and double precision?
  3. What is the role of IEEE 754 subnormal numbers?
  4. On some processors, computations involving many subnormal numbers can slow down dramatically. Which setting is used when performance takes priority?
  5. Under IEEE 754, what result does the standard specify for dividing a positive number by zero?