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.

11-05-2008

Saimadhav

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

11-05-2008

g4j31a5

Sorry, a little OOT. After some thorough browsing, I found this snippet:

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.

11-05-2008

CornedBee

Quote:

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