Why is my power operator (^) not working?
#include <stdio.h>
void main(void)
{
int a;
int result;
int sum = 0;
开发者_Go百科 printf("Enter a number: ");
scanf("%d", &a);
for( int i = 1; i <= 4; i++ )
{
result = a ^ i;
sum += result;
}
printf("%d\n", sum);
}
Why is ^
not working as the power operator?
Well, first off, the ^
operator in C/C++ is the bit-wise XOR. It has nothing to do with powers.
Now, regarding your problem with using the pow()
function, some googling shows that casting one of the arguments to double helps:
result = (int) pow((double) a,i);
Note that I also cast the result to int
as all pow()
overloads return double, not int
. I don't have a MS compiler available so I couldn't check the code above, though.
Since C99, there are also float
and long double
functions called powf
and powl
respectively, if that is of any help.
In C ^
is the bitwise XOR:
0101 ^ 1100 = 1001 // in binary
There's no operator for power, you'll need to use pow
function from math.h (or some other similar function):
result = pow( a, i );
pow() doesn't work with int
, hence the error "error C2668:'pow': ambiguous call to overloaded function"
http://www.cplusplus.com/reference/clibrary/cmath/pow/
Write your own power function for int
s:
int power(int base, int exp)
{
int result = 1;
while(exp) { result *= base; exp--; }
return result;
}
First of all ^
is a Bitwise XOR operator not power operator.
You can use other things to find power of any number. You can use for loop to find power of any number
Here is a program to find x^y i.e. xy
double i, x, y, pow;
x = 2;
y = 5;
pow = 1;
for(i=1; i<=y; i++)
{
pow = pow * x;
}
printf("2^5 = %lf", pow);
You can also simply use pow() function to find power of any number
double power, x, y;
x = 2;
y = 5;
power = pow(x, y); /* include math.h header file */
printf("2^5 = %lf", power);
include math.h and compile with gcc test.c -lm
It's not working because c as well as c++ do not have any operators to perform power operations.
What you can do is, you can use math.h library and use pow function. There is a Function for this instead of the operator.
` #include<stdio.h>
#include<math.h>
int main(){
int base = 3;
int power = 5;
pow(double(base), double(power));
return 0;
}`
You actually have to use pow(number, power);. Unfortunately, carats don't work as a power sign in C. Many times, if you find yourself not being able to do something from another language, its because there is a diffetent function that does it for you.
There is no way to use the ^
(Bitwise XOR) operator to calculate the power of a number.
Therefore, in order to calculate the power of a number we have two options, either we use a while loop or the pow() function.
1. Using a while loop.
#include <stdio.h>
int main() {
int base, expo;
long long result = 1;
printf("Enter a base no.: ");
scanf("%d", &base);
printf("Enter an exponent: ");
scanf("%d", &expo);
while (expo != 0) {
result *= base;
--expo;
}
printf("Answer = %lld", result);
return 0;
}
2. Using the pow()
function
#include <math.h>
#include <stdio.h>
int main() {
double base, exp, result;
printf("Enter a base number: ");
scanf("%lf", &base);
printf("Enter an exponent: ");
scanf("%lf", &exp);
// calculate the power of our input numbers
result = pow(base, exp);
printf("%.1lf^%.1lf = %.2lf", base, exp, result);
return 0;
}
If you are trying to calculate the power of base 2, you can use the bitwise shift operator to calculate the power. For example, say you wanted to calculate 2 to the power of 8.
2 << 7
For integer exponent, you may simply write your implementation of pow()
int myPow(int x, int n)
{
if (n == 0) return 1;
return x * myPow(x, n - 1);
}
All the solutions here, except for the ones that use math.h will not work for fractional powers.
The last example here is by far the worst and acctually copy'n'paste. It is not only slow but it will break stuff at some point. Just use the math.h with pow() as already stated and that's good enough. If you are not allowed to use math.h, use a tail-recursive function, that saves the current operation result in an accumulator and returns the accumulator at the end, thus avoiding exploding function calls due to extensive traces.
精彩评论