MCPcopy
hub / github.com/formatjs/formatjs / plus

Method plus

packages/bigdecimal/index.ts:277–305  ·  view source on GitHub ↗
(y: BigDecimal | number | string | bigint)

Source from the content-addressed store, hash-verified

275 }
276
277 plus(y: BigDecimal | number | string | bigint): BigDecimal {
278 const other = BigDecimal._coerce(y)
279 if (this._special || other._special) {
280 return this._specialArith(other, 'plus')
281 }
282 if (this._mantissa === 0n && other._mantissa === 0n) {
283 // -0 + -0 = -0, otherwise 0
284 const negZero = this._negativeZero && other._negativeZero
285 return BigDecimal._create(0n, 0, SpecialValue.NONE, negZero)
286 }
287 if (this._mantissa === 0n) return other
288 if (other._mantissa === 0n) return this
289
290 // Align exponents
291 let m1 = this._mantissa
292 let m2 = other._mantissa
293 const e1 = this._exponent
294 const e2 = other._exponent
295 const minE = Math.min(e1, e2)
296 if (e1 > minE) m1 *= bigintPow10(e1 - minE)
297 if (e2 > minE) m2 *= bigintPow10(e2 - minE)
298
299 const sum = m1 + m2
300 if (sum === 0n) {
301 return BigDecimal._create(0n, 0, SpecialValue.NONE, false)
302 }
303 const [nm, ne] = removeTrailingZeros(sum, minE)
304 return BigDecimal._create(nm, ne, SpecialValue.NONE, false)
305 }
306
307 minus(y: BigDecimal | number | string | bigint): BigDecimal {
308 return this.plus(BigDecimal._coerce(y).negated())

Callers 7

minusMethod · 0.95
bigdecimal.test.tsFile · 0.80
ToLocalTimeFunction · 0.80
ComputeExponentFunction · 0.80
findN1E1R1Function · 0.80
findN2E2R2Function · 0.80

Calls 5

_specialArithMethod · 0.95
bigintPow10Function · 0.85
removeTrailingZerosFunction · 0.85
_coerceMethod · 0.80
_createMethod · 0.80

Tested by

no test coverage detected