【发布时间】:2017-07-17 16:08:55
【问题描述】:
我正在寻找一些用于平方根计算的算法,并找到了这个源文件。我想尝试复制它,因为它看起来很简单,但我无法将它与某些已知算法(牛顿、巴比伦......)联系起来。能告诉我名字吗?
int sqrt(int num) {
int op = num;
int res = 0;
int one = 1 << 30; // The second-to-top bit is set: 1L<<30 for long
// "one" starts at the highest power of four <= the argument.
while (one > op)
one >>= 2;
while (one != 0) {
if (op >= res + one) {
op -= res + one;
res += 2 * one;
}
res >>= 1;
one >>= 2;
}
return res;
}
【问题讨论】:
-
"你能告诉我名字吗?"我将其称为“破碎”
sqrt(1073741824)--> -1 而不是预期的 32768。 -
"找到这个源文件" --> 在哪里?
-
@alukard990 VHDL 版本显示正常。您可以提醒该站点注意 C 版本中的缺陷。
标签: c algorithm square-root