Without Using Pow Function In C

Welcome. Please Login, Register, Or Activate! 
type your username and password to login
Date: November 24, 2009, 10:54 AM
431747 members and 298742 Topics
Latest Member: ketSmeamN
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 1392 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, cant it?
c0dec (m)
Re: Without Using Pow Function In C
« #7 on: June 09, 2006, 07:19 PM »

exp() returns a double.
judy4all
Re: Without Using Pow Function In C
« #8 on: November 30, 2008, 05:53 AM »

The easiest way to do it is to call a built in calculator with the expression of your choixe,  or an online calculator,  for example,

http://www.google.com/search?q=5^6

will give you a google page with the answer or 5^6, 

In c,  you can call a get on any website of your choice,  eg,  http://www.google.com/search?q=5^6,   and 'get' will give you all the text derived from the query,  you can then use 'sed' to search the expression for the answer to your question,  say if u need sample code doing this,

 Java World: Questions And Solutions (java Only Pls)  The Importance Of Software Testing And Not Just Software Programming  Difference Between Db_flashback_retention_target And Retention Policy  Page 2
Pages: (1) Go Up Send Topic to Friend by E-mail Reply 


Sections: Autos/Cars (2) Jobs/Vacancies (2) (3) Career Talk Education General(2) Politics Romance Computers Phones Travel
Sports Fashion Health Religion Celebrities TV/Movies (2) Music/Radio (2) Books Webmasters Programming

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

Nairaland is owned by Oluwaseun Osewa. See also: Nairalist Classified Ads
Nairaland Forum | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.