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

Function fromString

src/big-decimal/big-decimal.ts:1508–1564  ·  view source on GitHub ↗

* Parse a decimal string into (significand, exponent). * * Accepted formats: * "123", "-42", "0" — integers * "123.456", "-0.001", ".5" — decimal point * "1.5e10", "1.5E-3", "-2.5e+4" — scientific notation * "00123.4500" — lea

(s: string)

Source from the content-addressed store, hash-verified

1506 let H = BigDecimal.ZERO; // Hₖ
1507
1508 // A term no longer affects the work-digit A/B once it is ~10^{−work} below
1509 // B. The term grows until k ≈ n then decreases monotonically, so this
1510 // terminates (near k ≈ 4n in practice); maxK is only a safety backstop.
1511 const relTol = BigDecimal.ONE.div(new BigDecimal(10).pow(work, work), work);
1512 const maxK = 6 * n + 100;
1513
1514 for (let k = 1; k <= maxK; k++) {
1515 const k2 = new BigDecimal(k).mul(k);
1516 term = term.mul(n2).div(k2, work).toPrecision(work); // ·(n/k)² (.div rounds)
1517 H = H.add(BigDecimal.ONE.div(k, work), work).toPrecision(work);
1518 B = B.add(term, work).toPrecision(work);
1519 A = A.add(term.mul(H), work).toPrecision(work);
1520 if (k > n && term.lt(B.mul(relTol))) break;
1521 }
1522
1523 return A.div(B, work).sub(nBig.ln(work), work).toPrecision(digits);
1524}
1525
1526/**
1527 * Create a BigDecimal directly from significand + exponent, normalizing.
1528 * Avoids the constructor's string/number parsing overhead.
1529 */
1530export function fromRaw(sig: bigint, exp: number): BigDecimal {
1531 const [normSig, normExp] = normalize(sig, exp);
1532 // Use Object.create to avoid re-parsing; set readonly fields directly
1533 const bd = Object.create(BigDecimal.prototype) as BigDecimal;
1534 (bd as { significand: bigint }).significand = normSig;
1535 (bd as { exponent: number }).exponent = normExp;
1536 return bd;
1537}
1538
1539/**
1540 * The integer part `q` (truncated toward zero) and the fractional part `r`
1541 * of a finite value with a negative exponent, both as bigints: the value is
1542 * `q + r / unit`, and `r` has the sign of the value. When all the digits of
1543 * the value are past the decimal point and at least one zero follows the
1544 * point (`|x| < 0.1`), `q` is `0n`, `r` is the significand and `unit` is
1545 * `undefined` (it would be `10^(−exponent)`, which can be too large for a
1546 * bigint). Exact, and independent of any working precision.
1547 */
1548function splitAtPoint(x: BigDecimal): [bigint, bigint, bigint | undefined] {
1549 const shift = -x.exponent;
1550 if (shift > x._digitCount()) return [0n, x.significand, undefined];
1551 const unit = pow10(shift);
1552 return [x.significand / unit, x.significand % unit, unit];
1553}
1554
1555/**
1556 * The largest number of digits, below the last digit of the larger operand,
1557 * at which `addBounded` and `subBounded` still place the smaller operand
1558 * exactly. At least 10,000 digits, and 4 times the working precision plus
1559 * 100 above that.
1560 */
1561function maxExactGap(prec: number): number {
1562 return Math.max(10_000, 4 * prec + 100);
1563}
1564
1565/**

Callers 2

constructorMethod · 0.85
fromNumberFunction · 0.85

Calls 4

normalizeFunction · 0.70
sliceMethod · 0.65
replaceMethod · 0.65
isFiniteMethod · 0.45

Tested by

no test coverage detected