Im wondering how do I get the square root of a number in C. Can any body help. Thanx
This is a discussion on square root function within the C Programming forums, part of the General Programming Boards category; Im wondering how do I get the square root of a number in C. Can any body help. Thanx...
Im wondering how do I get the square root of a number in C. Can any body help. Thanx
>Im wondering how do I get the square root of a number in C.
The easiest way would be the sqrt() function in math.h.
-Prelude
My best code is written with the delete key.
Example:
-LukeCode:#include <stdio.h> #include <math.h> int main() { printf(sqrt(20)); return 0; }
Asking the right question is sometimes more important than knowing the answer.
Please read the FAQ
C Reference Card (A MUST!)
Pointers and Memory
The Essentials
CString lib
> printf(sqrt(20));
First parameter to printf is a string
printf( "%f\n", sqrt(20));
You could also easily write your own by using Newton's method of successive approximations.
'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition
or use a very simple ( time consuming ) method:
use a loop using long doubles and do a comparison of number * number to equal the desired number. a very basic approach. this will have some trouble with some numbers, though. didn't test it yet.
- toaster
>use a loop using long doubles and do a comparison of number * number to equal the desired number. a very basic approach.
While your at it, you could reinvent the wheel.
'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition
haha. no.
This is my favourite math reference:
http://mathworld.wolfram.com/SquareRootAlgorithms.html