Reputation: 22160
I tried x = y ** e
, but that didn't work.
Upvotes: 71
Views: 341108
Reputation: 400344
To add to what Evan said: C does not have a built-in operator for exponentiation, because it is not a primitive operation for most CPUs. Thus, it's implemented as a library function.
Also, for computing the function eˣ, you can use the exp(double)
, expf(float)
, and expl(long double)
functions.
Note that you do not want to use the ^
operator, which is the bitwise exclusive OR operator.
Upvotes: 47
Reputation: 1
you can use exponentiation by squaring as mentioned above, but this way, you manage the system integer minimum, so it does not overflow when working with negative exponents in O(log n)
#include <limits.h>
double pow(double base, int exp){
if (exp == 0) return 1;
if (exp == 1) return base;
if (exp < 0){
if (exp == INT_MIN) {
return (1 / base) * pow(base, exp + 1);
}
base = 1 / base;
exp = -exp;
}
double half = pow(base, exp / 2);
if (exp % 2 == 0) {
return half * half;
} else {
return half * half * base;
}
}
Upvotes: 0
Reputation: 90462
use the pow
function (it takes float
s/double
s though).
man pow
:
#include <math.h>
double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);
EDIT: For the special case of positive integer powers of 2
, you can use bit shifting: (1 << x)
will equal 2
to the power x
. There are some potential gotchas with this, but generally, it would be correct.
Upvotes: 114
Reputation: 754090
The non-recursive version of the function is not too hard - here it is for integers:
long powi(long x, unsigned n)
{
long p = x;
long r = 1;
while (n > 0)
{
if (n % 2 == 1)
r *= p;
p *= p;
n /= 2;
}
return(r);
}
(Hacked out of code for raising a double value to an integer power - had to remove the code to deal with reciprocals, for example.)
Upvotes: 5
Reputation: 842
Similar to an earlier answer, this will handle positive and negative integer powers of a double nicely.
double intpow(double a, int b)
{
double r = 1.0;
if (b < 0)
{
a = 1.0 / a;
b = -b;
}
while (b)
{
if (b & 1)
r *= a;
a *= a;
b >>= 1;
}
return r;
}
Upvotes: 5
Reputation: 31
int power(int x,int y){
int r=1;
do{
r*=r;
if(y%2)
r*=x;
}while(y>>=1);
return r;
};
(iterative)
int power(int x,int y){
return y?(y%2?x:1)*power(x*x,y>>1):1;
};
(if it has to be recursive)
imo, the algorithm should definitely be O(logn)
Upvotes: 2
Reputation: 204798
pow
only works on floating-point numbers (double
s, actually). If you want to take powers of integers, and the base isn't known to be an exponent of 2
, you'll have to roll your own.
Usually the dumb way is good enough.
int power(int base, unsigned int exp) {
int i, result = 1;
for (i = 0; i < exp; i++)
result *= base;
return result;
}
Here's a recursive solution which takes O(log n)
space and time instead of the easy O(1)
space O(n)
time:
int power(int base, int exp) {
if (exp == 0)
return 1;
else if (exp % 2)
return base * power(base, exp - 1);
else {
int temp = power(base, exp / 2);
return temp * temp;
}
}
Upvotes: 30
Reputation: 2947
or you could just write the power function, with recursion as a added bonus
int power(int x, int y){
if(y == 0)
return 1;
return (x * power(x,y-1) );
}
yes,yes i know this is less effecient space and time complexity but recursion is just more fun!!
Upvotes: 3