2
我正在尋找一些平方根計算算法,並發現這個源文件。我想嘗試複製它,因爲它看起來很簡單,但我不能將它與一些已知的算法(牛頓,巴比倫...)聯繫起來。你能告訴我這個名字嗎?在網上找到的平方根源代碼
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;
}
它在[維基百科在 「數字-toDigit計算」]描述(https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Digit-by-digit_calculation) –
「你能告訴我名字嗎? 「我會把它叫做「sqrt」(1073741824)' - > -1而不是預期的32768. – chux
「找到這個源文件」 - >在哪裏? – chux