![]() |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 1
| If anyone out there knows it please help me. Thanks |
| CODY21 is offline | |
| | #2 |
| Hail to the king, baby. Join Date: Oct 2008 Location: Faroe Islands
Posts: 718
| 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 |
| Akkernight is offline | |
| | #3 |
| Registered User Join Date: Oct 2008
Posts: 528
| It's sqrt for doubles, sqrtf for a double and sqrtl for a long double... And they reside in the "cmath" header. |
| EVOEx is offline | |
| | #4 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
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. | |
| matsp is offline | |
| | #5 |
| Registered User Join Date: Jun 2008
Posts: 1,274
| 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;
}
|
| C_ntua is offline | |
| | #6 |
| Malum in se Join Date: Apr 2007
Posts: 3,188
| or... Code: double Number;
__asm {
FLD Number
FSQRT Number
FSTP Number
}
__________________ Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off. |
| abachler is offline | |
| | #7 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| 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. |
| master5001 is offline | |
| | #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); Code: cout<<pow(a,2);
__________________ I would rather be hated for who I am than be loved for who I am not! |
| Saimadhav is offline | |
| | #9 |
| In the Land of Diddly-Doo Join Date: Jul 2006
Posts: 381
| 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;
}
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. |
| g4j31a5 is offline | |
| | #10 | |
| Registered User Join Date: Mar 2005 Location: Mountaintop, Pa
Posts: 1,059
| Quote:
| |
| BobS0327 is offline | |
| | #11 |
| Registered User Join Date: Nov 2005
Posts: 634
| That was very insightful, and surprising to me. |
| Raigne is offline | |
| | #12 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| Very interesting, Bob. Where on earth did you dig that up from? |
| master5001 is offline | |
| | #13 | |
| Registered User Join Date: Mar 2005 Location: Mountaintop, Pa
Posts: 1,059
| Quote:
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. | |
| BobS0327 is offline | |
| | #14 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| 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 |
| CornedBee is offline | |
| | #15 | |
| In the Land of Diddly-Doo Join Date: Jul 2006
Posts: 381
| Quote:
__________________ ERROR: Brain not found. Please insert a new brain! “Do nothing which is of no use.” - Miyamoto Musashi. | |
| g4j31a5 is offline | |
![]() |
| Tags |
| c++ code, code, square root |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pointer confusion | Blackroot | C++ Programming | 11 | 09-12-2007 12:44 AM |
| Issue w/ Guess My Number Program | mkylman | C++ Programming | 5 | 08-23-2007 01:31 AM |
| Finding the square root! Not Working! | Lah | C Programming | 5 | 09-14-2003 07:28 PM |
| Square Root | Kyoto Oshiro | C++ Programming | 5 | 09-05-2002 01:22 AM |
| can anyone find the problem in my code | ArseMan | C++ Programming | 2 | 09-20-2001 09:02 PM |