예제 #1
0
파일: Conversion.cs 프로젝트: vic/ioke
        /** @see BigInteger#doubleValue() */
        internal static double bigInteger2Double(BigInteger val)
        {
            // val.bitLength() < 64
            if ((val.numberLength < 2)
                || ((val.numberLength == 2) && (val.digits[1] > 0))) {
                return val.longValue();
            }
            // val.bitLength() >= 33 * 32 > 1024
            if (val.numberLength > 32) {
                return ((val.sign > 0) ? double.PositiveInfinity
                        : double.NegativeInfinity);
            }

            return Convert.ToDouble(val.ToString());
        }
예제 #2
0
파일: Conversion.cs 프로젝트: vic/ioke
        internal 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) {
                    v = -v;
                }
                return Convert.ToString(v, radix);
            }
            if ((radix == 10) || (radix < 0)
                || (radix > 26)) {
                return val.ToString();
            }
            double bitsForRadixDigit;
            bitsForRadixDigit = Math.Log(radix) / Math.Log(2);
            int resLengthInChars = (int) (val.abs().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 characters collecting them in the char array
                    resDigit = Division.divideArrayByInt(temp, temp, tempLen,
                                                         bigRadix);
                    int previous = currentChar;
                    do {
                        result[--currentChar] = Convert.ToString(resDigit % radix, radix)[0];
                    } 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] = Convert.ToString(resDigit % radix, 16)[0];
                    }
                }
            }
            while (result[currentChar] == '0') {
                currentChar++;
            }
            if (sign == -1) {
                result[--currentChar] = '-';
            }
            return new String(result, currentChar, resLengthInChars - currentChar);
        }