C++/compare float or double to 0

  1. double d = 0.0;
  2. if (d == 0.0) {
  3. }

위 같은 코드에서 EPSILON을 고려해야할까?

아니다.

Wikipedia:IEEE-754에 따르면, 짧게 이야기하여

  1. double d;
  2. d = 0.0;
  3. d == 0.0   // guaranteed to evaluate to true, 0.0 can be represented exactly in double

  1. double d;
  2. d = 0.1;
  3. d == 0.1   // not guaranteed to evaluate to true

가 된다. ■

이 글에는 0 개의 댓글이 있습니다.