C++ 计算机如何计算平方根?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12304577/
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
How does the computer calculate Square roots?
提问by Loers Antario
How does the computer calculate Square roots ? I mean what is going on there! How does it process it!! Does it use some mathematical ways like Newton's method? What about Trigonometric Functions? And almost all those Mathematical Functions . In the case that every language has its own way, then please let's talk about c++.
计算机如何计算平方根?我的意思是那里发生了什么!怎么处理啊!!它是否使用了一些像牛顿法这样的数学方法?三角函数呢?以及几乎所有那些数学函数。既然每种语言都有自己的方式,那我们就来谈谈c++吧。
回答by Stephen Canon
Most modern non-embedded CPUs (x86 and the larger ARM cores, for example) have hardware instructions to compute square roots directly. The hardware implementation backing these instructions varies, but typically is a variant on the schoolbook digit-by-digit algorithm (though not always in base two; base four or sixteen can also be used). These are typically among the slowest basic arithmetic operations on a CPU; timings like 16-64 cycles are not uncommon, and these instructions are often not pipelined.
大多数现代非嵌入式 CPU(例如 x86 和更大的 ARM 内核)都有直接计算平方根的硬件指令。支持这些指令的硬件实现各不相同,但通常是教科书逐位算法的变体(虽然并不总是以二为基数;也可以使用以四或十六为基数)。这些通常是 CPU 上最慢的基本算术运算;像 16-64 个周期这样的时序并不少见,而且这些指令通常不是流水线化的。
On CPUs that lack direct hardware square root instructions (Itanium, PPC, others), the typical approach is to generate an initial estimate (either with an instruction that produces the estimate, or with a lookup table) and then refine that estimate using an iterative method (Newton or Goldschmidt usually). You might track down some of Peter Markstein or Roger Golliver's writings on the subject if you're interested.
在缺少直接硬件平方根指令(Itanium、PPC 等)的 CPU 上,典型的方法是生成初始估计值(使用产生估计值的指令,或使用查找表),然后使用迭代改进该估计值方法(通常是牛顿或戈德施密特)。如果您有兴趣,您可以查找 Peter Markstein 或 Roger Golliver 关于该主题的一些著作。
More complex mathematical functions (like trig operations) are typically computed by reducing the argument into some fundamental domain and then approximating it with a polynomial or rational function. You can look at the sources of any of several math libraries that are available online for more detail (fdlibm is a good starting point).
更复杂的数学函数(如三角运算)通常是通过将参数减少到某个基本域,然后用多项式或有理函数逼近它来计算的。您可以查看在线提供的多个数学库中的任何一个的源以获取更多详细信息(fdlibm 是一个很好的起点)。
The x86 instruction set provides a number of instructions that support mathematical functions like exp, log, and sin, but these are not commonly used anymore, because good software library implementations give better performance.
x86 指令集提供了许多支持数学函数的指令,如 exp、log 和 sin,但这些不再常用,因为好的软件库实现提供了更好的性能。
回答by enriched
Good article on this http://www.codeproject.com/Articles/69941/Best-Square-Root-Method-Algorithm-Function-Precisishowing a comparison of the various methods that are used.
关于此http://www.codeproject.com/Articles/69941/Best-Square-Root-Method-Algorithm-Function-Precisi 的好文章显示了所使用的各种方法的比较。
回答by Jerry Coffin
Another possibility that hasn't been mentioned, is the CORDIC method. CORDIC isn't widely used/known in software, but is pretty common in hardware, and does pretty well at getting decent performance without using a lot of gates.
另一种尚未提及的可能性是CORDIC 方法。CORDIC 在软件中没有被广泛使用/广为人知,但在硬件中很常见,并且在不使用大量门的情况下获得不错的性能方面做得很好。
回答by user1598202
I think Newton's iterative convergence method is use in calculating Square Root
我认为牛顿的迭代收敛方法用于计算平方根
回答by Brett Hale
As others have pointed out, this is a very broad question - what works well for software might be a poor choice for a hardware implementation; and then there are issues of correct rounding for IEEE-754, hardware lookup tables, etc. There are a lot of open source C libs, with libm
implementations as well. A good overview of classic and modern methods can be found here.
正如其他人指出的那样,这是一个非常广泛的问题 - 对软件有效的方法对于硬件实现可能是一个糟糕的选择;然后是 IEEE-754、硬件查找表等的正确舍入问题。有很多开源 C 库,也有libm
实现。可以在此处找到经典和现代方法的良好概述。