protected override IDataItem TestFunc(IDataItem p1)
        {
            var u = p1.ToUInt32().Value;
            var w = UBigInt.From(u);

            return(new UBigIntDataItem(w));
        }
예제 #2
0
        private static UBigInt 完全除算_3(UBigInt u)
        {
            var inverse_3 = 2863311531;
            var q_digits  = new UInt32[0];

            while (u >= 0x100000000)
            {
                // u の桁が 2 ワード以上残っている場合
                var u0 = u & 0xffffffffU;
                var q0 = ((UBigInt)u0 * inverse_3) & 0xffffffffU;
                u        = u.USubtruct((UBigInt)q0 * 3U);
                q_digits = q_digits.Append(q0).ToArray();
                if ((u & 0xffffffff) != 0)
                {
                    throw new ApplicationException();
                }
                u >>= 32;
            }
            if (u % 3 != 0)
            {
                throw new ApplicationException();
            }
            q_digits = q_digits.Append((UInt32)u / 3).ToArray();
            var q = UBigInt.Zero;

            for (var index = q_digits.Length - 1; index >= 0; --index)
            {
                q = (q << 32) | q_digits[index];
            }

            return(q);
        }
예제 #3
0
        protected override IDataItem TestFunc(IDataItem p1, IDataItem p2)
        {
            var u = p1.ToUInt32().Value;
            var v = p2.ToUBigInt().Value;
            var w = UBigInt.Equals(u, v);

            return(new UInt32DataItem(w ? 1U : 0U));
        }
예제 #4
0
        protected override IDataItem TestFunc(IDataItem p1, IDataItem p2)
        {
            var u = p1.ToUInt32().Value;
            var v = p2.ToUInt32().Value;
            var w = UBigInt.Pow(u, v);

            return(new UBigIntDataItem(w));
        }
        protected override IDataItem TestFunc(IDataItem p1, IDataItem p2)
        {
            var u = p1.ToUBigInt().Value;
            var v = p2.ToInt64().Value;
            var w = UBigInt.Compare(u, v);

            return(new Int32DataItem(w));
        }
        protected override IDataItem TestFunc(IDataItem p1, IDataItem p2, IDataItem p3)
        {
            var s = p1.ToXString().Value;
            var f = p2.ToInt32().Value;
            var n = p3.ToNumberFormatInfo().Value;
            var r = UBigInt.Parse(s, (NumberStyles)f, n);

            return(new UBigIntDataItem(r));
        }
예제 #7
0
        public void CalculatePie(uint prsn, out UBigInt u, out UBigInt v)
        {
            var p = UBigInt.Pow(10, prsn);
            var q = UBigInt.Pow(10, 10);

            var y = ((12 * expansion(p, q, 18) + (expansion(p, q, 57) << 3) - 5 * expansion(p, q, 239)) << 2).ToUBigInt() / q;

            u = y.DivRem(p, out v);
        }
예제 #8
0
        // 2^(k-1) <= v < 2^k が成り立つ k を返す
        private static int GetBitCount(UBigInt v)
        {
            var k = 0;

            while ((UBigInt.One << k) <= v)
            {
                ++k;
            }
            return(k);
        }
예제 #9
0
        protected override IDataItem TestFunc(IDataItem p1, IDataItem p2, out IDataItem r2)
        {
            var    u = p1.ToUBigInt().Value;
            var    v = p2.ToUInt64().Value;
            UInt64 r;
            var    q = UBigInt.DivRem(u, v, out r);

            r2 = new UInt64DataItem(r);
            return(new UBigIntDataItem(q));
        }
예제 #10
0
        protected override IDataItem TestFunc(IDataItem p1, IDataItem p2, IDataItem p3, out IDataItem r2)
        {
            var     s = p1.ToXString().Value;
            var     f = p2.ToInt32().Value;
            var     n = p3.ToNumberFormatInfo().Value;
            UBigInt v;
            var     r = UBigInt.TryParse(s, (NumberStyles)(int)f, n, out v);

            r2 = new UBigIntDataItem(v);
            return(new UInt32DataItem(r ? 1U : 0U));
        }
        static void Main(string[] args)
        {
            var u1_array = new byte[] { 0x01, 0xe8, 0x03 };
            var u1       = UBigInt.FromByteArray(u1_array);
            var r1       = u1.FloorLog10(); // 3が正解

            var u2_array = new byte[] { 0x01, 0xff, 0xe3, 0x0b, 0x54, 0x02 };
            var u2       = UBigInt.FromByteArray(u2_array);
            var r2       = u2.FloorLog10(); // 9が正解

            Console.ReadLine();
        }
