使用Math.Pow(10,n)可以吗? [英] Is it OK to use Math.Pow (10, n)?
问题描述
我需要计算功效(10,n)
I need to compute power (10, n)
可以使用Math.Pow (10, n)
吗?
还是应该使用循环?
for (int i = 0; i < n; i++){
x*=10;
}
哪个更好?为什么?
推荐答案
如果基数和指数均为整数,则可以考虑不使用Pow.但是即使在那种情况下,Pow通常也更好,因为它更具可读性.如果至少一个浮点值,请使用Pow.
If both base and exponent are integers you might consider not using Pow. But even in that case Pow is usually better because its more readable. If at least one is a floatingpoint value, use Pow.
如果指数为0.5,则应使用Sqrt;如果指数是小整数(2,3,4),则表示带乘法的公式会更快,但可读性较低.
If the exponent is 0.5 you should use Sqrt, and if the exponent is a small integer (2,3,4) expressing the formula with multiplications is faster, but less readable.
如果您想使用整数指数实现快速幂运算,则平方乘运算法则可能不是您想要的,而不是简单的循环.但是在大多数情况下,Pow仍然更快.
If you want to implement fast exponentiation with an integer exponent the Square-and-Multiply algorithm and not a simple loop might be what you want. But in most scenarios Pow is still faster.
这篇关于使用Math.Pow(10,n)可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!