[comp.lang.c] integer square root rounding

andras@alzabo.uucp (Andras Kovacs) (10/04/90)

 I apologize if it is not the proper newsgroup to ask but I need help.
 I implemented the Newton-Raphson method to calculate integer square root with
the following code:

int Sqr(int param)
{
    int guess = 1, tmp;
    do
    {
        tmp = guess;
        guess = (guess + param / guess) / 2;
    }
    while (tmp != guess);
    return guess;
}

 (Actually, I wrote it in ARM assembly but this is the idea.)
 Now the problem is that this code rounds DOWN to the closest integer; can
someone tell me how to modify it to get properly rounded results?

 Please reply through E-mail.

 Thanks
  Andras Kovacs

"Don't have a cow, man!" (Attributed to Bart Simpson)