// TODO: must be verified
        public decimal ToDecimal()
        {
            var scaleDivisor = BigMath.Pow(BigInteger.FromInt64(10), _scale);
            var remainder    = BigMath.Remainder(GetUnscaledValue(), scaleDivisor);
            var scaledValue  = GetUnscaledValue() / scaleDivisor;

            var leftOfDecimal  = (decimal)scaledValue;
            var rightOfDecimal = (remainder) / ((decimal)scaleDivisor);

            return(leftOfDecimal + rightOfDecimal);
        }
        /**
         * Multiplies a number by a power of five.
         * This method is used in {@code BigDecimal} class.
         * @param val the number to be multiplied
         * @param exp a positive {@code int} exponent
         * @return {@code val * 5<sup>exp</sup>}
         */

        public static BigInteger MultiplyByFivePow(BigInteger val, int exp)
        {
            // PRE: exp >= 0
            if (exp < FivePows.Length)
            {
                return(MultiplyByPositiveInt(val, FivePows[exp]));
            }
            else if (exp < BigFivePows.Length)
            {
                return(val * BigFivePows[exp]);
            }
            else                // Large powers of five
            {
                return(val * BigMath.Pow(BigFivePows[1], exp));
            }
        }
        public static BigDecimal Pow(BigDecimal number, int n)
        {
            if (n == 0)
            {
                return(BigDecimal.One);
            }
            if ((n < 0) || (n > 999999999))
            {
                // math.07=Invalid Operation
                throw new ArithmeticException(Messages.math07);                 //$NON-NLS-1$
            }
            long newScale = number.Scale * (long)n;

            // Let be: this = [u,s]   so:  this^n = [u^n, s*n]
            return((number.IsZero)
                                ? BigDecimal.GetZeroScaledBy(newScale)
                                : new BigDecimal(BigMath.Pow(number.UnscaledValue, n), BigDecimal.ToIntScale(newScale)));
        }
 public BigIntegerTest()
 {
     twoToTheSeventy = BigMath.Pow(two, 70);
     SetUp();
 }
 public void PowI()
 {
     Assert.True(BigMath.Pow(two, 10).Equals(twoToTheTen), "Incorrect exponent returned for 2**10");
     Assert.True((BigMath.Pow(two, 30) * BigMath.Pow(two, 40)).Equals(twoToTheSeventy), "Incorrect exponent returned for 2**70");
     Assert.True(BigMath.Pow(ten, 50).Equals(aZillion), "Incorrect exponent returned for 10**50");
 }
        /**
         * It calculates a power of ten, which exponent could be out of 32-bit range.
         * Note that internally this method will be used in the worst case with
         * an exponent equals to: {@code Integer.MAX_VALUE - Integer.MIN_VALUE}.
         * @param exp the exponent of power of ten, it must be positive.
         * @return a {@code BigInteger} with value {@code 10<sup>exp</sup>}.
         */

        public static BigInteger PowerOf10(long exp)
        {
            // PRE: exp >= 0
            int intExp = (int)exp;

            // "SMALL POWERS"
            if (exp < BigTenPows.Length)
            {
                // The largest power that fit in 'long' type
                return(BigTenPows[intExp]);
            }
            else if (exp <= 50)
            {
                // To calculate:    10^exp
                return(BigMath.Pow(BigInteger.Ten, intExp));
            }
            else if (exp <= 1000)
            {
                // To calculate:    5^exp * 2^exp
                return(BigMath.Pow(BigFivePows[1], intExp) << intExp);
            }
            // "LARGE POWERS"

            /*
             * To check if there is free memory to allocate a BigInteger of the
             * estimated size, measured in bytes: 1 + [exp / log10(2)]
             */
            var byteArraySize = 1 + (exp / 2.4082399653118496);

            //if (byteArraySize > System.Diagnostics.Process.GetCurrentProcess().PeakVirtualMemorySize64) {
            //	// math.01=power of ten too big
            //	throw new ArithmeticException(Messages.math01); //$NON-NLS-1$
            //}

            // More than 128Mb
            if (byteArraySize > (128 * 1024))
            {
                throw new ArithmeticException(Messages.math01);
            }

            if (exp <= Int32.MaxValue)
            {
                // To calculate:    5^exp * 2^exp
                return(BigMath.Pow(BigFivePows[1], intExp) << intExp);
            }

            /*
             * "HUGE POWERS"
             *
             * This branch probably won't be executed since the power of ten is too
             * big.
             */
            // To calculate:    5^exp
            BigInteger powerOfFive = BigMath.Pow(BigFivePows[1], Int32.MaxValue);
            BigInteger res         = powerOfFive;
            long       longExp     = exp - Int32.MaxValue;

            intExp = (int)(exp % Int32.MaxValue);
            while (longExp > Int32.MaxValue)
            {
                res      = res * powerOfFive;
                longExp -= Int32.MaxValue;
            }
            res = res * BigMath.Pow(BigFivePows[1], intExp);
            // To calculate:    5^exp << exp
            res     = res << Int32.MaxValue;
            longExp = exp - Int32.MaxValue;
            while (longExp > Int32.MaxValue)
            {
                res      = res << Int32.MaxValue;
                longExp -= Int32.MaxValue;
            }
            res = res << intExp;
            return(res);
        }