算法如下:
res <- 0
for i from 15 downto 0 do:
change the ith bit of result to 1
if res^2 > x then:
change the ith bit of res back to 0
return res
我完全明白它是如何工作的,但我不知道这个方法叫什么。我一直在维基百科上查找平方根的计算方法,但无济于事。这是逐个数字的方法吗?
(有关的:如何在不使用 div 的情况下计算 x86-64 中数字的整数平方根? https://stackoverflow.com/questions/46460138/how-to-compute-the-integer-square-root-of-a-number-in-x86-64-without-using-div)
正如彼得·科德斯在评论中提到的,这是逐位算法 https://en.wikipedia.org/wiki/Integer_square_root#Digit-by-digit_algorithm(这里是二进制数字)。
这是一种二分搜索。如果平方结果更接近,则设置第 i 位x
但不超过它,因此将所需的两个近似值相加结果会越来越好。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)