# Thread: Square Root of a number

1. ## Square Root of a number

Doesnt work, what do I need to change?

Code:
```#include <iostream>
using namespace std;

double U;
double L = 0;
double guess;

double abs(int a,int b)
{
if ((a-b) < 0)
{
return (a-b)*-1;
}
else
{
return (a-b);
}
}
double sqrt(double x)

{
if (x == 0 || x == 1)
{
return x;
}
else
{
guess = x/2;
}
while (abs((guess*guess)-x) != .0000000000000000001)
if (guess*guess > x)
{
U = guess;
guess = ((U - L)/2) + L;
}
else
{
L = guess;
guess = ((U - L)/2) + L;
}
cout<<guess;
}```

2. Can you describe HOW it doesn't work.

--
Mats

3. Doesnt print the "guess".

4. Is is possible that
Code:
`while (abs((guess*guess)-x) != .0000000000000000001)`
never gets fulfilled?

--
Mats

5. So whats wrong? is it the syntax or the algorithm?

6. It's the convergence criterion.

The thing to remember is that floating point types cannot represent any arbitrary value: there is a smallest positive value that can be represented in a floating point variable. If your 0.000 (many zeros) 001 is less than that smallest representable value, then your algorithm may never satisfy your convergence criterion.

Out of curiosity, why not use the sqrt() function from <cmath>?

Incidentally, if you're not printing your guess, the function is never returning either ..... you have an infinite loop.

7. Code:
`while (abs((guess*guess)-x) != .0000000000000000001)`
I don't think your abs function is being called (correctly anyways), because you are only passing one argument.
I could be wrong, but in the test I ran:
Code:
```int main()
{
sqrt(9);

return 0;
}```
it wasn't being called.
However, I'm not sure why I wasn't receiving any errors when I compiled.

8. Right!

9. Originally Posted by freddyvorhees
Right!
a) Those are not integers.
b) Once they have turned into integers, how can the difference from subtracting a-b be a floating point value?

--
Mats

10. noted,but still noting

11. This is another version, it also doesnt work,prints nothing

Code:
```double abs(int a,int b)
{
if ((a-b) < 0)
{
return (a-b)*-1;
}
else
{
return (a-b);
}
}
double sqrt(int x)
{
double U = x;
double L = 0;
double guess = 0;

if (x == 0 || x == 1)
{
return x;
}

while (abs((x/2*x/2),x) > .001)
guess = (x/2);
if (guess*guess > x)
{
guess = (U - L)/2;
}
else
{
L = x;
guess = (U - L)/2;
}
return x/2;
}```

12. Code:
```while (abs((x/2*x/2),x) > .001)
guess = (x/2);```
You don't change x in the loop, so the loop never ends.

13. The problem is the above mentioned convergence problem. You need to increase 0.0000~001 or change the line

Code:
`while (abs((guess*guess)-x) != .0000000000000000001)`
to

Code:
`while (abs((guess*guess)-x) != 0.000001)`
First get it working, then worry abotu being acurate.

Also, I noticed you dont have a main(), is what you posted all the code? There may be problems in how you are calling the function.

On secodn thought just replace that whoel section with -

Code:
`while (abs((guess*guess)-x) > .00000001) guess = (guess + x/guess)/2;`

14. you always check that your error is GREATER THAN OR EQUAL TO your error tolerance; not exactly equal to.

the odds of it hitting that exact number are infinitesimal.

15. Working now but results are inaccurate:
Code:
```#include <iostream>
using namespace std;

double abs(int a,int b)
{
if ((a-b) < 0)
{
return (a-b)*-1;
}
else
{
return (a-b);
}
}
double sqrt(double x)

{double U = x;
double L = 0;
double guess= x/2;
if (x == 0 || x == 1)
{
return x;
}
else
while (abs((guess*guess),x) > .000000000001)
if (guess*guess > x)
{
U = guess;
guess = ((U - L)/2) + L;
}
else
{
L = guess;
guess = ((U - L)/2) + L;
}
return guess;
}
int main()
{
cout<<"Root ";
cout<<sqrt(9);
}```
prints 3.09375