public static BigInteger Divide(BigInteger dividend, BigInteger divisor) { if (divisor.Sign == 0) { // math.17=BigInteger divide by zero throw new ArithmeticException(Messages.math17); //$NON-NLS-1$ } int divisorSign = divisor.Sign; if (divisor.IsOne) { return((divisor.Sign > 0) ? dividend : -dividend); } int thisSign = dividend.Sign; int thisLen = dividend.numberLength; int divisorLen = divisor.numberLength; if (thisLen + divisorLen == 2) { long val = (dividend.digits[0] & 0xFFFFFFFFL) / (divisor.digits[0] & 0xFFFFFFFFL); if (thisSign != divisorSign) { val = -val; } return(BigInteger.FromInt64(val)); } int cmp = ((thisLen != divisorLen) ? ((thisLen > divisorLen) ? 1 : -1) : Elementary.CompareArrays(dividend.digits, divisor.digits, thisLen)); if (cmp == BigInteger.EQUALS) { return((thisSign == divisorSign) ? BigInteger.One : BigInteger.MinusOne); } if (cmp == BigInteger.LESS) { return(BigInteger.Zero); } int resLength = thisLen - divisorLen + 1; int[] resDigits = new int[resLength]; int resSign = ((thisSign == divisorSign) ? 1 : -1); if (divisorLen == 1) { Division.DivideArrayByInt(resDigits, dividend.digits, thisLen, divisor.digits[0]); } else { Division.Divide(resDigits, resLength, dividend.digits, thisLen, divisor.digits, divisorLen); } BigInteger result = new BigInteger(resSign, resLength, resDigits); result.CutOffLeadingZeroes(); return(result); }
/** * Computes the quotient and the remainder after a division by an {@code int} * number. * * @return an array of the form {@code [quotient, remainder]}. */ public static BigInteger[] DivideAndRemainderByInteger(BigInteger val, int divisor, int divisorSign) { // res[0] is a quotient and res[1] is a remainder: int[] valDigits = val.Digits; int valLen = val.numberLength; int valSign = val.Sign; if (valLen == 1) { long a = (valDigits[0] & 0xffffffffL); long b = (divisor & 0xffffffffL); long quo = a / b; long rem = a % b; if (valSign != divisorSign) { quo = -quo; } if (valSign < 0) { rem = -rem; } return(new BigInteger[] { BigInteger.FromInt64(quo), BigInteger.FromInt64(rem) }); } int quotientLength = valLen; int quotientSign = ((valSign == divisorSign) ? 1 : -1); int[] quotientDigits = new int[quotientLength]; int[] remainderDigits; remainderDigits = new int[] { Division.DivideArrayByInt( quotientDigits, valDigits, valLen, divisor) }; BigInteger result0 = new BigInteger(quotientSign, quotientLength, quotientDigits); BigInteger result1 = new BigInteger(valSign, 1, remainderDigits); result0.CutOffLeadingZeroes(); result1.CutOffLeadingZeroes(); return(new BigInteger[] { result0, result1 }); }
/** @see BigInteger#ToString(int) */ public static string BigInteger2String(BigInteger val, int radix) { int sign = val.Sign; int numberLength = val.numberLength; int[] digits = val.Digits; if (sign == 0) { return("0"); //$NON-NLS-1$ } if (numberLength == 1) { int highDigit = digits[numberLength - 1]; long v = highDigit & 0xFFFFFFFFL; if (sign < 0) { // Long.ToString has different semantic from C# for negative numbers return("-" + Convert.ToString(v, radix)); } return(Convert.ToString(v, radix)); } if ((radix == 10) || (radix < CharHelper.MIN_RADIX) || (radix > CharHelper.MAX_RADIX)) { return(val.ToString()); } double bitsForRadixDigit; bitsForRadixDigit = System.Math.Log(radix) / System.Math.Log(2); int resLengthInChars = (int)(BigMath.Abs(val).BitLength / bitsForRadixDigit + ((sign < 0) ? 1 : 0)) + 1; char[] result = new char[resLengthInChars]; int currentChar = resLengthInChars; int resDigit; if (radix != 16) { int[] temp = new int[numberLength]; Array.Copy(digits, 0, temp, 0, numberLength); int tempLen = numberLength; int charsPerInt = digitFitInInt[radix]; int i; // get the maximal power of radix that fits in int int bigRadix = bigRadices[radix - 2]; while (true) { // divide the array of digits by bigRadix and convert remainders // to CharHelpers collecting them in the char array resDigit = Division.DivideArrayByInt(temp, temp, tempLen, bigRadix); int previous = currentChar; do { result[--currentChar] = CharHelper.forDigit( resDigit % radix, radix); } while (((resDigit /= radix) != 0) && (currentChar != 0)); int delta = charsPerInt - previous + currentChar; for (i = 0; i < delta && currentChar > 0; i++) { result[--currentChar] = '0'; } for (i = tempLen - 1; (i > 0) && (temp[i] == 0); i--) { ; } tempLen = i + 1; if ((tempLen == 1) && (temp[0] == 0)) // the quotient is 0 { break; } } } else { // radix == 16 for (int i = 0; i < numberLength; i++) { for (int j = 0; (j < 8) && (currentChar > 0); j++) { resDigit = digits[i] >> (j << 2) & 0xf; result[--currentChar] = CharHelper.forDigit(resDigit, 16); } } } while (result[currentChar] == '0') { currentChar++; } if (sign == -1) { result[--currentChar] = '-'; } return(new String(result, currentChar, resLengthInChars - currentChar)); }