C语言 如何提升权力

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20872689/
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-09-02 10:34:21  来源:igfitidea点击:

How to raise to the power of

cmath

提问by user3139744

I am very new to C

我对 C 很陌生

I am trying to put something to the power of something

我试图将某事赋予某事的力量

eg. 5^3=125;

例如。5^3=125;

but when I code

但是当我编码时

#include <math.h>
...

printf("%d", 5^3);

I get 6. Am i doing something fundamentally wrong??

我得到 6. 我在做一些根本错误的事情吗?

回答by Nabla

^is the bitwise XOR operator in C.

^是 C 中的按位异或运算符。

5is 101in binary representation and 3is 011. So 101 XOR 011 = 110, which is 6in decimal.

5101二进制表示并且3011。所以101 XOR 011 = 110,这是6十进制的。

What you want is pow(5,3)(which is available after including math.has you did). Note however that it acts on doubles not integer, so your return value will be a floating point and in general not exact. If you need exact integer exponentiation, then you either have to use an additional library or implement your own power-function. This is not that hard, for an algorithm see exponentiation by squaring. Standard C does not include a function for integer exponentiation. Anyway with integer exponentiation it is important to take care of overflows, which may happen already for small input values.

您想要的是pow(5,3)(在math.h像您一样包含之后可用)。但是请注意,它作用于双精度数而不是整数,因此您的返回值将是浮点数,通常不准确。如果您需要精确的整数取幂,那么您必须使用额外的库或实现您自己的幂函数。这并不难,因为算法看到平方取幂。标准 C 不包括整数取幂函数。无论如何,对于整数取幂,重要的是要处理溢出,这对于小输入值可能已经发生。

Also to print the returned double you either have to cast the double to an int printf("%d", (int)pow(5,3))or print it as floating point printf("%f", pow(5,3))

同样要打印返回的双精度数,您必须将双精度数转换为 intprintf("%d", (int)pow(5,3))或将其打印为浮点数printf("%f", pow(5,3))

回答by pkacprzak

^is a XOR operator in C.

^是 C 中的 XOR 运算符。

Instead you should use the powfunction from math.h, however powreturns double

相反,您应该使用powfrom 函数math.h,但是pow返回 double

#include <math.h>
...

printf("%d", (int)pow(5, 3));

回答by splrs

You should use pow(5,3).

你应该使用pow(5,3).

You're using the bitwise XOR (^) operator there: in binary that's 101 ^ 011, which is 110, or 6(your current answer) in denary.

您在^那里使用按位 XOR ( ) 运算符:在二进制中,即101 ^ 011,即110,或6(您当前的答案)在 denary 中。

回答by haccks

There is no operator for exponentiation (power) in C. ^is used is a bitwise XORoperator in C language. Doing 5^3will return 6, not 125. For exponentiation you can use standard library function pow.
Signature:

目前在C中没有运营商幂(功率)^使用的是XORC 语言中的按位运算符。做5^3会回来6,不会125。对于求幂,您可以使用标准库函数pow
签名:

double pow(double x, double y);  

Header:

标题:

#include <math.h>  

Change

改变

printf("%d", 5^3);  

to

printf("%d",(int)pow(5,3));  

In C++ do not use <math.h>. Instead you can use <cmath>.

在 C++ 中不要使用<math.h>. 相反,您可以使用<cmath>.

回答by Yick Leung

Others have already recommended using the powfunction from math.hbecause ^is not the correct operator for performing power of. Alternatively you can also write your own power function just for fun.

其他人已经推荐使用powfrom 函数,math.h因为^它不是执行 power of 的正确运算符。或者,您也可以编写自己的幂函数,只是为了好玩。

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

Inside main:

内部主要:

printf("%d\n", power(5,3));

In this case you don't have to use the cast operator as pow()doesn't work with int.

在这种情况下,您不必使用强制转换运算符,因为pow()它不适用于 int。