(bits: number)
| 434 | y0Fp = BigInt(Math.round(Math.log(v) * 2 ** topBits)) << BigInt(sh); |
| 435 | } else { |
| 436 | const v = Number(x) / Number(scale); |
| 437 | y0Fp = BigInt(Math.round(Math.log(v) * Number(scale))); |
| 438 | } |
| 439 | |
| 440 | // p = x·e^{−y₀} = (1 + ε)·scale, ε ≈ 2^{−48}. The single fpexp call. |
| 441 | const p = (x * fpexp(-y0Fp, bits)) >> B; |
| 442 | const eps = p - scale; // ε·scale (may be negative if y₀ overshot ln v) |
| 443 | |
| 444 | // log1p(ε) = 2·atanh(u), u = ε/(2+ε). The atanh terms all share u's sign, so |
| 445 | // sum in magnitude: an arithmetic right-shift of a negative bigint floors |
| 446 | // toward −∞ and sticks at −1 instead of reaching 0, which would never |
| 447 | // terminate the loop. |
| 448 | const u = (eps << B) / (p + scale); |
| 449 | const uAbs = u < 0n ? -u : u; |
| 450 | const u2 = (uAbs * uAbs) >> B; |
| 451 | let term = uAbs; |
| 452 | let sum = uAbs; |
| 453 | for (let n = 3n; ; n += 2n) { |
| 454 | term = (term * u2) >> B; |
| 455 | if (term === 0n) break; |
| 456 | sum += term / n; |
| 457 | } |
no test coverage detected