当前位置: 首页>>代码示例>>C#>>正文


C# IBigInteger.Abs方法代码示例

本文整理汇总了C#中IBigInteger.Abs方法的典型用法代码示例。如果您正苦于以下问题:C# IBigInteger.Abs方法的具体用法?C# IBigInteger.Abs怎么用?C# IBigInteger.Abs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IBigInteger的用法示例。


在下文中一共展示了IBigInteger.Abs方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Remainder

        public IBigInteger Remainder(
            IBigInteger n)
        {
            if (n.SignValue == 0)
                throw new ArithmeticException("Division by zero error");

            if (this.SignValue == 0)
                return Zero;

            // For small values, use fast remainder method
            if (n.Magnitude.Length == 1)
            {
                int val = n.Magnitude[0];

                if (val > 0)
                {
                    if (val == 1)
                        return Zero;

                    // TODO Make this func work on uint, and handle val == 1?
                    int rem = Remainder(val);

                    return rem == 0
                               ? Zero
                               : new BigInteger(SignValue, new int[] {rem}, false);
                }
            }

            if (CompareNoLeadingZeroes(0, Magnitude, 0, n.Magnitude) < 0)
                return this;

            int[] result;
            if (n.QuickPow2Check()) // n is power of two
            {
                // TODO Move before small values branch above?
                result = LastNBits(n.Abs().BitLength - 1);
            }
            else
            {
                result = (int[]) this.Magnitude.Clone();
                result = Remainder(result, n.Magnitude);
            }

            return new BigInteger(SignValue, result, true);
        }
开发者ID:sanyaade-iot,项目名称:Schmoose-BouncyCastle,代码行数:45,代码来源:BigInteger.cs

示例2: Multiply

        public IBigInteger Multiply(
            IBigInteger val)
        {
            if (SignValue == 0 || val.SignValue == 0)
                return Zero;

            if (val.QuickPow2Check()) // val is power of two
            {
                IBigInteger result = this.ShiftLeft(val.Abs().BitLength - 1);
                return val.SignValue > 0 ? result : result.Negate();
            }

            if (this.QuickPow2Check()) // this is power of two
            {
                IBigInteger result = val.ShiftLeft(this.Abs().BitLength - 1);
                return this.SignValue > 0 ? result : result.Negate();
            }

            int resLength = (this.BitLength + val.BitLength)/BitsPerInt + 1;
            var res = new int[resLength];

            if (val == this)
            {
                Square(res, this.Magnitude);
            }
            else
            {
                Multiply(res, this.Magnitude, val.Magnitude);
            }

            return new BigInteger(SignValue*val.SignValue, res, true);
        }
开发者ID:sanyaade-iot,项目名称:Schmoose-BouncyCastle,代码行数:32,代码来源:BigInteger.cs

示例3: DivideAndRemainder

        public IBigInteger[] DivideAndRemainder(
            IBigInteger val)
        {
            if (val.SignValue == 0)
                throw new ArithmeticException("Division by zero error");

            var biggies = new IBigInteger[2];

            if (SignValue == 0)
            {
                biggies[0] = Zero;
                biggies[1] = Zero;
            }
            else if (val.QuickPow2Check()) // val is power of two
            {
                int e = val.Abs().BitLength - 1;
                IBigInteger quotient = this.Abs().ShiftRight(e);
                int[] remainder = this.LastNBits(e);

                biggies[0] = val.SignValue == this.SignValue ? quotient : quotient.Negate();
                biggies[1] = new BigInteger(this.SignValue, remainder, true);
            }
            else
            {
                var remainder = (int[]) this.Magnitude.Clone();
                int[] quotient = Divide(remainder, val.Magnitude);

                biggies[0] = new BigInteger(this.SignValue*val.SignValue, quotient, true);
                biggies[1] = new BigInteger(this.SignValue, remainder, true);
            }

            return biggies;
        }
开发者ID:sanyaade-iot,项目名称:Schmoose-BouncyCastle,代码行数:33,代码来源:BigInteger.cs

示例4: Gcd

        public IBigInteger Gcd(
            IBigInteger value)
        {
            if (value.SignValue == 0)
                return Abs();

            if (SignValue == 0)
                return value.Abs();

            IBigInteger r;
            IBigInteger u = this;
            IBigInteger v = value;

            while (v.SignValue != 0)
            {
                r = u.Mod(v);
                u = v;
                v = r;
            }

            return u;
        }
开发者ID:sanyaade-iot,项目名称:Schmoose-BouncyCastle,代码行数:22,代码来源:BigInteger.cs

示例5: Divide

        public IBigInteger Divide(
            IBigInteger val)
        {
            if (val.SignValue == 0)
                throw new ArithmeticException("Division by zero error");

            if (SignValue == 0)
                return Zero;

            if (val.QuickPow2Check()) // val is power of two
            {
                IBigInteger result = this.Abs().ShiftRight(val.Abs().BitLength - 1);
                return val.SignValue == this.SignValue ? result : result.Negate();
            }

            var mag = (int[]) this.Magnitude.Clone();

            return new BigInteger(this.SignValue*val.SignValue, Divide(mag, val.Magnitude), true);
        }
开发者ID:sanyaade-iot,项目名称:Schmoose-BouncyCastle,代码行数:19,代码来源:BigInteger.cs


注:本文中的IBigInteger.Abs方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。