什么是 C++ 求一个数的幂的函数?

发布于 2024-07-21 04:30:45 字数 74 浏览 16 评论 0原文

如何计算一个数的幂?

2^1

2^2

2^3

ETC...

How do I raise a number to a power?

2^1

2^2

2^3

etc...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

明月夜 2024-07-28 04:30:45

cmath 库中的 pow() 。 更多信息此处
不要忘记将 #include 放在文件顶部。

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

情仇皆在手 2024-07-28 04:30:45

标头中的 std::pow 具有以下重载:

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);

现在您不能只将

pow(2, N)

N 视为 int,因为它不知道哪一个它应该采用 floatdoublelong double 版本,并且您会收到歧义错误。 所有这三个都需要从 int 到浮点的转换,并且这三个的成本相同!

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

pow(2.0, N)

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

std::pow in the <cmath> header has these overloads:

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 double version 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!

Therefore, be sure to have the first argument typed so it matches one of those three perfectly. I usually use 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.

往事风中埋 2024-07-28 04:30:45

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

In C++ the "^" operator is a bitwise XOR. 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, and not other integers. The POW function is a math function that will work generically.

北恋 2024-07-28 04:30:45

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

#include

pow(2,3)

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

这就是您想要的在问吗?

苏加尔

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

#include <cmath>

pow(2,3)

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

Is that what you're asking?

Sujal

遗心遗梦遗幸福 2024-07-28 04:30:45

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

只需包含数学即可。 h 一切就绪。

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

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

疧_╮線 2024-07-28 04:30:45

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

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

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

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

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.

蓝眸 2024-07-28 04:30:45

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

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

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

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

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

    #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.

    #include <cmath>
    double x = 5, y = 7; //As an example, 5 ^ 7 = 78125
    pow(x, y); //Should return this: 78125
软糖 2024-07-28 04:30:45

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

示例代码:

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

示例输出:

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

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

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
提笔落墨 2024-07-28 04:30:45

中是 pow 或 powf

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

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

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

爱冒险 2024-07-28 04:30:45
#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);
}

//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);
}
#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

double raiseToPow(double x, int power)
{
    double result;
    int i;
    result =1.0;
    for (i=1, i<=power;i++)
    {
        result = result*x;
    }
    return(result);
}
絕版丫頭 2024-07-28 04:30:45

许多答案都建议使用 pow() 或类似的替代方案或他们自己的实现。 但是,鉴于您问题中的示例(2^12^22^3),我猜测您是否只需要计算 2 的整数次方。 如果是这种情况,我建议您使用 1 << n 代表 2^n

Many answers have suggested pow() or similar alternatives or their own implementations. However, given the examples (2^1, 2^2 and 2^3) in your question, I would guess whether you only need to raise 2 to an integer power. If this is the case, I would suggest you to use 1 << n for 2^n.

南…巷孤猫 2024-07-28 04:30:45

首先添加 #include 然后
您可以在代码中使用 pow 方法,例如:

pow(3.5, 3);

其中 3.5base3exp

First add #include <cmath> then
you can use pow methode in your code for example :

pow(3.5, 3);

Which 3.5 is base and 3 is exp

晨光如昨 2024-07-28 04:30:45
pow(2.0,1.0)
pow(2.0,2.0)
pow(2.0,3.0)

您原来的问题标题具有误导性。 如果只是正方形,请使用2*2

pow(2.0,1.0)
pow(2.0,2.0)
pow(2.0,3.0)

Your original question title is misleading. To just square, use 2*2.

俯瞰星空 2024-07-28 04:30:45

请注意,使用 pow(x,y) 的效率低于 x*x*x y 倍,如此处所示和回答https://stackoverflow.com/a/2940800/319728

因此,如果您想提高效率,请使用 x*x*x

Note that the use of pow(x,y) is less efficient than x*x*x y times as shown and answered here https://stackoverflow.com/a/2940800/319728.

So if you're going for efficiency use x*x*x.

ゞ花落谁相伴 2024-07-28 04:30:45

使用 cmath、tgmath 或 math.h 库中的 pow() 函数。

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int a,b;
cin >> a >> b;
cout << pow(a,b) << endl; // this calculates a^b

return 0;
}

