Thread: Square Root of a number

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    91

    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. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you describe HOW it doesn't work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Doesnt print the "guess".

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is is possible that
    Code:
    while (abs((guess*guess)-x) != .0000000000000000001)
    never gets fulfilled?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    So whats wrong? is it the syntax or the algorithm?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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. #7
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    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.
    IDE - Visual Studio 2005
    Windows XP Pro

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Right!

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by freddyvorhees View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    noted,but still noting

  11. #11
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    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. #12
    Registered User
    Join Date
    Oct 2007
    Location
    Norway
    Posts
    16
    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. #13
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    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;
    Last edited by abachler; 07-30-2008 at 08:53 AM.

  14. #14
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    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. #15
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  2. Finding the square root! Not Working!
    By Lah in forum C Programming
    Replies: 5
    Last Post: 09-14-2003, 07:28 PM
  3. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM
  4. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM
  5. square root
    By help in forum C Programming
    Replies: 5
    Last Post: 08-29-2001, 05:46 AM