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


C# BigInteger.Abs方法代码示例

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


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

示例1: TestAbs

        public void TestAbs()
        {
            var value = new BigInteger(-50);
            var absolute = value.Abs();

            Assert.AreEqual(new BigInteger(50), absolute);
        }
开发者ID:chosenmangos,项目名称:Encore,代码行数:7,代码来源:BigIntegerTest.cs

示例2: Multiply

        public virtual ECPoint Multiply(ECPoint p, BigInteger k)
        {
            int sign = k.SignValue;
            if (sign == 0 || p.IsInfinity)
                return p.Curve.Infinity;

            ECPoint positive = MultiplyPositive(p, k.Abs());
            return sign > 0 ? positive : positive.Negate();
        }
开发者ID:MBrekhof,项目名称:pleiobox-clients,代码行数:9,代码来源:AbstractECMultiplier.cs

示例3: BigInteger2Double

 /** @see BigInteger#ToDouble() */
 public static double BigInteger2Double(BigInteger val)
 {
     // val.bitLength() < 64
     if ((val.numberLength < 2)
             || ((val.numberLength == 2) && (val.Digits[1] > 0))) {
         return val.ToInt64();
     }
     // val.bitLength() >= 33 * 32 > 1024
     if (val.numberLength > 32) {
         return ((val.Sign > 0) ? Double.PositiveInfinity
                 : Double.NegativeInfinity);
     }
     int bitLen = val.Abs().BitLength;
     long exponent = bitLen - 1;
     int delta = bitLen - 54;
     // We need 54 top bits from this, the 53th bit is always 1 in lVal.
     long lVal = val.Abs().ShiftRight(delta).ToInt64();
     /*
      * Take 53 bits from lVal to mantissa. The least significant bit is
      * needed for rounding.
      */
     long mantissa = lVal & 0x1FFFFFFFFFFFFFL;
     if (exponent == 1023) {
         if (mantissa == 0X1FFFFFFFFFFFFFL) {
             return ((val.Sign > 0) ? Double.PositiveInfinity
                     : Double.NegativeInfinity);
         }
         if (mantissa == 0x1FFFFFFFFFFFFEL) {
             return ((val.Sign > 0) ? Double.MaxValue : -Double.MaxValue);
         }
     }
     // Round the mantissa
     if (((mantissa & 1) == 1)
             && (((mantissa & 2) == 2) || BitLevel.NonZeroDroppedBits(delta,
                     val.Digits))) {
         mantissa += 2;
     }
     mantissa >>= 1; // drop the rounding bit
     // long resSign = (val.sign < 0) ? 0x8000000000000000L : 0;
     long resSign = (val.Sign < 0) ? Int64.MinValue : 0;
     exponent = ((1023 + exponent) << 52) & 0x7FF0000000000000L;
     long result = resSign | exponent | mantissa;
     return BitConverter.Int64BitsToDouble(result);
 }
开发者ID:tupunco,项目名称:deveel-math,代码行数:45,代码来源:Conversion.cs

示例4: CalculateB

 protected virtual BigInteger CalculateB(BigInteger k, BigInteger g, int t)
 {
     bool negative = (g.SignValue < 0);
     BigInteger b = k.Multiply(g.Abs());
     bool extra = b.TestBit(t - 1);
     b = b.ShiftRight(t);
     if (extra)
     {
         b = b.Add(BigInteger.One);
     }
     return negative ? b.Negate() : b;
 }
开发者ID:ALange,项目名称:OutlookPrivacyPlugin,代码行数:12,代码来源:GlvTypeBEndomorphism.cs

示例5: Multiply

        public virtual ECPoint Multiply(ECPoint p, BigInteger k)
        {
            int sign = k.SignValue;
            if (sign == 0 || p.IsInfinity)
                return p.Curve.Infinity;

            ECPoint positive = MultiplyPositive(p, k.Abs());
            ECPoint result = sign > 0 ? positive : positive.Negate();

            /*
             * Although the various multipliers ought not to produce invalid output under normal
             * circumstances, a final check here is advised to guard against fault attacks.
             */
            return ECAlgorithms.ValidatePoint(result);
        }
开发者ID:ubberkid,项目名称:PeerATT,代码行数:15,代码来源:AbstractECMultiplier.cs

示例6: ReferenceMultiply

 /**
  * Simple shift-and-add multiplication. Serves as reference implementation
  * to verify (possibly faster) implementations, and for very small scalars.
  *
  * @param p
  *            The point to multiply.
  * @param k
  *            The multiplier.
  * @return The result of the point multiplication <code>kP</code>.
  */
 public static ECPoint ReferenceMultiply(ECPoint p, BigInteger k)
 {
     BigInteger x = k.Abs();
     ECPoint q = p.Curve.Infinity;
     int t = x.BitLength;
     if (t > 0)
     {
         if (x.TestBit(0))
         {
             q = p;
         }
         for (int i = 1; i < t; i++)
         {
             p = p.Twice();
             if (x.TestBit(i))
             {
                 q = q.Add(p);
             }
         }
     }
     return k.SignValue < 0 ? q.Negate() : q;
 }
