MCPcopy
hub / github.com/formatjs/formatjs / _compareTo

Method _compareTo

packages/bigdecimal/index.ts:527–570  ·  view source on GitHub ↗
(other: BigDecimal)

Source from the content-addressed store, hash-verified

525 }
526
527 private _compareTo(other: BigDecimal): number {
528 // Returns -1, 0, 1
529 if (
530 this._special === SpecialValue.NAN ||
531 other._special === SpecialValue.NAN
532 ) {
533 return NaN
534 }
535
536 // Handle infinities
537 if (this._special === SpecialValue.POSITIVE_INFINITY) {
538 return other._special === SpecialValue.POSITIVE_INFINITY ? 0 : 1
539 }
540 if (this._special === SpecialValue.NEGATIVE_INFINITY) {
541 return other._special === SpecialValue.NEGATIVE_INFINITY ? 0 : -1
542 }
543 if (other._special === SpecialValue.POSITIVE_INFINITY) return -1
544 if (other._special === SpecialValue.NEGATIVE_INFINITY) return 1
545
546 // Both finite - treat zeros as equal regardless of sign
547 const thisZero = this._mantissa === 0n
548 const otherZero = other._mantissa === 0n
549 if (thisZero && otherZero) return 0
550 if (thisZero) return other._mantissa > 0n ? -1 : 1
551 if (otherZero) return this._mantissa > 0n ? 1 : -1
552
553 // Different signs
554 const thisNeg = this._mantissa < 0n
555 const otherNeg = other._mantissa < 0n
556 if (thisNeg !== otherNeg) return thisNeg ? -1 : 1
557
558 // Same sign - align exponents and compare
559 let m1 = this._mantissa
560 let m2 = other._mantissa
561 const e1 = this._exponent
562 const e2 = other._exponent
563 const minE = Math.min(e1, e2)
564 if (e1 > minE) m1 *= bigintPow10(e1 - minE)
565 if (e2 > minE) m2 *= bigintPow10(e2 - minE)
566
567 if (m1 < m2) return -1
568 if (m1 > m2) return 1
569 return 0
570 }
571
572 lessThan(y: BigDecimal | number | string | bigint): boolean {
573 const c = this._compareTo(BigDecimal._coerce(y))

Callers 4

lessThanMethod · 0.95
greaterThanMethod · 0.95
lessThanOrEqualToMethod · 0.95
greaterThanOrEqualToMethod · 0.95

Calls 1

bigintPow10Function · 0.85

Tested by

no test coverage detected