예제 #12
0
 private static void ニュートンラプソン法による逆数の計算(UBigInt v, int factor_v, out UBigInt xi, out int factor_x)
 {
     factor_x = factor_v + 1;
     xi       = (UBigInt.From(7U) << (factor_x - 1)).USubtruct((v << (factor_x - factor_v + 1)));
     while (true)
     {
         var t         = (UBigInt.One << (factor_v + factor_x + 1)).USubtruct(v * xi);
         var xi_plus_1 = (xi * t) >> (factor_v + factor_x);
         if (xi_plus_1 == xi)
         {
             break;
         }
         xi = xi_plus_1;
     }
 }
예제 #13
0
        static UBigInt ニュートンラプソン法による除算(UBigInt u, UBigInt v)
        {
            var     factor_v = GetBitCount(v);
            int     factor_x;
            UBigInt xi;

            ニュートンラプソン法による逆数の計算(v, factor_v, out xi, out factor_x);
            var q = (u * xi) >> (factor_v + factor_x);
            var r = u.USubtruct(v * q);

            if (r >= v)
            {
                r = r.USubtruct(v);
                ++q;
            }
            return(q);
        }
예제 #14
0
        BigInt expansion(UBigInt p, UBigInt q, uint n)
        {
            var x  = (p * q) / n;
            var s  = (BigInt)x;
            var nn = (UBigInt)(n * n);
            var c  = BigInt.One;
            var k  = BigInt.One;

            while (x > 0)
            {
                x = x / nn;
                k = k + 2;
                c = -c;
                s = s + c * (x / k);
            }
            return(s);
        }