开发者ID:bitcoinkit,项目名称:BitcoinKit-CSharp,代码行数:32,代码来源:ECAlgorithms.cs

示例7: ModReduce

 protected virtual BigInteger ModReduce(BigInteger x)
 {
     if (r == null)
     {
         x = x.Mod(q);
     }
     else
     {
         bool negative = x.SignValue < 0;
         if (negative)
         {
             x = x.Abs();
         }
         int qLen = q.BitLength;
         if (r.SignValue > 0)
         {
             BigInteger qMod = BigInteger.One.ShiftLeft(qLen);
             bool rIsOne = r.Equals(BigInteger.One);
             while (x.BitLength > (qLen + 1))
             {
                 BigInteger u = x.ShiftRight(qLen);
                 BigInteger v = x.Remainder(qMod);
                 if (!rIsOne)
                 {
                     u = u.Multiply(r);
                 }
                 x = u.Add(v);
             }
         }
         else
         {
             int d = ((qLen - 1) & 31) + 1;
             BigInteger mu = r.Negate();
             BigInteger u = mu.Multiply(x.ShiftRight(qLen - d));
             BigInteger quot = u.ShiftRight(qLen + d);
             BigInteger v = quot.Multiply(q);
             BigInteger bk1 = BigInteger.One.ShiftLeft(qLen + d);
             v = v.Remainder(bk1);
             x = x.Remainder(bk1);
             x = x.Subtract(v);
             if (x.SignValue < 0)
             {
                 x = x.Add(bk1);
             }
         }
         while (x.CompareTo(q) >= 0)
         {
             x = x.Subtract(q);
         }
         if (negative && x.SignValue != 0)
         {
             x = q.Subtract(x);
         }
     }
     return x;
 }
开发者ID:ubberkid,项目名称:PeerATT,代码行数:56,代码来源:ECFieldElement.cs

示例8: BIDivide

        public static object BIDivide(BigInteger n, BigInteger d)
        {
            if (d.Equals(BigInteger.ZERO))
                throw new ArithmeticException("Divide by zero");
            //BigInteger gcd = n.gcd(d);
            BigInteger gcd = n.Gcd(d);
            if (gcd.Equals(BigInteger.ZERO))
                return 0;
            //n = n.divide(gcd);
            //d = d.divide(gcd);
            n = n / gcd;
            d = d / gcd;

            if (d.Equals(BigInteger.ONE))
                return reduce(n);
            //return new Ratio((d.signum() < 0 ? n.negate() : n),
            //    (d.signum() < 0 ? d.negate() : d));
            return new Ratio((d.Signum < 0 ? -n : n), d.Abs());
        }
开发者ID:jlomax,项目名称:clojure-clr,代码行数:19,代码来源:NumbersV0.cs

示例9: PrintInteger

            void PrintInteger(StringBuilder sb, BigInteger val)
            {
                StringBuilder sb1 = new StringBuilder();
                bool neg = val.IsNegative;
                BigInteger v = val.Abs();

                PrintLeadingSign(sb1,neg);

                if ( _conversion ==  ConversionAux.DecimalInteger )
                        PrintMagnitude(sb1,neg,v.ToString());
                else
                {
                    string s = v.ToString( _conversion == ConversionAux.OctalInteger ? 8u : 16u );
                    PrintIntOctHex(sb1,s,neg,true);
                }

                PrintTrailingSign(sb1,neg);
                PrintWithJustification(sb,sb1.ToString());
            }
开发者ID:jlomax,项目名称:clojure-clr,代码行数:19,代码来源:Printf.cs

示例10: Abs_zero_is_zero

 public void Abs_zero_is_zero()
 {
     BigInteger z = new BigInteger(0);
     Expect(z.Abs().IsZero);
 }
开发者ID:richhickey,项目名称:clojure-clr,代码行数:5,代码来源:BigIntegerTests.cs

示例11: ToBinary

 internal static string ToBinary(BigInteger val) {            
     string res = ToBinary(val.Abs(), true, true);
     if (val.IsNegative()) {
         res = "-" + res;
     }
     return res;
 }
开发者ID:CookieEaters,项目名称:FireHTTP,代码行数:7,代码来源:LongOps.cs

