# Calculating Square Root?

• 05-24-2007
Neo1
Calculating Square Root?
I've been messing about with some programs to calculate some basic math for me, but i've hit a brick wall with Square Root. Obviously, i could just use the sqrt(); from cmath, but i want to try and do it manually using some kind of formula or method rather than just using a premade function.

I've been browsing around for a formula for computing sqrt, and Wikipedia has a couple.

http://en.wikipedia.org/wiki/Square_Root

They're kind of complicated and i have a hard time understanding most of them. I did try the one with the logarithm, sqrt(x) = 10.5^log(x), but that gave me mostly random results, and definitely not the sqrt.

What i have now is not the prettiest way of doing it. This program actually succeeds in trying every combination of digits until it hits the correct one, but it only works with integers, so if the input to the program isn't something like 9 or 16, it just keeps on calculating numbers forever.

Code:

```#include <iostream> #include "clscr.h" float Square(float a); int main() {     float a = 0, b = 0;     std::cout << "Square Root (a)" << std::endl;     std::cout << "Enter a: ";     std::cin >> a;     clscr();     b = Square(a);     std::cout << "Square Root (" << a << ") = " << b << std::endl;     std::cin.ignore(2); } float Square(float a) {       int i = 0;       float b = 1;       while (1)       {             if ((b*b) == a)             {                       return(b);             }             else             {                 b++;             }       } }```
A very ugly way of doing it - i know. Is there a simple formula i have missed? How does the sqrt(); in cmath do it? I need it to work with non-integers aswell. As you might have noticed, im not very good at maths and long formulas, please be gentle :)
• 05-24-2007
laserlight
There was a recent contest on computing square roots with restriction on division. Without (and with) this restriction one idea is to implement the Newton-Raphson method.
• 05-24-2007
robatino
http://en.wikipedia.org/wiki/Methods...g_square_roots

The Babylonian method (essentially Newton's method for square roots) is probably the one you should be using. There's not much point in using logs to compute square roots since log() is more complicated to compute than sqrt(), so if you're not willing to call sqrt() then it doesn't make sense to call log() either.
• 05-24-2007
hk_mp5kpdw
Quote:

Originally Posted by Neo1
I did try the one with the logarithm, sqrt(x) = 10.5^log(x), but that gave me mostly random results, and definitely not the sqrt.

Well, the way you've written it here is wrong. It is not 10.5 raised to the power of log(x) but rather 10 raised to the power of (.5 times log(x)). Additionally, what log function did you use? The formula works off of base 10 log which would be the log10 function and not the other log function. If you just used the log function then you used the wrong one.

In code and written as a function this would be:
Code:

```double SquareRoot(double val) {     return pow(10.0,0.5*log10(val));  // 10 raised to the power (0.5 * log10(val)) }```
Results from this match the return value of the sqrt function in my test program.
• 05-24-2007
prog-bman
Sorry if I am crazy, but can't you just raise the value to 1/2(.5)

Code:

```#include <iostream> #include <cmath> const double VICINITY = 0.0001; double SquareRoot(double val) {         return pow(val, .5); } int main() {         //Grab the square roots         double customRoot = SquareRoot(10.0);         double libRoot = sqrt(10.0);                //Calc the difference between the two values         double valueDifference = fabs(libRoot - customRoot);         //Used to work around floating point         if(valueDifference < VICINITY){                 std::cout<<"The Roots Match";         }//if         else{                 std::cout<<"The Roots Are Different, By This Value: "<<valueDifference;         }//else         //Wait for user input         std::cin.get();         return 0;         }```
• 05-25-2007
iMalc
Basically you do this:
Code:

```float SquareRoot(float a) {     float c, b = a;     do {         c = b;         b = (a/b + b) * 0.5f;     } while (fabs(c - b) < EPSILON);     return c; }```