Esempio n. 1
0
        private static Expression BigIntConstant(BigInt value)
        {
            int ival;

            if (value.AsInt32(out ival))
            {
                return(Expression.Call(
                           new Func <int, BigInt>(CompilerHelpers.CreateBigInt).GetMethodInfo(),
                           Constant(ival)
                           ));
            }

            long lval;

            if (value.AsInt64(out lval))
            {
                return(Expression.Call(
                           new Func <long, BigInt>(CompilerHelpers.CreateBigInt).GetMethodInfo(),
                           Constant(lval)
                           ));
            }

            return(Expression.Call(
                       new Func <bool, byte[], BigInt>(CompilerHelpers.CreateBigInt).GetMethodInfo(),
                       Constant(value.Sign < 0),
                       CreateArray <byte>(value.Abs().ToByteArray())
                       ));
        }
Esempio n. 2
0
        /// <summary>
        /// Returns the least common multiple (<c>lcm</c>) of two big integers.
        /// </summary>
        /// <param name="a">First Integer: a.</param>
        /// <param name="b">Second Integer: b.</param>
        /// <returns>Least common multiple <c>lcm</c>(a,b)</returns>
        public static BigInteger LeastCommonMultiple(BigInteger a, BigInteger b)
        {
            if (a.IsZero || b.IsZero)
            {
                return(BigInteger.Zero);
            }

            return(BigInteger.Abs((a / BigInteger.GreatestCommonDivisor(a, b)) * b));
        }
Esempio n. 3
0
        public static int BitLength(BigInt x)
        {
            if (x.IsZero)
            {
                return(0);
            }

            byte[] bytes = BigInt.Abs(x).ToByteArray();
            int    index = bytes.Length;

            while (bytes[--index] == 0)
            {
                ;
            }

            return(index * 8 + BitLength((int)bytes[index]));
        }
Esempio n. 4
0
        /// <summary>
        /// Returns the least common multiple (<c>lcm</c>) of a set of big integers.
        /// </summary>
        /// <param name="integers">List of Integers.</param>
        /// <returns>Least common multiple <c>lcm</c>(list of integers)</returns>
        public static BigInteger LeastCommonMultiple(IList <BigInteger> integers)
        {
            if (null == integers)
            {
                throw new ArgumentNullException(nameof(integers));
            }

            if (integers.Count == 0)
            {
                return(1);
            }

            var lcm = BigInteger.Abs(integers[0]);

            for (int i = 1; i < integers.Count; i++)
            {
                lcm = LeastCommonMultiple(lcm, integers[i]);
            }

            return(lcm);
        }
Esempio n. 5
0
        /// <summary>
        /// Returns the greatest common divisor (<c>gcd</c>) of a set of big integers.
        /// </summary>
        /// <param name="integers">List of Integers.</param>
        /// <returns>Greatest common divisor <c>gcd</c>(list of integers)</returns>
        public static BigInteger GreatestCommonDivisor(IList <BigInteger> integers)
        {
            if (null == integers)
            {
                throw new ArgumentNullException(nameof(integers));
            }

            if (integers.Count == 0)
            {
                return(0);
            }

            var gcd = BigInteger.Abs(integers[0]);

            for (int i = 1; (i < integers.Count) && (gcd > BigInteger.One); i++)
            {
                gcd = GreatestCommonDivisor(gcd, integers[i]);
            }

            return(gcd);
        }
Esempio n. 6
0
        private static byte GetHighestByte(BigInt self, out int index, out byte[] byteArray)
        {
            byte[] bytes = BigInt.Abs(self).ToByteArray();
            if (self.IsZero)
            {
                byteArray = bytes;
                index     = 0;
                return(1);
            }

            int  hi = bytes.Length;
            byte b;

            do
            {
                b = bytes[--hi];
            } while (b == 0);
            index     = hi;
            byteArray = bytes;
            return(b);
        }
Esempio n. 7
0
        public static int GetBitCount(this BigInt self)
        {
            if (self.IsZero)
            {
                return(1);
            }
            byte[] bytes = BigInt.Abs(self).ToByteArray();

            int index = bytes.Length;

            while (bytes[--index] == 0)
            {
                ;
            }

            int count = index * 8;

            for (int hiByte = bytes[index]; hiByte > 0; hiByte >>= 1)
            {
                count++;
            }
            return(count);
        }
Esempio n. 8
0
 public BigInteger Abs()
 {
     return(new BigInteger(BigInt.Abs(Value)));
 }
Esempio n. 9
0
 public static BigInt Abs(this BigInt self)
 {
     return(BigInt.Abs(self));
 }
Esempio n. 10
0
 public BigInt abs()
 {
     return(FSBigInt.Abs(_num));
 }
Esempio n. 11
0
        public static BigFloat operator +(BigFloat x, BigFloat y)
        {
            Contract.Requires(x.exponentSize == y.exponentSize);
            Contract.Requires(x.significandSize == y.significandSize);

            if (x.value != "" || y.value != "")
            {
                if (x.value == "NaN" || y.value == "NaN" || x.value == "+oo" && y.value == "-oo" ||
                    x.value == "-oo" && y.value == "+oo")
                {
                    return(new BigFloat("NaN", x.significandSize, x.exponentSize));
                }

                if (x.value != "")
                {
                    return(new BigFloat(x.value, x.significandSize, x.exponentSize));
                }

                return(new BigFloat(y.value, y.significandSize, y.exponentSize));
            }

            if (x.exponent > y.exponent)
            {
                BigFloat temp = x;
                x = y;
                y = temp;
            }

            BIM xsig = x.significand, ysig = y.significand;
            BIM xexp = x.exponent, yexp = y.exponent;

            if (yexp - xexp > x.significandSize) //One of the numbers is relatively insignificant
            {
                return(new BigFloat(y.isSignBitSet, y.significand, y.exponent, y.significandSize, y.exponentSize));
            }

            BIM hiddenBitPow = BIM.Pow(2, x.significandSize - 1);

            if (xexp > 0)
            {
                xsig += hiddenBitPow;
            }
            else
            {
                ++xexp;
            }

            if (yexp > 0)
            {
                ysig += hiddenBitPow;
            }
            else
            {
                ++yexp;
            }

            if (x.isSignBitSet)
            {
                xsig = -xsig;
            }

            if (y.isSignBitSet)
            {
                ysig = -ysig;
            }

            xsig >>= (int)(yexp - xexp); //Guaranteed to fit in a 32-bit integer

            ysig += xsig;

            bool isNeg = ysig < 0;

            ysig = BIM.Abs(ysig);

            if (ysig == 0)
            {
                return(new BigFloat(x.isSignBitSet && y.isSignBitSet, 0, 0, x.significandSize, x.exponentSize));
            }

            if (ysig >= hiddenBitPow * 2)
            {
                ysig >>= 1;
                ++yexp;
            }

            while (ysig < hiddenBitPow && yexp > 1)
            {
                ysig <<= 1;
                --yexp;
            }

            if (ysig < hiddenBitPow)
            {
                yexp = 0;
            }
            else
            {
                ysig -= hiddenBitPow;
            }

            if (yexp >= BIM.Pow(2, x.exponentSize) - 1)
            {
                return(new BigFloat(y.isSignBitSet ? "-oo" : "+oo", x.significandSize, x.exponentSize));
            }

            return(new BigFloat(isNeg, ysig, yexp, x.significandSize, x.exponentSize));
        }