I want to compute 10 raised to the power minus m
. In addition to use the math function pow(10, -m)
, is there any fast and efficient way to do that?
我想计算10的-m次方。除了使用数学函数pow(10, -m),还有什么快速有效的方法吗?
What I ask such a simple question to the c++ gurus from SO is that, as you know, just like base 2, 10 is also a special base. If some value n
times the 10's power minus m
, it is equivalent to move n's decimal point to the left m times. I think it must be a fast and efficient way to cope with.
我向c++大师提出这样一个简单的问题,就像你知道的,就像基数2一样,10也是一个特殊的基础。如果某个值n乘以10的幂减m,就等于把n的小数点左移m倍。我认为这一定是一种快速有效的应对方式。
9 个解决方案
#1
5
For floating point m, so long as your standard library implementation is well written, then pow
will be efficient.
对于浮点m,只要您的标准库实现写得很好,那么pow将是有效的。
If m is an integer, and you hinted that it is, then you could use an array of pre calculated values.
如果m是一个整数,并且您暗示它是整数,那么您可以使用一个预计算值数组。
You should only be worrying about this kind of thing if that routine is a bottleneck in your code. That is if the calls to that routine take a significant proportion of the total running time.
如果这个例程是代码中的瓶颈,那么您就应该担心这种情况。这是如果对该例程的调用占总运行时间的很大一部分。
#2
4
Ten is not a special value on a binary machine, only two is. Use pow
or exponentiation by squaring.
十不是二进制机器上的特殊值,只有两个。用平方运算或指数运算。
#3
2
Unfortunately there is no fast and efficient way to calculate it using IEEE 754 floating point representation. The fastest way to get the result is to build a table for every value of m
that you care about, and then just perform a lookup.
不幸的是,没有快速有效的方法来计算它使用IEEE 754浮点表示法。得到结果的最快方法是为您关心的每一个值构建一个表,然后执行查找。
#4
0
If there's a fast and efficient way to do it then I'm sure your CPU supports it, unless you're running on an embedded system in which case I'd hope that the pow(...) implementation is well written.
如果有一种快速且有效的方法,那么我肯定您的CPU支持它,除非您在嵌入式系统上运行,在这种情况下,我希望pow(…)实现写得很好。
10 is special to us as most of us have ten fingers. Computers only have two digits, so 2 is special to them. :)
10对我们来说很特别,因为我们大多数人都有十个手指。计算机只有两个数字,所以2对它们来说是特殊的。:)
#5
0
Use lookup table there cant be more than 1000 floats and especially if m is integer.
使用查找表不能有超过1000个浮点数,特别是如果m是整数。
#6
0
If you could operate with log n instead of n for a significant time, you could save time because instead of
如果你可以用log n而不是n来操作很长一段时间,你可以节省时间,因为
n = pow(10*n,-m)
you now have to calculate (using the definition l = log10(n))
现在需要计算(使用定义l = log10(n)))
l = -m*(l+1)
#7
0
Just some more ideas which may lead you to further solutions...
再给你一些建议,也许能让你找到更好的解决办法……
-
If you are interested in optimization on algorithm level you might look for a parallelized approach.
如果您对算法级的优化感兴趣,您可以寻找一种并行的方法。
-
You may speed up on system/archtectural level on using Ipp (for Intel Processors), or e.g. AMD Core Math Library (ACML) for AMD
您可以在使用Ipp(用于英特尔处理器)或例如AMD核心数学库(ACML)时,在系统/技术层面上加快速度
-
To use the power of your graphics card may be another way (e.g. CUDA for NVIDEA cards)
使用显卡的功能可能是另一种方式(例如,CUDA表示NVIDEA)
-
I think it's also worth to look at OpenCL
我认为OpenCL也值得一看
#8
0
IEEE 754 specifies a bunch of floating-point formats. Those that are in widespread use are binary, which means that base 10 isn't in any way special. This is contrary to your assumption that "10 is also a special base".
IEEE 754指定了一组浮点格式。那些被广泛使用的是二进制,这意味着基数为10并没有什么特别之处。这与“10也是一个特殊基数”的假设相反。
Interestingly, IEEE 754-2008 does add decimal floating-point formats (decimal32
and friends). However, I'm yet to come across hardware implementations of those.
有趣的是,IEEE 754-2008确实增加了十进制浮点格式(decimal32和friends)。但是,我还没有遇到这些硬件实现。
In any case, you shouldn't be micro-optimizing your code before you've profiled it and established that this is indeed the bottleneck.
在任何情况下,您都不应该在对代码进行分析之前进行微优化,并确定这确实是瓶颈。
#9
0
If m is integer, just compute 10^m and calculate the inverse (1 / 10^m). Using exponentiation by squaring time will be proportional to log2(m). Using float, max m will be close to 1000, so you can just precompute using exponentiation by squaring and use a lookup table.
如果m是整数,计算10 ^ m和计算逆(^ 1/10米)。用指数除以平方时间将与log2(m)成正比。使用浮点数,最大m将接近1000,所以您可以通过平方和使用查找表来预计算指数。
#1
5
For floating point m, so long as your standard library implementation is well written, then pow
will be efficient.
对于浮点m,只要您的标准库实现写得很好,那么pow将是有效的。
If m is an integer, and you hinted that it is, then you could use an array of pre calculated values.
如果m是一个整数,并且您暗示它是整数,那么您可以使用一个预计算值数组。
You should only be worrying about this kind of thing if that routine is a bottleneck in your code. That is if the calls to that routine take a significant proportion of the total running time.
如果这个例程是代码中的瓶颈,那么您就应该担心这种情况。这是如果对该例程的调用占总运行时间的很大一部分。
#2
4
Ten is not a special value on a binary machine, only two is. Use pow
or exponentiation by squaring.
十不是二进制机器上的特殊值,只有两个。用平方运算或指数运算。
#3
2
Unfortunately there is no fast and efficient way to calculate it using IEEE 754 floating point representation. The fastest way to get the result is to build a table for every value of m
that you care about, and then just perform a lookup.
不幸的是,没有快速有效的方法来计算它使用IEEE 754浮点表示法。得到结果的最快方法是为您关心的每一个值构建一个表,然后执行查找。
#4
0
If there's a fast and efficient way to do it then I'm sure your CPU supports it, unless you're running on an embedded system in which case I'd hope that the pow(...) implementation is well written.
如果有一种快速且有效的方法,那么我肯定您的CPU支持它,除非您在嵌入式系统上运行,在这种情况下,我希望pow(…)实现写得很好。
10 is special to us as most of us have ten fingers. Computers only have two digits, so 2 is special to them. :)
10对我们来说很特别,因为我们大多数人都有十个手指。计算机只有两个数字,所以2对它们来说是特殊的。:)
#5
0
Use lookup table there cant be more than 1000 floats and especially if m is integer.
使用查找表不能有超过1000个浮点数,特别是如果m是整数。
#6
0
If you could operate with log n instead of n for a significant time, you could save time because instead of
如果你可以用log n而不是n来操作很长一段时间,你可以节省时间,因为
n = pow(10*n,-m)
you now have to calculate (using the definition l = log10(n))
现在需要计算(使用定义l = log10(n)))
l = -m*(l+1)
#7
0
Just some more ideas which may lead you to further solutions...
再给你一些建议,也许能让你找到更好的解决办法……
-
If you are interested in optimization on algorithm level you might look for a parallelized approach.
如果您对算法级的优化感兴趣,您可以寻找一种并行的方法。
-
You may speed up on system/archtectural level on using Ipp (for Intel Processors), or e.g. AMD Core Math Library (ACML) for AMD
您可以在使用Ipp(用于英特尔处理器)或例如AMD核心数学库(ACML)时,在系统/技术层面上加快速度
-
To use the power of your graphics card may be another way (e.g. CUDA for NVIDEA cards)
使用显卡的功能可能是另一种方式(例如,CUDA表示NVIDEA)
-
I think it's also worth to look at OpenCL
我认为OpenCL也值得一看
#8
0
IEEE 754 specifies a bunch of floating-point formats. Those that are in widespread use are binary, which means that base 10 isn't in any way special. This is contrary to your assumption that "10 is also a special base".
IEEE 754指定了一组浮点格式。那些被广泛使用的是二进制,这意味着基数为10并没有什么特别之处。这与“10也是一个特殊基数”的假设相反。
Interestingly, IEEE 754-2008 does add decimal floating-point formats (decimal32
and friends). However, I'm yet to come across hardware implementations of those.
有趣的是,IEEE 754-2008确实增加了十进制浮点格式(decimal32和friends)。但是,我还没有遇到这些硬件实现。
In any case, you shouldn't be micro-optimizing your code before you've profiled it and established that this is indeed the bottleneck.
在任何情况下,您都不应该在对代码进行分析之前进行微优化,并确定这确实是瓶颈。
#9
0
If m is integer, just compute 10^m and calculate the inverse (1 / 10^m). Using exponentiation by squaring time will be proportional to log2(m). Using float, max m will be close to 1000, so you can just precompute using exponentiation by squaring and use a lookup table.
如果m是整数,计算10 ^ m和计算逆(^ 1/10米)。用指数除以平方时间将与log2(m)成正比。使用浮点数,最大m将接近1000,所以您可以通过平方和使用查找表来预计算指数。