Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,213,993 members, 8,020,448 topics. Date: Wednesday, 04 December 2024 at 10:42 AM

Without Using Pow Function In C - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Without Using Pow Function In C (25087 Views)

Creating UWP Application In C# Or C++ (xaml) / [problem] Write A Program In C++ That Finds The Hcf Of 2 Numbers Without Using A Recursive Function / Is Main Function In C/c++ User Defined Function Or Predefined Function? (2) (3) (4)

(1) (Reply) (Go Down)

Without Using Pow Function In C by Sherlin: 4:52am On Jun 02, 2006
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.
Re: Without Using Pow Function In C by Seun(m): 11:17am On Jun 02, 2006
If it's an integer, I can help. But raising to the power of a floating point number? No idea!
Re: Without Using Pow Function In C by c0dec(m): 8:46am On Jun 05, 2006
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);
}
Re: Without Using Pow Function In C by c0dec(m): 9:05am On Jun 05, 2006
oh and if log(x) doesn't produce what u need, try log10(x).
Re: Without Using Pow Function In C by Seun(m): 10:44am On Jun 05, 2006
Thanks for sharing. Didn't feel that the use of the log finction would be allowed. undecided
Re: Without Using Pow Function In C by c0dec(m): 5:46pm On Jun 05, 2006
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.
Re: Without Using Pow Function In C by pfowighz(m): 4:08pm On Jun 09, 2006
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?
Re: Without Using Pow Function In C by c0dec(m): 7:19pm On Jun 09, 2006
exp() returns a double.
Re: Without Using Pow Function In C by judy4all: 5:53am On Nov 30, 2008
The easiest way to do it is to call a built in calculator with the expression of ur 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,
Re: Without Using Pow Function In C by holusormi(m): 7:08am On Dec 11, 2009
Sori guyz plz dont crucify me coz am kinda new to c programming,and am even learning it maself.but i was wondering why u couldnt use the exponentation (^) operator.i guess it shuld work.
Re: Without Using Pow Function In C by Cockyzy(m): 2:07pm On Dec 11, 2009
This show work. main() { float a=2.15,b=1.5; power(&a,&b);} void power(float *x,*y) { float i=1.0,val=1.0; while(i++<=*y) val=val * *x; printf(" %f the power of %f is %f",*x,*y,val);
Re: Without Using Pow Function In C by akbarthegreat: 8:55pm On Sep 25, 2016
We can find the power of a number without using pow function and using recursion.
Let power(a, b) means ab.
If b is even : power(a, b) = power(a, b/2)*power(a, b/2);
if b is odd : power(a, b) = power(a, b/2)*power(a, b/2)*a;
For example:
28 = 24 * 24;
29 = 24 * 24 * 2;
Source : http://www.techcrashcourse.com/2014/10/c-program-calculate-power-of-number.html

(1) (Reply)

Sam Altman Visits Nigeria On AI Tour / Could Lagos, Nigeria Be The Next Silicon Valley? - American Media / List Of Nigerian Software That Compete Globally

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 16
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.