Problem with a recursive function to find sqrt of a number

Posted by Eternal Learner on Stack Overflow See other posts from Stack Overflow or by Eternal Learner
Published on 2010-05-20T22:37:38Z Indexed on 2010/05/20 22:40 UTC
Read the original article Hit count: 172

Filed under:

Below is a simple program which computes sqrt of a number using Bisection. While executing this with a call like sqrtr(4,1,4) in goes into an endless recursion . I am unable to figure out why this is happening. Below is the function :

double sqrtr(double N , double Low ,double High  )
{

     double value = 0.00;

     double mid = (Low + High + 1)/2;

    if(Low == High)
     {
        value =  High;

     }

     else if (N < mid * mid )
     {
        value = sqrtr(N,Low,mid-1) ;


     }
     else if(N >= mid * mid)
     {
         value = sqrtr(N,mid,High) ;

     }

     return value;

}

© Stack Overflow or respective owner

Related posts about c++