示例#1
0
        public void GetApproxParts(out int exp, out ulong man)
        {
            if (_iuLast == 0)
            {
                man = _uSmall;
                exp = 0;
                return;
            }

            var num = _iuLast - 1;

            man = NumericHelper.BuildUInt64(_bits[num + 1], _bits[num]);
            exp = num * 32;
            if (num <= 0)
            {
                return;
            }

            var num1 = NumericHelper.CbitHighZero(_bits[num + 1]);

            if (num1 <= 0)
            {
                return;
            }

            man  = (man << num1) | (_bits[num - 1] >> (32 - num1));
            exp -= num1;
        }
示例#2
0
        public uint DivMod(uint num5)
        {
            if (num5 == 1)
            {
                return(0);
            }

            if (_iuLast == 0)
            {
                var num = _uSmall;
                _uSmall = num / num5;
                return(num % num5);
            }

            EnsureWritable();
            var num1 = (ulong)0;

            for (var i = _iuLast; i >= 0; i--)
            {
                num1     = NumericHelper.BuildUInt64((uint)num1, _bits[i]);
                _bits[i] = (uint)(num1 / num5);
                num1    %= num5;
            }

            Trim();
            return((uint)num1);
        }
示例#3
0
        private ulong GetHigh2(int cu)
        {
            if (cu - 1 <= _iuLast)
            {
                return(NumericHelper.BuildUInt64(_bits[cu - 1], _bits[cu - 2]));
            }

            return(cu - 2 != _iuLast ? 0 : _bits[cu - 2]);
        }
示例#4
0
        private static ReverseStringBuilder CreateBuilder(BigInteger value, NumberFormatInfo info, bool decimalFmt, int digits)
        {
            // First convert to base 10^9.
            const uint numericBase      = 1000000000; // 10^9
            const int  numericBaseLog10 = 9;
            var        sourceLength     = Length(value.InternalBits);
            int        maxConvertedLength;

            try
            {
                maxConvertedLength = checked ((sourceLength * 10 / 9) + 2);
            }
            catch (OverflowException e)
            {
                throw new FormatException("The value is too large to be represented by this format specifier.", e);
            }

            var converted       = new uint[maxConvertedLength];
            var convertedLength = 0;

            for (var sourceIndex = sourceLength; --sourceIndex >= 0;)
            {
                // Take a cipher from the source
                var carry = value.InternalBits[sourceIndex];
                // Add it to converted
                for (var convertedIndex = 0; convertedIndex < convertedLength; convertedIndex++)
                {
                    ref var current     = ref converted[convertedIndex];
                    var     cipherBlock = NumericHelper.BuildUInt64(current, carry);
                    current = (uint)(cipherBlock % numericBase);
                    carry   = (uint)(cipherBlock / numericBase);
                }

                if (carry == 0)
                {
                    continue;
                }

                converted[convertedLength++] = carry % numericBase;
                carry /= numericBase;
                if (carry != 0)
                {
                    converted[convertedLength++] = carry;
                }
            }
示例#5
0
        public static uint Mod(ref BigIntegerBuilder regNum, uint num5)
        {
            if (num5 == 1)
            {
                return(0);
            }
            if (regNum._iuLast == 0)
            {
                return(regNum._uSmall % num5);
            }
            var num = (ulong)0;

            for (var i = regNum._iuLast; i >= 0; i--)
            {
                num = NumericHelper.BuildUInt64((uint)num, regNum._bits[i]);
                num = num % num5;
            }
            return((uint)num);
        }
示例#6
0
        private static void ModDivCore(ref BigIntegerBuilder regNum, ref BigIntegerBuilder regDen, bool fQuo, ref BigIntegerBuilder regQuo)
        {
            regQuo.Set(0);
            if (regNum._iuLast < regDen._iuLast)
            {
                return;
            }

            var num1 = regDen._iuLast + 1;
            var num2 = regNum._iuLast - regDen._iuLast;
            var num3 = num2;
            var num4 = regNum._iuLast;

            while (true)
            {
                if (num4 < num2)
                {
                    num3++;
                    break;
                }

                if (regDen._bits[num4 - num2] == regNum._bits[num4])
                {
                    num4--;
                }
                else
                {
                    if (regDen._bits[num4 - num2] < regNum._bits[num4])
                    {
                        num3++;
                    }

                    break;
                }
            }

            if (num3 == 0)
            {
                return;
            }

            if (fQuo)
            {
                regQuo.SetSizeLazy(num3);
            }

            var num5 = regDen._bits[num1 - 1];
            var num6 = regDen._bits[num1 - 2];
            var num7 = NumericHelper.CbitHighZero(num5);
            var num8 = 32 - num7;

            if (num7 > 0)
            {
                num5   = (num5 << (num7 & 31)) | (num6 >> (num8 & 31));
                num6 <<= num7 & 31;
                if (num1 > 2)
                {
                    num6 |= regDen._bits[num1 - 3] >> (num8 & 31);
                }
            }

            regNum.EnsureWritable();
            var num9 = num3;

            while (true)
            {
                var num10 = num9 - 1;
                num9 = num10;
                if (num10 < 0)
                {
                    break;
                }

                var num   = num9 + num1 > regNum._iuLast ? 0 : regNum._bits[num9 + num1];
                var num11 = num;
                var num12 = NumericHelper.BuildUInt64(num11, regNum._bits[num9 + num1 - 1]);
                var num13 = regNum._bits[num9 + num1 - 2];
                if (num7 > 0)
                {
                    num12   = (num12 << (num7 & 63)) | (num13 >> (num8 & 31));
                    num13 <<= num7 & 31;
                    if (num9 + num1 >= 3)
                    {
                        num13 |= regNum._bits[num9 + num1 - 3] >> (num8 & 31);
                    }
                }

                var num14 = num12 / num5;
                var num15 = (ulong)(uint)(num12 % num5);
                if (num14 > uint.MaxValue)
                {
                    num15 += num5 * (num14 - uint.MaxValue);
                    num14  = uint.MaxValue;
                }

                while (num15 <= uint.MaxValue && num14 * num6 > NumericHelper.BuildUInt64((uint)num15, num13))
                {
                    num14--;
                    num15 += num5;
                }

                if (num14 > 0)
                {
                    var num16 = (ulong)0;
                    for (var i = 0; i < num1; i++)
                    {
                        num16 += regDen._bits[i] * num14;
                        var num17 = (uint)num16;
                        num16 >>= 32;
                        if (regNum._bits[num9 + i] < num17)
                        {
                            num16++;
                        }

                        regNum._bits[num9 + i] -= num17;
                    }

                    if (num11 < num16)
                    {
                        uint num18 = 0;
                        for (var j = 0; j < num1; j++)
                        {
                            num18 = AddCarry(ref regNum._bits[num9 + j], regDen._bits[j], num18);
                        }

                        num14--;
                    }

                    regNum._iuLast = num9 + num1 - 1;
                }

                if (!fQuo)
                {
                    continue;
                }

                if (num3 != 1)
                {
                    regQuo._bits[num9] = (uint)num14;
                }
                else
                {
                    regQuo._uSmall = (uint)num14;
                }
            }

            regNum._iuLast = num1 - 1;
            regNum.Trim();
        }