示例12: ImplShamirsTrickWNaf

        internal static ECPoint ImplShamirsTrickWNaf(ECPoint P, BigInteger k, ECPointMap pointMapQ, BigInteger l)
        {
            bool negK = k.SignValue < 0, negL = l.SignValue < 0;

            k = k.Abs();
            l = l.Abs();

            int width = System.Math.Max(2, System.Math.Min(16, WNafUtilities.GetWindowSize(System.Math.Max(k.BitLength, l.BitLength))));

            ECPoint Q = WNafUtilities.MapPointWithPrecomp(P, width, true, pointMapQ);
            WNafPreCompInfo infoP = WNafUtilities.GetWNafPreCompInfo(P);
            WNafPreCompInfo infoQ = WNafUtilities.GetWNafPreCompInfo(Q);

            ECPoint[] preCompP = negK ? infoP.PreCompNeg : infoP.PreComp;
            ECPoint[] preCompQ = negL ? infoQ.PreCompNeg : infoQ.PreComp;
            ECPoint[] preCompNegP = negK ? infoP.PreComp : infoP.PreCompNeg;
            ECPoint[] preCompNegQ = negL ? infoQ.PreComp : infoQ.PreCompNeg;

            byte[] wnafP = WNafUtilities.GenerateWindowNaf(width, k);
            byte[] wnafQ = WNafUtilities.GenerateWindowNaf(width, l);

            return ImplShamirsTrickWNaf(preCompP, preCompNegP, wnafP, preCompQ, preCompNegQ, wnafQ);
        }
开发者ID:MBrekhof,项目名称:pleiobox-clients,代码行数:23,代码来源:ECAlgorithms.cs

示例13: TestDiv

        private void TestDiv(BigInteger i1, BigInteger i2)
        {
            BigInteger q = i1.Divide(i2);
            BigInteger r = i1.Remainder(i2);
            BigInteger remainder;
            BigInteger quotient = i1.DivideAndRemainder(i2, out remainder);

            Assert.IsTrue(q.Equals(quotient), "Divide and DivideAndRemainder do not agree");
            Assert.IsTrue(r.Equals(remainder), "Remainder and DivideAndRemainder do not agree");
            Assert.IsTrue(q.Sign != 0 || q.Equals(zero), "signum and equals(zero) do not agree on quotient");
            Assert.IsTrue(r.Sign != 0 || r.Equals(zero), "signum and equals(zero) do not agree on remainder");
            Assert.IsTrue(q.Sign == 0 || q.Sign == i1.Sign * i2.Sign, "wrong sign on quotient");
            Assert.IsTrue(r.Sign == 0 || r.Sign == i1.Sign, "wrong sign on remainder");
            Assert.IsTrue(r.Abs().CompareTo(i2.Abs()) < 0, "remainder out of range");
            Assert.IsTrue(q.Abs().Add(one).Multiply(i2.Abs()).CompareTo(i1.Abs()) > 0, "quotient too small");
            Assert.IsTrue(q.Abs().Multiply(i2.Abs()).CompareTo(i1.Abs()) <= 0, "quotient too large");
            BigInteger p = q.Multiply(i2);
            BigInteger a = p.Add(r);
            Assert.IsTrue(a.Equals(i1), "(a/b)*b+(a%b) != a");
            try {
                BigInteger mod = i1.Mod(i2);
                Assert.IsTrue(mod.Sign >= 0, "mod is negative");
                Assert.IsTrue(mod.Abs().CompareTo(i2.Abs()) < 0, "mod out of range");
                Assert.IsTrue(r.Sign < 0 || r.Equals(mod), "positive remainder == mod");
                Assert.IsTrue(r.Sign >= 0 || r.Equals(mod.Subtract(i2)), "negative remainder == mod - divisor");
            } catch (ArithmeticException e) {
                Assert.IsTrue(i2.Sign <= 0, "mod fails on negative divisor only");
            }
        }
开发者ID:deveel,项目名称:deveel-math,代码行数:29,代码来源:BigIntegerTest.cs

示例14: BitLength

        // Like GetBitCount(Abs(x)), except 0 maps to 0
        public static int BitLength(BigInteger x)
        {
            if (x.IsZero())
            {
                return 0;
            }

            return x.Abs().GetBitCount();
        }
开发者ID:mlnlover11,项目名称:IExtendFramework,代码行数:10,代码来源:DLRMath.cs

示例15: IntToOctets

        /// <summary>
        /// Convert a BigInteger to bytes
        /// </summary>
        /// 
        /// <param name="X">The BigInteger</param>
        /// 
        /// <returns>Returns the BigInteger as a byte array</returns>
        public static byte[] IntToOctets(BigInteger X)
        {
            byte[] valBytes = X.Abs().ToByteArray();

            // check whether the array includes a sign bit
            if ((X.BitLength & 7) != 0)
                return valBytes;

            // get rid of the sign bit (first byte)
            byte[] tmp = new byte[X.BitLength >> 3];
            Array.Copy(valBytes, 1, tmp, 0, tmp.Length);

            return tmp;
        }
开发者ID:Steppenwolfe65,项目名称:Rainbow-NET,代码行数:21,代码来源:BigMath.cs


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