Thread: code to find th square root of a number

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    1

    Unhappy code to find th square root of a number

    I'm a newbie and I'm writing a program preform Pythagoras’ theorem but I cant work out the code to square a number.
    If anyone out there knows it please help me.
    Thanks

  2. #2
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Ehh... I think it's in math.h header, and I think it was something like... sqroot(), but probably not, try searching "Square Root in C++" or something on Google
    Currently research OpenGL

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    It's sqrt for doubles, sqrtf for a double and sqrtl for a long double...
    And they reside in the "cmath" header.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by EVOEx View Post
    It's sqrt for doubles, sqrtf for a double and sqrtl for a long double...
    And they reside in the "cmath" header.
    Correction: sqrtf() would be the function for float.

    Although in C++ I think there are wrappers to handle float, double and long double under a single name.

    --
    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 C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    Thumbs up

    Code:
    template<class T)
    class T mySqrt(T x, double precision)
    {
        if (x <= 0) return 0;
        T r = x;
        while (x - x/r > precision)
            r = (r + x/r) / 2;
        return r;
    }
    or for obvious reasons just use the ready sqrt() in math.h which handles double, long double and float http://www.cplusplus.com/reference/c...math/sqrt.html

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    or...

    Code:
    double Number;
     
    __asm {
       FLD     Number
       FSQRT Number
       FSTP   Number
       }
    which is far faster than calling the sqrt() function

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I find the hypot() function useful. By the way, depending on what you are doing, its usually optimal to NOT square root your result and just always work with (x^2). It makes collision detection algorithms work oh so much faster.

  8. #8
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49
    both the functions are in cmath header file.
    to find the squre root
    Code:
    cout<<sqrt(variable);
    to find the square
    Code:
    cout<<pow(a,2);
    I would rather be hated for who I am than be loved for who I am not!

  9. #9
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Sorry, a little OOT. After some thorough browsing, I found this snippet:

    Code:
    float InvSqrt (float x)
    {
        float xhalf = 0.5f*x;
        int i = *(int*)&x;
        i = 0x5f3759df - (i>>1);
        x = *(float*)&i;
        x = x*(1.5f - xhalf*x*x);
        return x;
    }
    FYI, it is Quake's inverse square root. But I sure can't understand it clearly. Especially the " i = 0x5f3759df - (i>>1);" part. I think it's much faster than the cmath's version.

    EDIT: My mistake. You needed square and square root. Not inverse square root.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  10. #10
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    But I sure can't understand it clearly. Especially the " i = 0x5f3759df - (i>>1);"
    0x5F3759DF

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    That was very insightful, and surprising to me.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Very interesting, Bob. Where on earth did you dig that up from?

  13. #13
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Quote Originally Posted by master5001 View Post
    Very interesting, Bob. Where on earth did you dig that up from?

    From Chris Lomont's home page. Unfortunately, the link to his papers from his home page has some isues at times. So, I linked directly to the PDF.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by abachler View Post
    or...

    Code:
    double Number;
     
    __asm {
       FLD     Number
       FSQRT Number
       FSTP   Number
       }
    which is far faster than calling the sqrt() function
    And far less portable. Not to mention that, with the right switches (e.g. -ffast-math in GCC), your compiler might just optimize the sqrt call to the FSQRT instruction anyway. Which wouldn't break the compiler's register management code like the ASM snippet does. Or, in a loop, the compiler might parallelize a sqrt algorithm into a SIMD implementation with four times the throughput of the x87 (and a non-braindead architecture).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by BobS0327 View Post
    Thanks.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer confusion
    By Blackroot in forum C++ Programming
    Replies: 11
    Last Post: 09-12-2007, 12:44 AM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Finding the square root! Not Working!
    By Lah in forum C Programming
    Replies: 5
    Last Post: 09-14-2003, 07:28 PM
  4. Square Root
    By Kyoto Oshiro in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2002, 01:22 AM
  5. can anyone find the problem in my code
    By ArseMan in forum C++ Programming
    Replies: 2
    Last Post: 09-20-2001, 09:02 PM

Tags for this Thread