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

Method mulToPrecision

src/big-decimal/big-decimal.ts:558–589  ·  view source on GitHub ↗

* Fused multiply-then-round: equivalent to `this.mul(other).toPrecision(prec)` * but skips the intermediate normalize of the full-width product and the * `bigintDigits` re-scan that `toPrecision` would otherwise run on it — the * same double-work class the `div` fix eliminated. * * Th

(other: BigDecimal | number, prec: number)

Source from the content-addressed store, hash-verified

556 * @internal
557 */
558 _mulToPrecision(other: BigDecimal | number, prec: number): BigDecimal {
559 if (typeof other === 'number') other = new BigDecimal(other);
560
561 const thisExp = this.exponent;
562 const otherExp = other.exponent;
563 const thisSig = this.significand;
564 const otherSig = other.significand;
565
566 // Fast path: both finite and nonzero (the product is a plain integer
567 // multiply whose digit count is derivable from the operand sizes).
568 if (
569 thisSig !== 0n &&
570 otherSig !== 0n &&
571 Number.isFinite(thisExp) &&
572 Number.isFinite(otherExp)
573 ) {
574 const productSig = thisSig * otherSig;
575 const productExp = thisExp + otherExp;
576 // da+db-1 ≤ digits(|a·b|) ≤ da+db; one cached-pow10 compare resolves ±1.
577 const lo = this._digitCount() + other._digitCount() - 1;
578 const absProd = productSig < 0n ? -productSig : productSig;
579 const rawDigits = absProd >= pow10(lo) ? lo + 1 : lo;
580 if (rawDigits <= prec) return fromRaw(productSig, productExp);
581 return rawUnnormalized(productSig, productExp).roundToPrecKnownDigits(
582 prec,
583 rawDigits
584 );
585 }
586
587 // Slow path: NaN, Infinity, or a zero operand — defer to the plain path.
588 return this.mul(other).toPrecision(prec);
589 }
590
591 /**
592 * Negate this value. Zero.neg() → Zero.

Callers 12

transcendentals.tsFile · 0.80
powMethod · 0.80
powFunction · 0.80
gammalnCoreFunction · 0.80
digammaCoreFunction · 0.80
trigammaCoreFunction · 0.80
polygammaCoreFunction · 0.80
_bignumComponentMethod · 0.80
powMethod · 0.80
rootMethod · 0.80
expMethod · 0.80

Calls 8

digitCountMethod · 0.95
mulMethod · 0.95
pow10Function · 0.90
fromRawFunction · 0.85
rawUnnormalizedFunction · 0.85
toPrecisionMethod · 0.80
isFiniteMethod · 0.45

Tested by

no test coverage detected