什么是 C++ 求一个数的幂的函数?
如何计算一个数的幂?
2^1
2^2
2^3
ETC...
How do I raise a number to a power?
2^1
2^2
2^3
etc...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何计算一个数的幂?
2^1
2^2
2^3
ETC...
How do I raise a number to a power?
2^1
2^2
2^3
etc...
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(17)
cmath 库中的 pow() 。 更多信息此处。
不要忘记将
#include
放在文件顶部。pow() in the cmath library. More info here.
Don't forget to put
#include<cmath>
at the top of the file.
标头中的std::pow
具有以下重载:现在您不能只将
N 视为 int,因为它不知道哪一个它应该采用
float
、double
或long double
版本,并且您会收到歧义错误。 所有这三个都需要从 int 到浮点的转换,并且这三个的成本相同!因此,请确保输入第一个参数,使其与这三个参数中的一个完美匹配。 我通常使用
double
又是我的一些律师废话。 我自己也经常陷入这个陷阱,所以我要警告你。
std::pow
in the<cmath>
header has these overloads:Now you can't just do
with N being an int, because it doesn't know which of
float
,double
, orlong 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
Some lawyer crap from me again. I've often fallen in this pitfall myself, so I'm going to warn you about it.
在 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.
您应该能够在数学中使用普通的 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
使用 pow(x,y) 函数:参见此处
只需包含数学即可。 h 一切就绪。
Use the pow(x,y) function: See Here
Just include math.h and you're all set.
虽然 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.
我没有足够的声誉来发表评论,但如果您喜欢使用 QT,他们有自己的版本。
或者,如果您不使用 QT,cmath 也有基本相同的功能。
I don't have enough reputation to comment, but if you like working with QT, they have their own version.
Or if you aren't using QT, cmath has basically the same thing.
如果您只想处理base_2,那么我建议使用左移运算符<<而不是数学库 >。
示例代码:
示例输出:
if you want to deal with base_2 only then i recommend using left shift operator << instead of math library.
sample code :
sample output :
中是 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
//raiseToPower函数的定义
//definition of the function raiseToPower
许多答案都建议使用
pow()
或类似的替代方案或他们自己的实现。 但是,鉴于您问题中的示例(2^1
、2^2
和2^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
and2^3
) in your question, I would guess whether you only need to raise2
to an integer power. If this is the case, I would suggest you to use1 << n
for2^n
.首先添加
#include
然后您可以在代码中使用
pow
方法,例如:其中 3.5 是 base 和 3 是 exp
First add
#include <cmath>
thenyou can use
pow
methode in your code for example :Which 3.5 is base and 3 is exp
您原来的问题标题具有误导性。 如果只是正方形,请使用
2*2
。Your original question title is misleading. To just square, use
2*2
.请注意,使用
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 thanx*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
.使用 cmath、tgmath 或 math.h 库中的 pow() 函数。
请注意,如果您将 power 输入作为除 long double 之外的任何数据类型,那么答案将提升为 double 的答案。 也就是说,它将接受输入并给出双倍的输出。 对于长双精度输入,返回类型为长双精度。 为了改变 int 使用的答案,
int c=(int)pow(a,b)
但是,请记住,对于某些数字,这可能会导致数字小于正确答案。 例如,您必须计算 5^2,那么在某些编译器上,结果可以返回为 24.99999999999。 将数据类型更改为 int 时,正确答案将是 24 而不是 25。 所以,现在就这样做
,你的答案将是正确的。
此外,对于非常大的数字,在将数据类型 double 更改为 long long int 时,数据会丢失。
例如,您编写
并给出输入 a=3 和 b=38
那么结果将是 1350851717672992000,而正确答案是 1350851717672992089,这是因为 pow() 函数返回 1.35085e+18,它被提升为 int 作为 1350851717672992000。我建议为这种情况编写一个自定义幂函数,例如:-
然后在任何需要的时候调用它,
对于大于 long long int 范围的数字,可以使用 boost 库或字符串。
use pow() function in cmath, tgmath or math.h library.
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
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
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:-
and then calling it whenever you want like,
For numbers greater than the range of long long int, either use boost library or strings.
我正在使用库
cmath
或math.h
来利用pow()
库函数来处理权力I am using the library
cmath
ormath.h
in order to make use of thepow()
library functions that takes care of the powers