# Thread: code to find th square root of a number

1. ## 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.
Thanks

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

3. It's sqrt for doubles, sqrtf for a double and sqrtl for a long double...
And they reside in the "cmath" header.

4. Originally Posted by EVOEx
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

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

Code:
double Number;

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

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

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

10. But I sure can't understand it clearly. Especially the " i = 0x5f3759df - (i>>1);"
0x5F3759DF

11. That was very insightful, and surprising to me.

12. Very interesting, Bob. Where on earth did you dig that up from?

13. Originally Posted by master5001
Very interesting, Bob. Where on earth did you dig that up from?

14. Originally Posted by abachler
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).

15. Originally Posted by BobS0327
Thanks.