Im wondering how do I get the square root of a number in C. Can any body help. Thanx
Printable View
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
Example:
-LukeCode:#include <stdio.h>
#include <math.h>
int main()
{
printf(sqrt(20));
return 0;
}
> 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.
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.
haha. no.
This is my favourite math reference:
http://mathworld.wolfram.com/SquareRootAlgorithms.html