A few maths related C programming q's

Joined
5 Aug 2006
Posts
11,418
Location
Derbyshire
In C (NOT C+/C++!!), how do I do square root of a number?

Also how do I do powers. At the mo if I need d^3 I just do d*d*d.

I have the ability in C to do equations, and have done a few ones that work fine, I am just not sure on the above.
thanks
 
#include <math.h> ? :)

sqrt and pow are the functions you're looking for although IIRC the "^" operator also works in C.

ps. there's a Programming forum for this sort of thing ;)
 
oh yea just noticed on the wrong section, oops.
At the top I have #include <stdio.h> iirc for all my programs and the other programs that do equations work ok afaik.
where would I put sqrt?? Say I had declared a double called A, before the fprintf line I would put A = sqrtA in order to assign the result back into A? Or I could just call it B or something if I had delared B as well at the start.
 
oh yea just noticed on the wrong section, oops.
At the top I have #include <stdio.h> iirc for all my programs and the other programs that do equations work ok afaik.
where would I put sqrt?? Say I had declared a double called A, before the fprintf line I would put A = sqrtA in order to assign the result back into A? Or I could just call it B or something if I had delared B as well at the start.

RTFM!

man sqrt
man pow

:p
 
or you could write your own root and power functions, which might not be a bad idea as you'll learn more.

for the power function, loops or recursion.
for root, maybe an approximation with guess/divide/average method.
 
Back
Top Bottom