开发者

Please help me find out why i am in an infinite loop?

this is a bisection root method

double p1::root(double (*pf)(double k), int a, int b, double e) {

  // void nrerror(char error_text[]);

  double开发者_高级运维 left = (double)a;
  double right = (double)b;
  double midpoint;

  do
  {
      midpoint = ((right+left)/2);

      if(pf(left) *pf(midpoint) <0){
          right = midpoint;
      }
      else if(pf(right) * pf(midpoint) <0){
          left = midpoint;
      }
      else{
          break;
      }
  }while(abs(right-left) >2*e && abs(left-right)>e);

  return midpoint;
}


I see multiple exclusive relationals <, < and >

I expect at least one of them to be inclusive (>= or <=)

This is a good general rule of thumb (exceptions may occur, e.g. with intermediate increments of the compared values... so you need to stay awake, which is the first goot rule of thumb)


I would suggest putting some printfs in an doing some tracing of where you are.


Compile the program giving a -g switch for debugging and run it by gdb. That way you will know for what values is your do-while condition becoming true erroneously.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