MCPcopy
hub / github.com/formatjs/formatjs / _log10

Method _log10

packages/bigdecimal/index.ts:479–508  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

477 }
478
479 private _log10(): BigDecimal {
480 // log10(mantissa * 10^exponent) = log10(mantissa) + exponent
481 const absMantissa = bigintAbs(this._mantissa)
482 const digits = digitCount(absMantissa)
483 // integerPart = digits - 1 + exponent (this is floor(log10(x)) for exact powers)
484
485 // For high precision: compute log10 of the mantissa
486 // Normalize mantissa to [1, 10) by dividing by 10^(digits-1)
487 // Then log10(mantissa) = (digits-1) + log10(normalized)
488 // Total = (digits-1) + log10(normalized) + exponent
489
490 // Get the leading ~17 significant digits for Math.log10
491 // Use log laws: log10(leading × 10^shift) = log10(leading) + shift
492 // This avoids Math.pow(10, shift) overflow for shift > 308
493 let log10Mantissa: number
494 if (digits <= 15) {
495 log10Mantissa = Math.log10(Number(absMantissa))
496 } else {
497 const shift = digits - 17
498 const leading = absMantissa / bigintPow10(shift)
499 log10Mantissa = Math.log10(Number(leading)) + shift
500 }
501
502 // Total log10 = log10(mantissa) + exponent
503 const totalLog10 = log10Mantissa + this._exponent
504
505 // Convert to BigDecimal with ~18 digits of precision
506 // We use String conversion to preserve precision
507 return new BigDecimal(totalLog10)
508 }
509
510 // --- Comparison ---
511

Callers 1

logMethod · 0.95

Calls 3

bigintAbsFunction · 0.85
digitCountFunction · 0.85
bigintPow10Function · 0.85

Tested by

no test coverage detected