public BigDecimal subtract(BigDecimal subtrahend) { int diffScale = this._scale - subtrahend._scale; // Fast return when some operand is zero if (this.isZero()) { if (diffScale <= 0) { return subtrahend.negate(); } if (subtrahend.isZero()) { return this; } } else if (subtrahend.isZero()) { if (diffScale >= 0) { return this; } } // Let be: this = [u1,s1] and subtrahend = [u2,s2] so: if (diffScale == 0) { // case s1 = s2 : [u1 - u2 , s1] if (Math.Max(this._bitLength, subtrahend._bitLength) + 1 < 64) { return valueOf(this.smallValue - subtrahend.smallValue,this._scale); } return new BigDecimal(this.getUnscaledValue().subtract(subtrahend.getUnscaledValue()), this._scale); } else if (diffScale > 0) { // case s1 > s2 : [ u1 - u2 * 10 ^ (s1 - s2) , s1 ] if(diffScale < LONG_TEN_POW.Length && Math.Max(this._bitLength,subtrahend._bitLength+LONG_TEN_POW_BIT_LENGTH[diffScale])+1<64) { return valueOf(this.smallValue-subtrahend.smallValue*LONG_TEN_POW[diffScale],this._scale); } return new BigDecimal(this.getUnscaledValue().subtract( Multiplication.multiplyByTenPow(subtrahend.getUnscaledValue(),diffScale)), this._scale); } else {// case s2 > s1 : [ u1 * 10 ^ (s2 - s1) - u2 , s2 ] diffScale = -diffScale; if(diffScale < LONG_TEN_POW.Length && Math.Max(this._bitLength+LONG_TEN_POW_BIT_LENGTH[diffScale],subtrahend._bitLength)+1<64) { return valueOf(this.smallValue*LONG_TEN_POW[diffScale]-subtrahend.smallValue,subtrahend._scale); } return new BigDecimal(Multiplication.multiplyByTenPow(this.getUnscaledValue(),diffScale) .subtract(subtrahend.getUnscaledValue()), subtrahend._scale); } }