mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
mruby-numeric-ext (int_sqrt): translate Japanese comment (oops)
This commit is contained in:
@@ -1251,11 +1251,11 @@ mpz_bit_length(const mpz_t *x)
|
||||
{
|
||||
if (x->sz == 0 || x->sn == 0) return 0;
|
||||
|
||||
// 最上位 limb を取得(リトルエンディアン: 末尾)
|
||||
// Get the most significant limb (last element in little-endian order)
|
||||
size_t i = x->sz - 1;
|
||||
mp_limb high = x->p[i];
|
||||
|
||||
// high の中で最上位のビットを探す
|
||||
// Count the number of bits in the most significant limb
|
||||
size_t bits = 0;
|
||||
while (high != 0) {
|
||||
high >>= 1;
|
||||
@@ -1277,7 +1277,7 @@ mpz_sqrt(mrb_state *mrb, mpz_t *z, mpz_t *x)
|
||||
return;
|
||||
}
|
||||
|
||||
// 初期値の設定: bit-length の半分の位置に 1 を立てる
|
||||
// Estimate initial value: 1 << (bit_length(x) / 2)
|
||||
size_t xbits = mpz_bit_length(x);
|
||||
size_t sbit = (xbits + 1) / 2;
|
||||
mpz_t s, t;
|
||||
@@ -1286,14 +1286,16 @@ mpz_sqrt(mrb_state *mrb, mpz_t *z, mpz_t *x)
|
||||
|
||||
mpz_init(mrb, &t);
|
||||
|
||||
// ループ: s = (s + x / s) / 2
|
||||
// Iteratively refine s using Newton-Raphson method:
|
||||
// s = (s + x / s) / 2
|
||||
for (;;) {
|
||||
mpz_mdiv(mrb, &t, x, &s); // t = x / s
|
||||
mpz_add(mrb, &t, &t, &s); // t = s + x/s
|
||||
mpz_div_2exp(mrb, &t, &t, 1); // t = (s + x/s) / 2
|
||||
|
||||
if (mpz_cmp(mrb, &t, &s) >= 0) {
|
||||
break; // 収束
|
||||
// Converged: t >= s
|
||||
break;
|
||||
}
|
||||
|
||||
mpz_set(mrb, &s, &t);
|
||||
|
||||
Reference in New Issue
Block a user