你如何在 C++ 中使用指数和变量?

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

How do you use an exponent in c++ with a variable?

c++variablesexponent

提问by Soully

So I realize that #include is necessary, and that there is a pow(x,y) where x^y works...but when I attempted to use pow(2,(num-1)), it kicked back an error...

所以我意识到 #include 是必要的,并且有一个 pow(x,y) 在哪里 x^y 工作......但是当我尝试使用 pow(2,(num-1)) 时,它踢回了一个错误...

errorC2668: 'pow' : ambiguous call to overloaded function

errorC2668:“pow”:对重载函数的调用不明确

the line of code I have for it is as follows

我的代码行如下

perfect = (pow(2,(num-1))) * (pow(2,num)-1);

Any recommendations?

有什么建议吗?

Thanks in advance

提前致谢

EDIT:

编辑:

num is indeed declared as an int.

num 确实被声明为 int。

num does have a value, starts at 1 and goes to UINT_MAX

num 确实有一个值,从 1 开始到 UINT_MAX

Added an asterisk to equation

为等式添加了星号

回答by mathstuf

The compiler doesn't know which pow() function to call. The overloads listed heregives the following list:

编译器不知道要调用哪个 pow() 函数。此处列出的重载提供了以下列表:

      float pow (       float base,       float exponent );
     double pow (      double base,      double exponent );
long double pow ( long double base, long double exponent );
      float pow (       float base,         int exponent );
     double pow (      double base,         int exponent );
long double pow ( long double base,         int exponent );

The compiler won't guess which one to use. Make it explicit with a casts.

编译器不会猜测使用哪一个。使用强制转换使其明确。

perfect = (pow(2.,(double)(num-1))) < (pow(2.,(double)num)-1);

There may be some extra casts there, but they won't hurt anything.

那里可能会有一些额外的演员,但他们不会伤害任何东西。

回答by paxdiablo

These are the allowed pow()functions in C++. The problem is that your code has an int as the first argument and C++ doesn't know whether to promote it to a double or long double.

这些是pow()C++ 中允许的函数。问题是您的代码有一个 int 作为第一个参数,而 C++ 不知道是将它提升为 double 还是 long double。

     double pow (      double base,      double exponent );
long double pow ( long double base, long double exponent );
      float pow (       float base,       float exponent );
     double pow (      double base,         int exponent );
long double pow ( long double base,         int exponent );

Try putting in (double)2instead of just 2.

尝试放入(double)2而不仅仅是2.

perfect = (pow((double)2,(num-1))) * (pow((double)2,num)-1)

回答by lc.

From error C2668: 'pow' : ambiguous call to overloaded function in VC++ 2005 only, alex.m wrote,

错误 C2668: 'pow' : 仅在 VC++ 2005 中对重载函数的模糊调用,alex.m 写道,

"pow" (any overload) takes a floating point type number (single, double or long double precision) as first argument, not an integer. This is where the error comes from, since the compiler can't guess the way you want your long integer to be converted.

Just try writing a cast expression, like this:

Code Block

c = pow((double)numberOfScansCompleted, 2);

“pow”(任何重载)将浮点类型数(单精度、双精度或长双精度)作为第一个参数,而不是整数。这就是错误的来源,因为编译器无法猜测您希望长整数转换的方式。

尝试编写一个强制转换表达式,如下所示:

代码块

c = pow((double)numberOfScansCompleted, 2);

So, if you try pow((double)2,(num-1)), it should work.

所以,如果你尝试pow((double)2,(num-1)),它应该可以工作。



Amusing side note, as I typed the beginning of it into Google, "pow ambiguous call to overloaded function" came up as the top suggested search.

有趣的旁注,当我在谷歌中输入它的开头时,“对重载函数的pow ambiguous call”出现在最热门的建议搜索中。

回答by user44556

Add missing multiplication (or what you want) and also there should be semicolon at the end of the line:

添加缺少的乘法(或您想要的),并且行尾应该有分号:

perfect = (pow(2,(num-1))) * (pow(2,num)-1) ;

回答by palm3D

Remember, you can also write integer powers of 2 with a shift operator:

请记住,您还可以使用移位运算符编写 2 的整数幂:

int perfect = (1<<(num-1)) * ((1<<num) - 1);

Or with ldexp(also included from <cmath>):

或与ldexp(也包括自<cmath>):

double perfect = ldexp(1.0, num-1) * (ldexp(1.0, num) - 1);