For-loop in C++ using double breaking out one step early, boundary value not reached

后端 未结 10 698
难免孤独
难免孤独 2020-12-06 03:24

I have a simple C++ program compiled using gcc 4.2.4 on 32-bit Ubuntu 8.04. It has a for-loop in which a double variable is incremented from zero t

10条回答
  •  不知归路
    2020-12-06 04:04

    Generally, when you compare doubles, simple comparison is not good enough, and you should compare them "up to a precision". i.e. :

    if ( fabs(double1-double2) < 0.0000001 ) {
      do-something
    }
    

    The problem occurs due to the representation of double variables.

提交回复
热议问题