Thread: Square Root of a number

  1. #31
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    whereas teh mothod I suggested does find 0 < x < 1 for all positive real numbers.

  2. #32

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    By the way, ((U - L)/2) is the same thing as (U + L) / 2.
    Or, better yet, (U + L) >> 1.

    EDIT:
    didn't realize they were doubles
    Last edited by rudyman; 07-30-2008 at 04:44 PM.

  3. #33
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Are you sure bitshifting works with doubles, though?
    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).

  4. #34
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by anon View Post
    Are you sure bitshifting works with doubles, though?
    It doesnt. well, at least not as a fast divide by 2^N.

  5. #35
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're trying to calculate the square root by using the Newton-Raphson method by the looks of it. The problem is that you got the formula wrong. Go back and look it up again!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #36
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by iMalc View Post
    You're trying to calculate the square root by using the Newton-Raphson method by the looks of it. The problem is that you got the formula wrong. Go back and look it up again!
    I think it's less Newton-Raphson and more binary search. (I guess "bisection" is the more proper term in this context.)

  7. #37
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Heck I see; you're right. That's horrible!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #38
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    If you want to get really fancy with square roots, here's a link describing how to calculate square roots by hand:

    http://www.geocities.com/cnowlen/Cat...Squareroot.htm

    I once implemented this in a class that does arithmetic on arbitrary length integers. Granted, it wasn't as efficient as the gmp implementation, but it's still cool to learn how to do.

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