|
Sherlin
|
Hi,
Can anyone help me to find the power of a number without using pow function in C. For eg., 2.5 raised to the power of 1.5.
Thanks.
|
|
|
|
|
|
Seun (m)
|
If it's an integer, I can help. But raising to the power of a floating point number? No idea!
|
|
|
|
|
|
c0dec (m)
|
for integers this should work: int powi(int number, int exponent) { int i, product = 1; for (i = 0; i < exponent; i++) product *= number;
return product; } for floats, well i don't have time now to test it but i guess this should work: double powf(float base, float exponent) { return exp(log(base) * exponent); }
|
|
|
|
|
|
c0dec (m)
|
oh and if log(x) doesn't produce what u need, try log10(x).
|
|
|
|
|
|
Seun (m)
|
Thanks for sharing. Didn't feel that the use of the log finction would be allowed. 
|
|
|
|
|
|
c0dec (m)
|
no problem. i didn't think log was allowed but i didnt see any other way of solving it except some taylor series expansion wahala.
|
|
|
|
|
|
pfowighz (m)
|
double powf(float base, float exponent) Shudnt that be float powf(float base, float exponent) The return val cud be a floating point stuff now, can't it?
|
|
|
|
|
|
c0dec (m)
|
exp() returns a double.
|
|
|
|
|
|