在c ++中计算功率的最好或最有效的方法? [英] Best or most efficient way to calculate power in c++?
问题描述
由于有几种方式像pow(),powl()和创建自己的电源功能我想知道哪种是最好的或最有效的方法来计算功率?
As there are several ways like pow(), powl() and creating your own power function I'm wondering which is the best or most efficient way to calculate power?
运行时间或准确性或易于理解/实现的效率。
Efficient in terms of running time or accuracy or ease of understanding/implementing.
我的输入是double类型的变量(运行时已知的值,exp范围为10 -100)
My inputs are variables of type double (value known at running time, exp ranges 10-100)
有关我上一个问题的讨论:
There is a discussion on my previous question :
推荐答案
这取决于你计算的权力。有优化如 x * x
而不是 pow(x,2)
It depends what powers you are calculating. There are optimizations like x * x
instead of pow(x, 2)
, and there are optimizations for negative powers, powers of special numbers like 1, 2, 0, etc.
pow()
是一个非常通用的函数,通常不是最佳的特定情况下,你知道提前的参数之一。
pow()
is a very general function which is usually not optimal for specific cases where you know one of the arguments in advance.
这篇关于在c ++中计算功率的最好或最有效的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!