예제 #1
0
        internal string ToDecimal()
        {
            if (IsZero())
            {
                return("0");
            }

            BigInt ten       = new BigInt(0xA);
            BigInt numerator = new BigInt();
            BigInt quotient  = new BigInt();
            BigInt remainder = new BigInt();

            numerator.CopyFrom(this);

            // Each hex digit can account for log(16) = 1.21 decimal digits. Times two hex digits in a byte
            // and m_size bytes used in this BigInt, yields the maximum number of characters for the decimal
            // representation of the BigInt.
            char[] dec = new char[(int)Math.Ceiling(m_size * 2 * 1.21)];

            int index = 0;

            do
            {
                Divide(numerator, ten, ref quotient, ref remainder);
                dec[index++] = decValues[remainder.IsZero() ? 0 : (int)remainder.m_elements[0]];
                numerator.CopyFrom(quotient);
            } while (quotient.IsZero() == false);

            Array.Reverse(dec, 0, index);
            return(new String(dec, 0, index));
        }
예제 #2
0
 internal static void Divide(BigInt numerator, BigInt denominator, ref BigInt quotient, ref BigInt remainder)
 {
     if (numerator < denominator)
     {
         quotient.Clear();
         remainder.CopyFrom(numerator);
     }
     else if (numerator == denominator)
     {
         quotient.Clear();
         quotient.SetDigit(0, 1);
         remainder.Clear();
     }
     else
     {
         BigInt a = new BigInt();
         a.CopyFrom(numerator);
         BigInt num2 = new BigInt();
         num2.CopyFrom(denominator);
         uint num3 = 0;
         while (num2.Size < a.Size)
         {
             num2.Multiply(0x100);
             num3++;
         }
         if (num2 > a)
         {
             num2.Divide(0x100);
             num3--;
         }
         int    num4  = 0;
         int    digit = 0;
         int    b     = 0;
         BigInt c     = new BigInt();
         quotient.Clear();
         for (int i = 0; i <= num3; i++)
         {
             num4  = (a.Size == num2.Size) ? a.GetDigit(a.Size - 1) : ((0x100 * a.GetDigit(a.Size - 1)) + a.GetDigit(a.Size - 2));
             digit = num2.GetDigit(num2.Size - 1);
             b     = num4 / digit;
             if (b >= 0x100)
             {
                 b = 0xff;
             }
             Multiply(num2, b, ref c);
             while (c > a)
             {
                 b--;
                 Multiply(num2, b, ref c);
             }
             quotient.Multiply(0x100);
             Add(quotient, (byte)b, ref quotient);
             Subtract(a, c, ref a);
             num2.Divide(0x100);
         }
         remainder.CopyFrom(a);
     }
 }
 internal static void Divide(BigInt numerator, BigInt denominator, ref BigInt quotient, ref BigInt remainder)
 {
     if (numerator < denominator)
     {
         quotient.Clear();
         remainder.CopyFrom(numerator);
     }
     else if (numerator == denominator)
     {
         quotient.Clear();
         quotient.SetDigit(0, 1);
         remainder.Clear();
     }
     else
     {
         BigInt a = new BigInt();
         a.CopyFrom(numerator);
         BigInt num2 = new BigInt();
         num2.CopyFrom(denominator);
         uint num3 = 0;
         while (num2.Size < a.Size)
         {
             num2.Multiply(0x100);
             num3++;
         }
         if (num2 > a)
         {
             num2.Divide(0x100);
             num3--;
         }
         int num4 = 0;
         int digit = 0;
         int b = 0;
         BigInt c = new BigInt();
         quotient.Clear();
         for (int i = 0; i <= num3; i++)
         {
             num4 = (a.Size == num2.Size) ? a.GetDigit(a.Size - 1) : ((0x100 * a.GetDigit(a.Size - 1)) + a.GetDigit(a.Size - 2));
             digit = num2.GetDigit(num2.Size - 1);
             b = num4 / digit;
             if (b >= 0x100)
             {
                 b = 0xff;
             }
             Multiply(num2, b, ref c);
             while (c > a)
             {
                 b--;
                 Multiply(num2, b, ref c);
             }
             quotient.Multiply(0x100);
             Add(quotient, (byte) b, ref quotient);
             Subtract(a, c, ref a);
             num2.Divide(0x100);
         }
         remainder.CopyFrom(a);
     }
 }
예제 #4
0
        internal string ToDecimal()
        {
            if (this.IsZero())
            {
                return("0");
            }
            BigInt denominator = new BigInt(10);
            BigInt numerator   = new BigInt();
            BigInt quotient    = new BigInt();
            BigInt remainder   = new BigInt();

            numerator.CopyFrom(this);
            char[] array  = new char[(int)Math.Ceiling((double)((this.m_size * 2) * 1.21))];
            int    length = 0;

            do
            {
                Divide(numerator, denominator, ref quotient, ref remainder);
                array[length++] = decValues[remainder.IsZero() ? 0 : remainder.m_elements[0]];
                numerator.CopyFrom(quotient);
            }while (!quotient.IsZero());
            Array.Reverse(array, 0, length);
            return(new string(array, 0, length));
        }