예제 #15
0
        static void Main(string[] args)
        {
            //var v = UBigInt.FromByteArray(new byte[] { 0x01, 0x01, 0x01 });
            //var v = UBigInt.FromByteArray(new byte[] { 0x01, 0x04, 0xd2, 0x02, 0x96, 0x49 });
            //var v = UBigInt.FromByteArray(new byte[] { 0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x01 });
            //var v = UBigInt.FromByteArray(new byte[] { 0x01, 0x08, 0xd2, 0x0a, 0x1f, 0xeb, 0x8c, 0xa9, 0x54, 0xab });
            //var u = UBigInt.FromByteArray(new byte[] { 0x01, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 });
            //var v = UBigInt.FromByteArray(new byte[] { 0x01, 0x09, 0xff, 0xff, 0x0f, 0x63, 0x2d, 0x5e, 0xc7, 0x6b, 0x05 });
            //var v = UBigInt.FromByteArray(new byte[] { 0x01, 0x09, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 });
            //var u = BigInt.FromByteArray(new byte[] { 0x03, 0x05, 0x00, 0x00, 0x00, 0x00, 0x01 });
            //var v = 1UL;
            //var u = 1234567890U;
            //var u = 4294967295U;
            //var u = 4294967296UL;
            //var u = 12345678901234567890UL;

            var u = UBigInt.FromByteArray(new byte[] { 0x01, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 });
            var v = UBigInt.FromByteArray(new byte[] { 0x01, 0x11, 0xd2, 0x0a, 0x3f, 0xce, 0x96, 0x5f, 0xbc, 0xac, 0xb8, 0xf3, 0xdb, 0xc0, 0x75, 0x20, 0xc9, 0xa0, 0x03 });
            var w = u.Multiply(MultiplicationMethod.ToomCook, v);

            Console.ReadLine();
        }
        public static IDataItem FromXElement(XElement element)
        {
            if (element == null)
            {
                return(new NullDataItem());
            }
            var type = element.Attribute("type").Value;
            var text = element.Value;

            switch (type)
            {
            case "int32":
                return(new Int32DataItem(Int32.Parse(text)));

            case "uint32":
                return(new UInt32DataItem(UInt32.Parse(text)));

            case "uint64":
                return(new UInt64DataItem(UInt64.Parse(text)));

            case "ubigint":
            {
                var bytes = text.Split(',').Select(x => byte.Parse(x, NumberStyles.HexNumber)).ToArray();
                var p     = UBigInt.FromByteArray(bytes);
                return(new UBigIntDataItem(p));
            }

            case "string":
                return(new XStringDataItem(text));

            case "numberformatinfo":
            {
                var dic = text.Split(':')
                          .Where(line => line != "")
                          .Select(line => line.Split('='))
                          .Select(columns =>
                    {
                        if (columns.Length != 2)
                        {
                            throw new ApplicationException();
                        }
                        var key   = columns[0];
                        var value = columns[1].Replace("&#58;", ":").Replace("&#61;", "=").Replace("&amp;", "&");
                        return(new { key, value });
                    })
                          .ToDictionary(item => item.key, item => item.value);
                var culture = dic.ContainsKey("CultureName") ? CultureInfo.CreateSpecificCulture(dic["CultureName"]) : CultureInfo.InvariantCulture;
                foreach (var key_value in dic)
                {
                    switch (key_value.Key)
                    {
                    case "CurrencyDecimalDigits":
                        culture.NumberFormat.CurrencyDecimalDigits = int.Parse(key_value.Value);
                        break;

                    case "CurrencyDecimalSeparator":
                        culture.NumberFormat.CurrencyDecimalSeparator = key_value.Value;
                        break;

                    case "CurrencyGroupSeparator":
                        culture.NumberFormat.CurrencyGroupSeparator = key_value.Value;
                        break;

                    case "CurrencyGroupSizes":
                        culture.NumberFormat.CurrencyGroupSizes = key_value.Value.Select(c => c - '0').ToArray();
                        break;

                    case "CurrencyNegativePattern":
                        culture.NumberFormat.CurrencyNegativePattern = int.Parse(key_value.Value);
                        break;

                    case "CurrencyPositivePattern":
                        culture.NumberFormat.CurrencyPositivePattern = int.Parse(key_value.Value);
                        break;

                    case "CurrencySymbol":
                        culture.NumberFormat.CurrencySymbol = key_value.Value;
                        break;

                    case "NegativeSign":
                        culture.NumberFormat.NegativeSign = key_value.Value;
                        break;

                    case "NumberDecimalDigits":
                        culture.NumberFormat.NumberDecimalDigits = int.Parse(key_value.Value);
                        break;

                    case "NumberDecimalSeparator":
                        culture.NumberFormat.NumberDecimalSeparator = key_value.Value;
                        break;

                    case "NumberGroupSeparator":
                        culture.NumberFormat.NumberGroupSeparator = key_value.Value;
                        break;

                    case "NumberGroupSizes":
                        culture.NumberFormat.NumberGroupSizes = key_value.Value.Select(c => c - '0').ToArray();
                        break;

                    case "NumberNegativePattern":
                        culture.NumberFormat.NumberNegativePattern = int.Parse(key_value.Value);
                        break;

                    case "PercentDecimalDigits":
                        culture.NumberFormat.PercentDecimalDigits = int.Parse(key_value.Value);
                        break;

                    case "PercentDecimalSeparator":
                        culture.NumberFormat.PercentDecimalSeparator = key_value.Value;
                        break;

                    case "PercentGroupSeparator":
                        culture.NumberFormat.PercentGroupSeparator = key_value.Value;
                        break;

                    case "PercentGroupSizes":
                        culture.NumberFormat.PercentGroupSizes = key_value.Value.Select(c => c - '0').ToArray();
                        break;

                    case "PercentNegativePattern":
                        culture.NumberFormat.PercentNegativePattern = int.Parse(key_value.Value);
                        break;

                    case "PercentPositivePattern":
                        culture.NumberFormat.PercentPositivePattern = int.Parse(key_value.Value);
                        break;

                    case "PercentSymbol":
                        culture.NumberFormat.PercentSymbol = key_value.Value;
                        break;

                    case "PerMilleSymbol":
                        culture.NumberFormat.PerMilleSymbol = key_value.Value;
                        break;

                    case "PositiveSign":
                        culture.NumberFormat.PositiveSign = key_value.Value;
                        break;
                    }
                }
                return(new NumberFormatInfoDataItem(text, culture.NumberFormat));
            }

            case "exception":
                switch (text.ToLower())
                {
                case "overflowexception":
                    return(new ExceptionDataItem(typeof(OverflowException)));

                case "argumentexception":
                    return(new ExceptionDataItem(typeof(ArgumentException)));

                case "argumentnullexception":
                    return(new ExceptionDataItem(typeof(ArgumentNullException)));

                case "formatexception":
                    return(new ExceptionDataItem(typeof(FormatException)));

                case "dividebyzeroexception":
                    return(new ExceptionDataItem(typeof(DivideByZeroException)));

                case "arithmeticexception":
                    return(new ExceptionDataItem(typeof(ArithmeticException)));

                default:
                    throw new ApplicationException();
                }

            default:
                throw new ApplicationException();
            }
        }
 public UBigIntDataItem(UBigInt value)
     : this(string.Join(",", value.ToByteArray().Select(x => x.ToString("x2"))))
 {
 }