C++ 为什么我的幂运算符 (^) 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4843304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:43:31  来源:igfitidea点击:

Why is my power operator (^) not working?

c++c

提问by Abid Ali

#include <stdio.h>

void main(void)
{
    int a;
    int result;
    int sum = 0;
    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?

为什么^不作为电力运营商工作?

回答by Sergei Tachenov

Well, first off, the ^operator in C/C++ is the bit-wise XOR. It has nothing to do with powers.

首先,^C/C++ 中的运算符是按位异或。它与权力无关。

Now, regarding your problem with using the pow()function, some googlingshows that casting one of the arguments to double helps:

现在,关于您使用该pow()函数的问题,一些谷歌搜索表明将其中一个参数转换为 double 有帮助:

result = (int) pow((double) a,i);

Note that I also cast the result to intas all pow()overloads return double, not int. I don't have a MS compiler available so I couldn't check the code above, though.

请注意,我还将结果强制转换int为,因为所有pow()重载都返回 double,而不是int. 我没有可用的 MS 编译器,所以我无法检查上面的代码。

Since C99, there are also floatand long doublefunctions called powfand powlrespectively, if that is of any help.

由于C99,也有floatlong double调用函数powfpowl分别,如果这是任何帮助。

回答by peoro

In C ^is the bitwise XOR:

在 C 中^是按位异或:

0101 ^ 1100 = 1001 // in binary

There's no operator for power, you'll need to use powfunction from math.h (or some other similar function):

没有幂运算符,您需要使用powmath.h 中的函数(或其他类似函数):

result = pow( a, i );

回答by Max

pow() doesn't work with int, hence the error "error C2668:'pow': ambiguous call to overloaded function"

pow() 不适用于int,因此出现错误“错误 C2668:'pow': 对重载函数的不明确调用”

http://www.cplusplus.com/reference/clibrary/cmath/pow/

http://www.cplusplus.com/reference/clibrary/cmath/pow/

Write your own power function for ints:

ints编写您自己的幂函数:

int power(int base, int exp)
{
    int result = 1;
    while(exp) { result *= base; exp--; }
    return result;
}

回答by Pankaj Prakash

First of all ^is a Bitwise XOR operatornotpower operator.

首先^按位异或运算符而不是幂运算符。

You can use other things to find power of any number. You can use for loop to find power of any number

您可以使用其他东西来找到任何数字的幂。您可以使用for 循环来查找任何数字的幂

Here is a program to find x^y i.e. xy

这是一个查找 x^y 的程序,即 x y

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

您也可以简单地使用pow() 函数来查找任何数字的幂

double power, x, y;
x = 2;
y = 5;
power = pow(x, y); /* include math.h header file */

printf("2^5 = %lf", power);

回答by Nick Anderegg

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.

你实际上必须使用 pow(number, power);。不幸的是,克拉在 C 语言中不能用作权力符号。很多时候,如果您发现自己无法用另一种语言做某事,那是因为有不同的功能可以为您做这件事。

回答by anup

include math.h and compile with gcc test.c -lm

包含 math.h 并使用 gcc test.c -lm 编译

回答by Anant Vardhan

Instead of using ^, use 'pow' function which is a predefined function which performs the Power operation and it can be used by including math.hheader file.

^使用 'pow' 函数代替使用,它是一个预定义的函数,它执行 Power 操作,可以通过包含math.h头文件来使用它。

^This symbol performs BIT-WISE XOR operation in C, C++.

^此符号在 C、C++ 中执行 BIT-WISE XOR 运算。

Replace a^iwith pow(a,i).

替换a^ipow(a,i)