CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2006
    Posts
    13

    Cool Manual Square Root Function...??

    This is a simple Integer based Manual Square root function that i discovered on google ...

    unsigned char sqrt(unsigned int N) {
    unsigned int x,j;

    for (j = 1<<7; j<>0; j >>= 1 )
    {
    x = x + j;
    if (x*x > N )
    x = x - j;
    }
    return(x);
    }

    COuld someone please explain the logic behind this function? I don't quite understand what the << and >> does??? Also, what is "unsigned" ? THe only data types im familiar with at the time is int, char, string, double, and void???? lol, thanks in advance .

  2. #2
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Re: Manual Square Root Function...??

    Well the unsigned int just means the function will not take negative integers as an argument, or return a negative integer.

    Most numerical data types in C++ have an unsigned counterpart; it just means it is not negative.

    For example...
    int -------- unsigned int
    long -------- unsigned long
    short -------- unsigned short
    etc.
    etc.

    This makes sense because you cannot take the square root of a negative number.

    Well, you can, but then you start dealing with imaginary numbers so we won't even go there
    Last edited by dcjr84; May 2nd, 2006 at 03:50 AM.

  3. #3
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Manual Square Root Function...??

    Quote Originally Posted by dcjr84
    For example...
    double -------- unsigned double
    "unsigned double"? That's something new...
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Manual Square Root Function...??

    Quote Originally Posted by usnsailorjeoff
    ...COuld someone please explain the logic behind this function? I don't quite understand what the << and >> does???...
    This is simple trial-and-error method: try adding various powers of 2 to the test value and check if it is over the target value - then subtract that last value.
    >> and << are binary shift operators (right and left).
    >>= - shift with assignment, j >>= 1 is the same as j = j >> 1
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  5. #5
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Re: Manual Square Root Function...??

    Quote Originally Posted by VladimirF
    "unsigned double"? That's something new...
    Yeah, I don't know what I was thinking there
    I was just trying to get my point across about the
    non-negatve values, it must have slipped.

  6. #6
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Manual Square Root Function...??

    the logic is use of binary arithmetic and "trial and error".

    The input value is 16-bit. So the maximum square root is 1111111

    For each of the 7 bits, try adding it and square the number. If we're too high, remove the bit again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured