SQRT Mystery...

This is a discussion on SQRT Mystery... within the C Programming forums, part of the General Programming Boards category; I would like to know exactly how sqrt() works. Either in assembly or C, doesn't matter. What I need it ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    42

    SQRT Mystery...

    I would like to know exactly how sqrt() works. Either in assembly or C, doesn't matter. What I need it for, is for my Controller on our schools robot. It doesn't have any sqrt function, and I would write one if I knew how it worked. Can anybody help?
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Does it have a pow() function? If it does, you could just do pow( x, 0.5 ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Does the compiler have float/double arithmetic, or only ints?

    Dave

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Look up Newton-Raphson eg. here:

    http://www.sosmath.com/calculus/diff/der07/der07.html

    DavT
    DavT
    -----------------------------------------------

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    23
    This code is in JAVA. I never wrote the C equivalent, but it should help you understand the math behind finding the square root of a number. This function finds the sqrt with .1 proximity.

    Code:
    public static double SquareRT(int num)
    {
    	int cnt, x;
    	double sqrt, tmp;
    		
    	for (cnt = 0; (cnt*cnt) <= num; cnt++); 
    		
    	if ((cnt*cnt) == num)
    		return cnt;
    	if ((cnt*cnt) > num) 
                    /* now I'm pretty sure you need this unless 
                      you changed the 'for' condition to (cnt*cnt) < num */
    		cnt--;
    	
    	tmp = num - (cnt*cnt); /*find the difference of two squares. */
    	x = cnt * 2;/* length of formed rect. (inaccurate) */
    	sqrt = tmp / x; /* the width of rect. */
    	sqrt += cnt; /* add the width toe cnt */
    	
    	return sqrt;
    }
    Sorry it has no comments: My laziness. In short, it finds the closest perfect square to the number you want to find the sqrt for and then takes away perfect square from the whole thing.
    To understand the rest, you need to imagine a square with the area num. cnt will be a number whose square is closest to num.(ie. if num is 30, cnt is 5). Then you take away from num the square of cnt.(ie. 30 -25). If you can picture this, you'll see that a little strip would be left around the original square. If you make a rectangle from this strip, your rectangle will be 2*cnt in length and the area is the difference of the two areas (ie. the 30 - 25 = 5). You can find the width and add it to the cnt.
    The inaccuracy comes in when you assume that 2 *cnt is the length of the rectangle you formed. You'll actually miss a little bit. I might actually try to see if I can fix the inaccuracy, but it's already accurate enough for most applications.
    The 'if' statement is just overly-cautious just because when I wrote it I didn't exactly understand how the for loop worked.
    Just copy the important parts - which is most of the code. Better yet, if you understand how it workes, write your own.

    I desparately hope that is what you were asking or I just wasted a lot of time.

    edit : added the math explanation of how to calculate the sqrt.
    edit2: added comments to code.
    Last edited by Cikotic; 03-22-2004 at 10:43 AM.
    If pointers have made you suicidal, you're not alone. But there is no need to hurt yourself. There are people who are willing to help. Just call your local C Crisis Centre and talk to the professtionals there. If you don't have a C3 in your neighborhood, go to the global C3 at www.cprogramming.com . If you're still suicidal, don't lose hope. Gently close your book on C and throw it in the fireplace. If you live in a tower, you can throw it out the window. There, doesn't that feel better?

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    18

  7. #7
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    Munich, Germany
    Posts
    224
    If you want to have your own algorithm, just use the BINOMIAL THEOREM

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    If you want to have your own algorithm, just use the BINOMIAL THEOREM
    That is a joke right?
    DavT
    -----------------------------------------------

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. undefined reference to sqrt
    By shamma in forum C Programming
    Replies: 1
    Last Post: 05-09-2009, 01:42 PM
  2. sqrt() function help
    By willc0de4food in forum C Programming
    Replies: 5
    Last Post: 03-14-2005, 08:07 PM
  3. Mystery.
    By Nutshell in forum C Programming
    Replies: 1
    Last Post: 01-27-2002, 12:41 AM
  4. how sqrt ?
    By sambs1978 in forum C Programming
    Replies: 3
    Last Post: 09-20-2001, 08:14 AM
  5. The great mystery
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 39
    Last Post: 08-15-2001, 08:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21