* 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)
| 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. |
no test coverage detected