## Tuesday, December 29, 2009

### Back to Basics: Square Root Implementation in Python

Two days ago a friend of mine who is a computer science student asked me about the simplest square root algorithm that I know about. Well, I know how pesky students suffer when trying to computer square roots, so I had to choose an easy one.

I know about the famous Cramack method but this sounds complicated to me (though it's the most efficient method AFAIK). So I preferred the goody (Guess, Evaluate, Improve) method. So for example if I want to calculate the square root of the number let's say, 2, my strategy will be like the following:

1- Pick a guess (1 for example)
2- Evaluate that guess: is 1 * 1 near enough to 2 (see the bold near enough expression!)
3- If the guess is not near enough, Improve it.

Note that I didn't use the term "equals" when evaluating the expression, rather I used the term "near enough" and this is because, with numbers that are not full squares (like 2 for example), the square root is going to be a floating point number (square root of 2 = 1.414213562373095). Now if you multiply this number --the square root of 2-- by itself, you should expect to have 2 as an answer. Well, that's not gonna happen, rather you're going to get (2.0000000000000004). This is happening due to the way that computers handle floating point numbers --which is a topic of other series of posts by itself. In this case if you used the term equals (or == operator) when evaluating the guess you will never make it, and your program will not halt. That's why I carefully used the term near enough. Defining what near enough means is your choice now, for certain situations you might consider a number that is less than the square root by 0.1 is near enough or 0.001 or 0.0001, etc. This depends on the level of precision that you need for your specific scenario.

The implementation:
Here's a python implementation of this algorithm.

```## calculates the square root of a positive number
## with the passed in precision
def sqrtWithPrecision(x, precision):
## The precision must be greater than zero,
## root must be a positive number
assert(precision > 0, str(precision), 'is not a valid value, it must be a positive integer')
assert x > 0, 'root can not be a negative number'
low = 0
high = max(x, 1)
counter = 0
guess= (low + high) /2.0
while abs (guess ** 2 -x) > precision and counter <= 100:
if(guess ** 2 < x):
low = guess
else:
high = guess
guess = (low + high) / 2.0
counter += 1
assert counter <= 100, '100 iterations done and no good answer'
## printing the number of iterations,
## in productive code, you might want to remove the next line
print 'Num of iterations:', counter, 'Estimate:', guess
return guess```

And here's a c# example:

```float sqrtWithPrecision(float x, float precision)
{
if (x < 0 || precision <= 0)
throw new ArgumentException("x, and precission gotta be non negative numbers");
float low = 0;
float high = Math.Max(1f, x);
float guess = (low + high)/2f;
int counter = 0;
while( Math.Abs(guess * guess - x) > precision && counter <= 100)
{
if (guess * guess < x)
low = guess;
else
high = guess;
guess = (low + high)/2f;
counter++;
}
if(counter > 100)
throw new Exception("100 iterations done with no good answer");
Console.WriteLine("Num of Iterations: {0} , estimate: {1}", counter, guess);
return guess;
}```

Packetman / человекпакет said...

Version written in haskell. Looks fine by me :) And a bit shorter

approx n (g', g'') = if square guess > n
then (g', guess)
else (guess, g'')
where guess = (g' + g'') / 2
square x = x * x

root x = fst . last . takeWhile (\(g', g'') -> g'' - g' > 0.00001) \$ iterate (approx x) (0, x)

Packetman / человекпакет said...

With this code we can approximate any non linear function without extremums in range of work.

approx f n (g', g'') = if f guess > n
then (g', guess)
else (guess, g'')
where guess = (g' + g'') / 2

solve f x = fst . last . takeWhile (\(g', g'') -> g'' - g' > 0.00001) . iterate \$ (approx f x) (0, x)

square x = x * x
square3 x = x * x * x
square4 = square . square
sin' x = sin \$ x + 0.3

solve square 2
solve square3 12
solve square4 20
solve sin' 1 -- asin
... etc