private static bool TryParse(string s, out MathContext context, out Exception exception)
        {
            char[] charVal = s.ToCharArray();
            int    i;          // Index of charVal
            int    j;          // Index of chRoundingMode
            int    digit;      // It will contain the digit parsed

            if ((charVal.Length < 27) || (charVal.Length > 45))
            {
                // math.0E=bad string format
                exception = new FormatException(Messages.math0E);                 //$NON-NLS-1$
                context   = null;
                return(false);
            }

            // Parsing "precision=" String
            for (i = 0; (i < chPrecision.Length) && (charVal[i] == chPrecision[i]); i++)
            {
            }

            if (i < chPrecision.Length)
            {
                // math.0E=bad string format
                throw new FormatException(Messages.math0E);                 //$NON-NLS-1$
            }
            // Parsing the value for "precision="...
            digit = CharHelper.toDigit(charVal[i], 10);
            if (digit == -1)
            {
                // math.0E=bad string format
                exception = new FormatException(Messages.math0E);                 //$NON-NLS-1$
                context   = null;
                return(false);
            }

            var precision = digit;

            i++;

            do
            {
                digit = CharHelper.toDigit(charVal[i], 10);
                if (digit == -1)
                {
                    if (charVal[i] == ' ')
                    {
                        // It parsed all the digits
                        i++;
                        break;
                    }

                    // It isn't  a valid digit, and isn't a white space
                    // math.0E=bad string format
                    exception = new ArgumentException(Messages.math0E);                     //$NON-NLS-1$
                    context   = null;
                    return(false);
                }
                // Accumulating the value parsed
                precision = precision * 10 + digit;
                if (precision < 0)
                {
                    // math.0E=bad string format
                    exception = new ArgumentException(Messages.math0E);                     //$NON-NLS-1$
                    context   = null;
                    return(false);
                }

                i++;
            } while (true);
            // Parsing "roundingMode="
            for (j = 0; (j < chRoundingMode.Length) &&
                 (charVal[i] == chRoundingMode[j]); i++, j++)
            {
                ;
            }

            if (j < chRoundingMode.Length)
            {
                // math.0E=bad string format
                throw new FormatException(Messages.math0E);                 //$NON-NLS-1$
            }

            RoundingMode roundingMode;

            try {
                // Parsing the value for "roundingMode"...
                roundingMode = (RoundingMode)Enum.Parse(typeof(RoundingMode), new string(charVal, i, charVal.Length - i), true);
            } catch (Exception ex) {
                exception = ex;
                context   = null;
                return(false);
            }

            exception = null;
            context   = new MathContext(precision, roundingMode);
            return(true);
        }
        /** @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));
        }
示例#3
0
        /**
         * Constructs a new {@code MathContext} from a string. The string has to
         * specify the precision and the rounding mode to be used and has to follow
         * the following syntax: "precision=&lt;precision&gt; roundingMode=&lt;roundingMode&gt;"
         * This is the same form as the one returned by the {@link #ToString}
         * method.
         *
         * @param val
         *            a string describing the precision and rounding mode for the
         *            new {@code MathContext}.
         * @throws IllegalArgumentException
         *             if the string is not in the correct format or if the
         *             precision specified is < 0.
         */
        public MathContext(String val)
        {
            char[] charVal = val.ToCharArray();
            int    i;          // Index of charVal
            int    j;          // Index of chRoundingMode
            int    digit;      // It will contain the digit parsed

            if ((charVal.Length < 27) || (charVal.Length > 45))
            {
                // math.0E=bad string format
                throw new ArgumentException(Messages.math0E);                 //$NON-NLS-1$
            }
            // Parsing "precision=" String
            for (i = 0; (i < chPrecision.Length) && (charVal[i] == chPrecision[i]); i++)
            {
                ;
            }

            if (i < chPrecision.Length)
            {
                // math.0E=bad string format
                throw new ArgumentException(Messages.math0E);                 //$NON-NLS-1$
            }
            // Parsing the value for "precision="...
            digit = CharHelper.toDigit(charVal[i], 10);
            if (digit == -1)
            {
                // math.0E=bad string format
                throw new ArgumentException(Messages.math0E);                 //$NON-NLS-1$
            }
            this.precision = this.precision * 10 + digit;
            i++;

            do
            {
                digit = CharHelper.toDigit(charVal[i], 10);
                if (digit == -1)
                {
                    if (charVal[i] == ' ')
                    {
                        // It parsed all the digits
                        i++;
                        break;
                    }
                    // It isn't  a valid digit, and isn't a white space
                    // math.0E=bad string format
                    throw new ArgumentException(Messages.math0E);                     //$NON-NLS-1$
                }
                // Accumulating the value parsed
                this.precision = this.precision * 10 + digit;
                if (this.precision < 0)
                {
                    // math.0E=bad string format
                    throw new ArgumentException(Messages.math0E);                     //$NON-NLS-1$
                }
                i++;
            } while (true);
            // Parsing "roundingMode="
            for (j = 0; (j < chRoundingMode.Length) &&
                 (charVal[i] == chRoundingMode[j]); i++, j++)
            {
                ;
            }

            if (j < chRoundingMode.Length)
            {
                // math.0E=bad string format
                throw new ArgumentException(Messages.math0E);                 //$NON-NLS-1$
            }
            // Parsing the value for "roundingMode"...
            this.roundingMode = (RoundingMode)Enum.Parse(typeof(RoundingMode), new string(charVal, i, charVal.Length - i), true);
        }