# Square Root of a number

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 07-30-2008
freddyvorhees
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; }```
• 07-30-2008
matsp
Can you describe HOW it doesn't work.

--
Mats
• 07-30-2008
freddyvorhees
Doesnt print the "guess".
• 07-30-2008
matsp
Is is possible that
Code:

`while (abs((guess*guess)-x) != .0000000000000000001)`
never gets fulfilled?

--
Mats
• 07-30-2008
freddyvorhees
So whats wrong? is it the syntax or the algorithm?
• 07-30-2008
grumpy
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.
• 07-30-2008
Cpro
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.
• 07-30-2008
freddyvorhees
Right!
• 07-30-2008
matsp
Quote:

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
• 07-30-2008
freddyvorhees
noted,but still noting
• 07-30-2008
freddyvorhees
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; }```
• 07-30-2008
Waterbottle
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.
• 07-30-2008
abachler
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;```
• 07-30-2008
m37h0d
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.
• 07-30-2008
freddyvorhees
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
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last