Without Using Pow Function In C

A Member? Please Login  
type your username and password to login
Date: October 14, 2008, 09:17 AM
249646 members and 148326 Topics
Latest Member: marveli
Nairaland [Nigerian Forum] Home Help Search Who is currently online? Login Register
Nairaland Forum  |  Technology  |  Programming  |  Without Using Pow Function In C
Pages: (1) Go Down Send this topic Notify of replies
Author Topic: Without Using Pow Function In C  (Read 379 views)
Sherlin
Without Using Pow Function In C
« on: June 02, 2006, 04:52 AM »

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)
Re: Without Using Pow Function In C
« #1 on: June 02, 2006, 11:17 AM »

If it's an integer, I can help.  But raising to the power of a floating point number?  No idea!
c0dec (m)
Re: Without Using Pow Function In C
« #2 on: June 05, 2006, 08:46 AM »

for integers this should work:
Code:
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:
Code:
double powf(float base, float exponent)
{
return exp(log(base) * exponent);
}
c0dec (m)
Re: Without Using Pow Function In C
« #3 on: June 05, 2006, 09:05 AM »

oh and if log(x) doesn't produce what u need, try log10(x).
Seun (m)
Re: Without Using Pow Function In C
« #4 on: June 05, 2006, 10:44 AM »

Thanks for sharing.  Didn't feel that the use of the log finction would be allowed.  Undecided
c0dec (m)
Re: Without Using Pow Function In C
« #5 on: June 05, 2006, 05:46 PM »

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)
Re: Without Using Pow Function In C
« #6 on: June 09, 2006, 04:08 PM »

Quote
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)
Re: Without Using Pow Function In C
« #7 on: June 09, 2006, 07:19 PM »

exp() returns a double.
 Ms-cobol 4.5 Or 5.0  How Can I Become A Good Web Programmer (esp If I Dont Have Aptech Bucks)  Networking And Programming  Page 2
Pages: (1) Go Up Send Topic to Friend by E-mail Reply 
Google
 
Web www.nairaland.com
Sections: TV/Movies (2) Music/Radio (2) Celebrities Job Talk Jobs/Vacancies (2) Career Talk Romance Books Politics Sports Fashion Travel
Health Schooling Religion General(2) Business Webmaster Programming Computers Phones Cars & Trucks

Links: Page1 Page2 Page3 Page4 Page5 Page6 Page7 Page8 Page9 Page10

Nairaland is owned by Oluwaseun Osewa
Nairaland Forum | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.