[net.lang.forth] sqrt

fbp@cybvax0.UUCP (Rick Peralta) (09/06/85)

This is'nt really quick or smart, but I think it works for most cases.

N - the number to find the root of.
R - resolution of the answer.
I - latest increment.
V - latest value to test.

sqrt(N)
{
    R = .01
    I = N / 2
    V = I
    if (N < 0) barf

    loop:
	I = N / 2
	if ( (V*V) > N+R )
	{
	    V = V - I
	    goto loop
	}
	else
	if ( (V*V) < N-R ) 
	{
	    V = V + I
	    goto loop
	}
    return V
}

Sorry for the pseudo code, I don't speak forth too well.

Rick  ...!cybvax0[!dmc0]!fbp

"Life is a compromise"