MCPcopy Create free account
hub / github.com/effect-app/libs / divideWithPrecision

Function divideWithPrecision

repos/effect/packages/effect/src/BigDecimal.ts:472–521  ·  view source on GitHub ↗
(
  num: bigint,
  den: bigint,
  scale: number,
  precision: number
)

Source from the content-addressed store, hash-verified

470 precision: number
471): BigDecimal => {
472 const numNegative = num < bigint0
473 const denNegative = den < bigint0
474 const negateResult = numNegative !== denNegative
475
476 num = numNegative ? -num : num
477 den = denNegative ? -den : den
478
479 // Shift digits until numerator is larger than denominator (set scale appropriately).
480 while (num < den) {
481 num *= bigint10
482 scale++
483 }
484
485 // First division.
486 let quotient = num / den
487 let remainder = num % den
488
489 if (remainder === bigint0) {
490 // No remainder, return immediately.
491 return make(negateResult ? -quotient : quotient, scale)
492 }
493
494 // The quotient is guaranteed to be non-negative at this point. No need to consider sign.
495 let count = `${quotient}`.length
496
497 // Shift the remainder by 1 decimal; The quotient will be 1 digit upon next division.
498 remainder *= bigint10
499 while (remainder !== bigint0 && count < precision) {
500 const q = remainder / den
501 const r = remainder % den
502 quotient = quotient * bigint10 + q
503 remainder = r * bigint10
504
505 count++
506 scale++
507 }
508
509 if (remainder !== bigint0) {
510 // Round final number with remainder.
511 quotient += roundTerminal(remainder / den)
512 }
513
514 return make(negateResult ? -quotient : quotient, scale)
515}
516
517/**
518 * Internal function used for rounding.
519 *
520 * Returns 1 if the most significant digit is >= 5, otherwise 0.
521 *
522 * This is used after dividing a number by a power of ten and rounding the last digit.
523 *
524 * @internal

Callers 1

BigDecimal.tsFile · 0.85

Calls 2

roundTerminalFunction · 0.85
makeFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…