예제 #5
0
        //
        // Integer division of one BigInt by another.
        //

        internal static void Divide(BigInt numerator, BigInt denominator, ref BigInt quotient, ref BigInt remainder)
        {
            // Avoid extra computations in special cases.

            if (numerator < denominator)
            {
                quotient.Clear();
                remainder.CopyFrom(numerator);
                return;
            }

            if (numerator == denominator)
            {
                quotient.Clear(); quotient.SetDigit(0, 1);
                remainder.Clear();
                return;
            }

            BigInt dividend = new BigInt();

            dividend.CopyFrom(numerator);
            BigInt divisor = new BigInt();

            divisor.CopyFrom(denominator);

            uint zeroCount = 0;

            // We pad the divisor with zeros until its size equals that of the dividend.
            while (divisor.Size < dividend.Size)
            {
                divisor.Multiply(m_base);
                zeroCount++;
            }

            if (divisor > dividend)
            {
                divisor.Divide(m_base);
                zeroCount--;
            }

            // Use school division techniques, make a guess for how many times
            // divisor goes into dividend, making adjustment if necessary.
            int a = 0;
            int b = 0;
            int c = 0;

            BigInt hold = new BigInt();

            quotient.Clear();
            for (int index = 0; index <= zeroCount; index++)
            {
                a = dividend.Size == divisor.Size ? dividend.GetDigit(dividend.Size - 1) :
                    m_base *dividend.GetDigit(dividend.Size - 1) + dividend.GetDigit(dividend.Size - 2);

                b = divisor.GetDigit(divisor.Size - 1);
                c = a / b;

                if (c >= m_base)
                {
                    c = 0xFF;
                }

                Multiply(divisor, c, ref hold);
                while (hold > dividend)
                {
                    c--;
                    Multiply(divisor, c, ref hold);
                }

                quotient.Multiply(m_base);
                Add(quotient, (byte)c, ref quotient);
                Subtract(dividend, hold, ref dividend);
                divisor.Divide(m_base);
            }
            remainder.CopyFrom(dividend);
        }
예제 #6
0
        internal string ToDecimal ()
        {
            if (IsZero())
                return "0";

            BigInt ten = new BigInt(0xA);
            BigInt numerator = new BigInt();
            BigInt quotient = new BigInt();
            BigInt remainder = new BigInt();

            numerator.CopyFrom(this);

            // Each hex digit can account for log(16) = 1.21 decimal digits. Times two hex digits in a byte
            // and m_size bytes used in this BigInt, yields the maximum number of characters for the decimal
            // representation of the BigInt.
            char[] dec = new char[(int)Math.Ceiling(m_size * 2 * 1.21)];

            int index = 0;
            do
            {
                Divide(numerator, ten, ref quotient, ref remainder);
                dec[index++] = decValues[remainder.IsZero() ? 0 : (int)remainder.m_elements[0]];
                numerator.CopyFrom(quotient);
            } while (quotient.IsZero() == false);

            Array.Reverse(dec, 0, index);
            return new String(dec, 0, index);
        }
예제 #7
0
        //
        // Integer division of one BigInt by another.
        //

        internal static void Divide (BigInt numerator, BigInt denominator, ref BigInt quotient, ref BigInt remainder) {
            // Avoid extra computations in special cases.

            if (numerator < denominator) {
                quotient.Clear();
                remainder.CopyFrom(numerator);
                return;
            }
    
            if (numerator == denominator) {
                quotient.Clear(); quotient.SetDigit(0, 1); 
                remainder.Clear();
                return;
            }

            BigInt dividend = new BigInt();
            dividend.CopyFrom(numerator);
            BigInt divisor = new BigInt();
            divisor.CopyFrom(denominator);

            uint zeroCount = 0;
            // We pad the divisor with zeros until its size equals that of the dividend.
            while (divisor.Size < dividend.Size) {
                divisor.Multiply(m_base);
                zeroCount++; 
            }

            if (divisor > dividend) {
                divisor.Divide(m_base);
                zeroCount--;
            }

            // Use school division techniques, make a guess for how many times
            // divisor goes into dividend, making adjustment if necessary.
            int a = 0;
            int b = 0;
            int c = 0;

            BigInt hold = new BigInt();
            quotient.Clear();
            for (int index = 0; index <= zeroCount; index++) {
                a = dividend.Size == divisor.Size ? dividend.GetDigit(dividend.Size - 1) :
                                                    m_base * dividend.GetDigit(dividend.Size - 1) + dividend.GetDigit(dividend.Size - 2);
                b = divisor.GetDigit(divisor.Size - 1);
                c = a / b;

                if (c >= m_base) 
                    c = 0xFF;

                Multiply(divisor, c, ref hold);
                while (hold > dividend) {
                    c--;
                    Multiply(divisor, c, ref hold);
                }

                quotient.Multiply(m_base);
                Add(quotient, (byte) c, ref quotient);
                Subtract(dividend, hold, ref dividend);
                divisor.Divide(m_base);
            }
            remainder.CopyFrom(dividend);
        }
 internal string ToDecimal()
 {
     if (this.IsZero())
     {
         return "0";
     }
     BigInt denominator = new BigInt(10);
     BigInt numerator = new BigInt();
     BigInt quotient = new BigInt();
     BigInt remainder = new BigInt();
     numerator.CopyFrom(this);
     char[] array = new char[(int) Math.Ceiling((double) ((this.m_size * 2) * 1.21))];
     int length = 0;
     do
     {
         Divide(numerator, denominator, ref quotient, ref remainder);
         array[length++] = decValues[remainder.IsZero() ? 0 : remainder.m_elements[0]];
         numerator.CopyFrom(quotient);
     }
     while (!quotient.IsZero());
     Array.Reverse(array, 0, length);
     return new string(array, 0, length);
 }