Thread: Square Root of a number

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because you are converting your double value to a float, so as soon as you get close enough to 3*3 is roughly 9, it will retrun 9-9 == 0 from your abs function [which by the way is a bad name, I'd call it absdiff or some such].

    --
    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.

  2. #17
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Actually he seems to be converting doubles to int's - even worse.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by anon View Post
    Actually he seems to be converting doubles to int's - even worse.
    Yes, that's actually what I meant to type - somehow the brain-to-finger interface obviously is a bit faulty sometimes... ;-)

    --
    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.

  4. #19
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    It's a bad idea to use the same names as standard library functions. There are functions named sqrt() and abs() in <cmath>. It was pointed out earlier, but it's worth restating that it's a VERY bad idea to use == or != with floating point numbers as it will produce inconsistent results.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #20
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    still has warnings

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code, and what are the current warnings?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Got it working

    Code:
    #include <iostream>
    using namespace std;
    
    
    double absDiff(double a,double b)
    {
    	if ((a-b) < 0)
    	{
    		return (a-b)*-1;
    	}
    	else
    	{
    		return (a-b);
    	}
    }
    double sqrtNum(double x)
    {
    double U = x;
    double L = 0;
    double guess = x/2;
    	if (x == 0 || x == 1)
    	{
    		return x;
    	}
    	else 
    		while (absDiff((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(5)<<endl;
    }
    Any improvements needed?

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You may want to do something special, in case someone decides to type sqrtNum(-1). (And notice, that you forgot to change your call in main to your function as opposed to the library function.)

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Any improvements needed?
    You could use abs() from <cmath> instead of implementing abdDiff(), but if your goal is to avoid using stuff from <cmath> entirely then change (a-b)*-1 to (b-a). You could also pull guess = ((U - L)/2) + L; to after the if and else blocks. Oh, and your indentation could be more consistent.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    What do you mean by pulling guess = ((U - L)/2) + L?

  11. #26
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by freddyvorhees View Post
    What do you mean by pulling guess = ((U - L)/2) + L?
    You do it in the if-part; you do it in the else-part. So why not just do it, outside of the if statement? (Other than "I don't want to fiddle with all the braces".)

  12. #27
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    ahhhhhh

  13. #28
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Maybe like this?
    Code:
    	if (x == 0 || x == 1)
    	{
    		return x;
    	}
    	else 
    		while (absDiff((guess*guess),x) > .0000001)
    		{
    		if (guess*guess > x)
    			U = guess;
    		else 
    			L = guess;
    		}
    		guess = ((U - L)/2) + L;
    		return guess;

  14. #29
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Code:
    	if (x == 0 || x == 1)
    as stated several times, don't compare 'double' values with integer values. The condition is not correct.

  15. #30
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Oh, and your initial guesses implicitly assume that 0 < sqrt(x) < x; this is not true if 0< x < 1. For instance, sqrt(0.25) = 0.5, and you won't find those numbers.

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