请注意,如果您将 power 输入作为除 long double 之外的任何数据类型,那么答案将提升为 double 的答案。 也就是说,它将接受输入并给出双倍的输出。 对于长双精度输入,返回类型为长双精度。 为了改变 int 使用的答案,
int c=(int)pow(a,b)

但是,请记住,对于某些数字,这可能会导致数字小于正确答案。 例如,您必须计算 5^2,那么在某些编译器上,结果可以返回为 24.99999999999。 将数据类型更改为 int 时,正确答案将是 24 而不是 25。 所以,现在就这样做

int c=(int)(pow(a,b)+0.5)

,你的答案将是正确的。
此外,对于非常大的数字,在将数据类型 double 更改为 long long int 时,数据会丢失。
例如,您编写

long long int c=(long long int)(pow(a,b)+0.5);

并给出输入 a=3 和 b=38
那么结果将是 1350851717672992000,而正确答案是 1350851717672992089,这是因为 pow() 函数返回 1.35085e+18,它被提升为 int 作为 1350851717672992000。我建议为这种情况编写一个自定义幂函数,例如:-

long long int __pow (long long int a, long long int b)
{
long long int q=1;
for (long long int i=0;i<=b-1;i++)
{
q=q*a;
}

return q;
}

然后在任何需要的时候调用它,

int main()
{
long long int a,b;
cin >> a >> b;

long long int c=__pow(a,b);
cout << c << endl;

return 0;
}

对于大于 long long int 范围的数字,可以使用 boost 库或字符串。

use pow() function in cmath, tgmath or math.h library.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int a,b;
cin >> a >> b;
cout << pow(a,b) << endl; // this calculates a^b

return 0;
}

do note that if you give input to power as any data type other than long double then the answer will be promoted to that of double. that is it will take input and give output as double. for long double inputs the return type is long double. for changing the answer to int use,
int c=(int)pow(a,b)

But, do keep in mind for some numbers this may result in a number less than the correct answer. so for example you have to calculate 5^2, then the answer can be returned as 24.99999999999 on some compilers. on changing the data type to int the answer will be 24 rather than 25 the correct answer. So, do this

int c=(int)(pow(a,b)+0.5)

Now, your answer will be correct.
also, for very large numbers data is lost in changing data type double to long long int.
for example you write

long long int c=(long long int)(pow(a,b)+0.5);

and give input a=3 and b=38
then the result will come out to be 1350851717672992000 while the correct answer is 1350851717672992089, this happens because pow() function return 1.35085e+18 which gets promoted to int as 1350851717672992000. I suggest writing a custom power function for such scenarios, like:-

long long int __pow (long long int a, long long int b)
{
long long int q=1;
for (long long int i=0;i<=b-1;i++)
{
q=q*a;
}

return q;
}

and then calling it whenever you want like,

int main()
{
long long int a,b;
cin >> a >> b;

long long int c=__pow(a,b);
cout << c << endl;

return 0;
}

For numbers greater than the range of long long int, either use boost library or strings.

相对绾红妆 2024-07-28 04:30:45

我正在使用库 cmathmath.h 来利用 pow() 库函数来处理权力

#include<iostream>
#include<cmath>

int main()
{
    double number,power, result;
    cout<<"\nEnter the number to raise to power: ";
    cin>>number;
    cout<<"\nEnter the power to raise to: ";
    cin>>power;

    result = pow(number,power);

    cout<<"\n"<< number <<"^"<< power<<" = "<< result;

    return 0;
}

I am using the library cmath or math.h in order to make use of the pow() library functions that takes care of the powers

#include<iostream>
#include<cmath>

int main()
{
    double number,power, result;
    cout<<"\nEnter the number to raise to power: ";
    cin>>number;
    cout<<"\nEnter the power to raise to: ";
    cin>>power;

    result = pow(number,power);

    cout<<"\n"<< number <<"^"<< power<<" = "<< result;

    return 0;
}
长亭外,古道边 2024-07-28 04:30:45
int power (int i, int ow) // works only for ow >= 1
{ // but does not require <cmath> library!=)
    if (ow > 1)
    {
         i = i * power (i, ow - 1);
    }
    return i;
}

cout << power(6,7); //you can enter variables here
int power (int i, int ow) // works only for ow >= 1
{ // but does not require <cmath> library!=)
    if (ow > 1)
    {
         i = i * power (i, ow - 1);
    }
    return i;
}

cout << power(6,7); //you can enter variables here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文