C语言 在 C 中的力量?

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

To the power of in C?

cmath

提问by samir

So in python, all I have to do is

所以在python中,我所要做的就是

print(3**4) 

Which gives me 81

这给了我 81

How do I do this in C? I searched a bit and say the exp()function, but have no clue how to use it, thanks in advance

我如何在 C 中做到这一点?我搜索了一下并说出了该exp()功能,但不知道如何使用它,提前致谢

回答by Gangadhar

You need pow();function from math.hheader.
syntax

您需要pow();来自math.h标题的功能。
句法

#include <math.h>
double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);

Here x is base and y is exponent. result is x^y.

这里 x 是基数,y 是指数。结果是x^y

usage

用法

pow(2,4);  

result is 2^4 = 16. //this is math notation only   
// In c ^ is a bitwise operator

And make sure you include math.hto avoid warning ("incompatible implicit declaration of built in function 'pow'").

并确保包含math.h以避免警告(“ incompatible implicit declaration of built in function 'pow'”)。

Link math library by using -lmwhile compiling. This is dependent on Your environment.
For example if you use Windows it's not required to do so, but it is in UNIX based systems.

-lm编译时使用链接数学库。这取决于您的环境。
例如,如果您使用 Windows,则不需要这样做,但它是在基于 UNIX 的系统中。

回答by verbose

#include <math.h>


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

回答by Ryan Webb

you can use pow(base, exponent)from #include <math.h>

你可以使用pow(base, exponent)#include <math.h>

or create your own:

或创建自己的:

int myPow(int x,int n)
{
    int i; /* Variable used in loop counter */
    int number = 1;

    for (i = 0; i < n; ++i)
        number *= x;

    return(number);
}

回答by Yu Hao

There's no operator for such usage in C, but a family of functions:

在 C 中没有用于这种用法的运算符,而是一系列函数:

double pow (double base , double exponent);
float powf (float base  , float exponent);
long double powl (long double base, long double exponent);

Note that the later two are only part of standard C since C99.

请注意,后两个只是自 C99 以来标准 C 的一部分。

If you get a warning like:

如果您收到如下警告:

"incompatible implicit declaration of built in function 'pow' "

“内置函数‘pow’的不兼容隐式声明”

That's because you forgot #include <math.h>.

那是因为你忘记了#include <math.h>

回答by Keith

For another approach, note that all the standard library functions work with floating point types. You can implement an integer type function like this:

对于另一种方法,请注意所有标准库函数都使用浮点类型。你可以像这样实现一个整数类型的函数:

unsigned power(unsigned base, unsigned degree)
{
    unsigned result = 1;
    unsigned term = base;
    while (degree)
    {
        if (degree & 1)
            result *= term;
        term *= term;
        degree = degree >> 1;
    }
    return result;
}

This effectively does repeated multiples, but cuts down on that a bit by using the bit representation. For low integer powers this is quite effective.

这有效地重复倍数,但通过使用位表示减少了一点。对于低整数幂,这是非常有效的。

回答by tintin

just use pow(a,b),which is exactly 3**4in python

只需使用pow(a,b),这正是3**4在python中

回答by Bodhi

Actually in C, you don't have an power operator. You will need to manually run a loop to get the result. Even the exp function just operates in that way only. But if you need to use that function, include the following header

实际上,在 C 中,您没有幂运算符。您需要手动运行循环才能获得结果。甚至 exp 函数也只是以这种方式运行。但是如果您需要使用该功能,请包含以下标题

#include <math.h>

then you can use pow().

然后你可以使用 pow()。