Comparing Objects

AP Computer Science A· difficulty 4/5

Why is <code>if (0.1 + 0.2 == 0.3)</code> unreliable?

  • A

    Always works

  • B

    Round-off error in floating-point representation

    check_circle
  • C

    0.1 isn't a valid number

  • D

    Doubles can't be compared

Explanation

Binary FP cannot represent 0.1 exactly; 0.1+0.2 = 0.30000000000000004.

Want 10 more like this — adaptive to your weak spots?

Related questions