MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / lnTwoFixed

Function lnTwoFixed

src/big-decimal/utils.ts:436–454  ·  view source on GitHub ↗
(bits: number)

Source from the content-addressed store, hash-verified

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 }

Callers 1

fplnAGMFunction · 0.85

Calls 4

pow10Function · 0.85
ln2ChudnovskyBitsFunction · 0.85
sliceMethod · 0.65
ceilMethod · 0.45

Tested by

no test coverage detected