public BigInteger remainder(BigInteger divisor) { if (divisor.sign == 0) { throw new ArithmeticException("BigInteger divide by zero"); } int thisLen = numberLength; int divisorLen = divisor.numberLength; if (((thisLen != divisorLen) ? ((thisLen > divisorLen) ? 1 : -1) : Elementary.compareArrays(digits, divisor.digits, thisLen)) == LESS) { return(this); } int resLength = divisorLen; int[] resDigits = new int[resLength]; if (resLength == 1) { resDigits[0] = Division.remainderArrayByInt(digits, thisLen, divisor.digits[0]); } else { int qLen = thisLen - divisorLen + 1; resDigits = Division.divide(null, qLen, digits, thisLen, divisor.digits, divisorLen); } BigInteger result = new BigInteger(sign, resLength, resDigits); result.cutOffLeadingZeroes(); return(result); }
public BigInteger divide(BigInteger divisor) { if (divisor.sign == 0) { throw new ArithmeticException("BigInteger divide by zero"); } int divisorSign = divisor.sign; if (divisor.isOne()) { return((divisor.sign > 0) ? this : this.negate()); } int thisSign = sign; int thisLen = numberLength; int divisorLen = divisor.numberLength; if (thisLen + divisorLen == 2) { long val = (digits[0] & 0xFFFFFFFFL) / (divisor.digits[0] & 0xFFFFFFFFL); if (thisSign != divisorSign) { val = -val; } return(valueOf(val)); } int cmp = ((thisLen != divisorLen) ? ((thisLen > divisorLen) ? 1 : -1) : Elementary.compareArrays(digits, divisor.digits, thisLen)); if (cmp == EQUALS) { return((thisSign == divisorSign) ? ONE : MINUS_ONE); } if (cmp == LESS) { return(ZERO); } int resLength = thisLen - divisorLen + 1; int[] resDigits = new int[resLength]; int resSign = ((thisSign == divisorSign) ? 1 : -1); if (divisorLen == 1) { Division.divideArrayByInt(resDigits, digits, thisLen, divisor.digits[0]); } else { Division.divide(resDigits, resLength, digits, thisLen, divisor.digits, divisorLen); } BigInteger result = new BigInteger(resSign, resLength, resDigits); result.cutOffLeadingZeroes(); return(result); }
public BigInteger[] divideAndRemainder(BigInteger divisor) { int divisorSign = divisor.sign; if (divisorSign == 0) { throw new ArithmeticException("BigInteger divide by zero"); } int divisorLen = divisor.numberLength; int[] divisorDigits = divisor.digits; if (divisorLen == 1) { return(Division.divideAndRemainderByInteger(this, divisorDigits[0], divisorSign)); } // res[0] is a quotient and res[1] is a remainder: int[] thisDigits = digits; int thisLen = numberLength; int cmp = (thisLen != divisorLen) ? ((thisLen > divisorLen) ? 1 : -1) : Elementary.compareArrays(thisDigits, divisorDigits, thisLen); if (cmp < 0) { return(new BigInteger[] { ZERO, this }); } int thisSign = sign; int quotientLength = thisLen - divisorLen + 1; int remainderLength = divisorLen; int quotientSign = ((thisSign == divisorSign) ? 1 : -1); int[] quotientDigits = new int[quotientLength]; int[] remainderDigits = Division.divide(quotientDigits, quotientLength, thisDigits, thisLen, divisorDigits, divisorLen); BigInteger result0 = new BigInteger(quotientSign, quotientLength, quotientDigits); BigInteger result1 = new BigInteger(thisSign, remainderLength, remainderDigits); result0.cutOffLeadingZeroes(); result1.cutOffLeadingZeroes(); return(new BigInteger[] { result0, result1 }); }