LearnFrenzy
Sign in Take a free test

Decision Making · Decision Making MCQ · Question 6

How to compare two doubles in an if condition Answer, explanation & discussion

  • MCQ
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

Two computed doubles should be treated as equal. Which condition is appropriate?

  • A a == b
  • B fabs(a - b) < 1e-9 (or a relative tolerance) Correct
  • C (int) a == (int) b
  • D a - b == 0
Correct Answer: Option B

Rounding errors make exact equality unreliable (0.1 * 3 != 0.3). Compare the difference with a tolerance – relative to the values' size for large numbers.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links