将数字提高到幂的 C++ 函数是什么?

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

What is the C++ function to raise a number to a power?

c++math

提问by Joey Robert

How do I raise a number to a power?

我如何将数字提高到幂?

2^1

2^2

2^3

etc...

等等...

回答by Joey Robert

pow() in the cmath library. More info here. Don't forget to put #include<cmath>at the top of the file.

pow() 在 cmath 库中。更多信息在这里。不要忘记放在#include<cmath>文件的顶部。

回答by Johannes Schaub - litb

std::powin the <cmath>header has these overloads:

std::pow<cmath>标题中有这些重载:

pow(float, float);
pow(float, int);
pow(double, double); // taken over from C
pow(double, int);
pow(long double, long double);
pow(long double, int);

Now you can't just do

现在你不能只做

pow(2, N)

with N being an int, because it doesn't know which of float, double, or long doubleversion it should take, and you would get an ambiguity error. All three would need a conversion from int to floating point, and all three are equally costly!

N 是一个整数,因为它不知道它应该采用哪个float、哪个double或哪个long double版本,并且你会得到一个歧义错误。这三个都需要从 int 到浮点的转换,而且这三个成本都一样!

Therefore, be sure to have the first argument typed so it matches one of those three perfectly. I usually use double

因此,请确保输入第一个参数,使其与这三个参数中的一个完美匹配。我通常使用double

pow(2.0, N)

Some lawyer crap from me again. I've often fallen in this pitfall myself, so I'm going to warn you about it.

一些律师又从我这里废话了。我自己也经常陷入这个陷阱,所以我要警告你。

回答by Cindy

In C++ the "^" operator is a bitwise OR. It does not work for raising to a power. The x << n is a left shift of the binary number which is the same as multiplying x by 2 n number of times and that can only be used when raising 2 to a power. The POW function is a math function that will work generically.

在 C++ 中,“^”运算符是按位或。它不适用于提升到幂。x << n 是二进制数的左移,这与将 x 乘以 2 n 次相同,并且只能在将 2 乘以幂时使用。POW 函数是一个通用的数学函数。

回答by sujal

You should be able to use normal C methods in math.

您应该能够在数学中使用普通的 C 方法。

#include <cmath>

#include <cmath>

pow(2,3)

pow(2,3)

if you're on a unix-like system, man cmath

如果您使用的是类 Unix 系统, man cmath

Is that what you're asking?

这就是你要问的吗?

Sujal

苏加尔

回答by Adam Haile

Use the pow(x,y) function: See Here

使用 pow(x,y) 函数:参见此处

Just include math.h and you're all set.

只需包含 math.h 就可以了。

回答by leander

While pow( base, exp )is a great suggestion, be aware that it typically works in floating-point.

虽然这pow( base, exp )是一个很好的建议,但请注意它通常适用于浮点数。

This may or may not be what you want: on some systems a simple loop multiplying on an accumulator will be faster for integer types.

这可能是也可能不是您想要的:在某些系统上,对于整数类型,累加器上的简单循环乘法会更快。

And for square specifically, you might as well just multiply the numbers together yourself, floating-point or integer; it's not really a decrease in readability (IMHO) and you avoid the performance overhead of a function call.

特别是对于平方,您不妨自己将数字相乘,浮点数或整数;这并不是真正的可读性下降(恕我直言),并且您避免了函数调用的性能开销。

回答by Agricola

I don't have enough reputation to comment, but if you like working with QT, they have their own version.

我没有足够的声誉来评论,但如果你喜欢使用 QT,他们有自己的版本。

    #include <QtCore/qmath.h>
    qPow(x, y); // returns x raised to the y power.

Or if you aren't using QT, cmath has basically the same thing.

或者,如果您不使用 QT,则 cmath 具有基本相同的功能。

    #include <cmath>
    double x = 5, y = 7; //As an example, 5 ^ 7 = 78125
    pow(x, y); //Should return this: 78125

回答by HaSeeB MiR

if you want to deal with base_2only then i recommend using left shift operator <<instead of math library.

如果你只想处理base_2那么我建议使用左移运算符<<而不是数学库

sample code :

示例代码:

int exp = 16;
for(int base_2 = 1; base_2 < (1 << exp); (base_2 <<= 1)){
std::cout << base_2 << std::endl;
}

sample output :

样本输出:

1   2   4   8   16  32  64  128  256  512  1024  2048  4096  8192  16384  32768

回答by Filza Naser

#include <iostream>
#include <conio.h>

using namespace std;

double raiseToPow(double ,int) //raiseToPow variable of type double which takes arguments (double, int)

void main()
{
    double x; //initializing the variable x and i 
    int i;
    cout<<"please enter the number"; 
    cin>>x;
    cout<<"plese enter the integer power that you want this number raised to";
    cin>>i;
    cout<<x<<"raise to power"<<i<<"is equal to"<<raiseToPow(x,i);
}

//definition of the function raiseToPower

//函数raiseToPower的定义

double raiseToPow(double x, int power)
{
    double result;
    int i;
    result =1.0;
    for (i=1, i<=power;i++)
    {
        result = result*x;
    }
    return(result);
}

回答by Dario

It's pow or powf in <math.h>

它是 pow 或 powf <math.h>

There is no special infix operator like in Visual Basic or Python

没有像 Visual Basic 或 Python 中那样的特殊中缀运算符