Example #1
0
        /// <summary>Method that is calculating (this / op) for BigInteger objects.
        /// Overrided from parent</summary>
        /// <param name="op">Second operand</param>
        /// <returns>BigInteger equal to (this / op)  upcasted to BigNumber</returns>
        public override BigNumber Divide(BigNumber op)
        {
            if (!(op is BigInteger))
            {
                throw new ArgumentException("Cannot Add BigInteger to " + op.GetType());
            }

            if (op.CleanString == "0")
            {
                throw new DivideByZeroException();
            }
            BigInteger bfLeft  = this;
            BigInteger bfRight = (BigInteger)op;

            var leftList  = BigIntegerToIntList(bfLeft, 0);
            var rightList = BigIntegerToIntList(bfRight, 0);

            leftList.RemoveTailingZeros();
            rightList.RemoveTailingZeros();

            List <int> resultList = leftList.DivByList(rightList, NormalizeList, out List <int> subList);
            BigInteger bfAns      = new BigInteger(IntListToString(resultList));

            if (bfLeft.Sign * bfRight.Sign < 0)
            {
                bfAns.Negate();
            }
            return(bfAns);
        }
Example #2
0
        /// <summary>Method that is calculating (this + op) for BigInteger objects.
        /// Overrided from parent</summary>
        /// <param name="op">Second operand</param>
        /// <returns>BigInteger equal to (this + op)  upcasted to BigNumber</returns>
        public override BigNumber Add(BigNumber op)
        {
            if (!(op is BigInteger))
            {
                throw new ArgumentException("Cannot Add BigInteger and " + op.GetType());
            }

            BigInteger bfLeft  = this;
            BigInteger bfRight = (BigInteger)op;

            if (bfLeft.Sign != bfRight.Sign)
            {
                return(bfLeft.Substract(-bfRight));
            }

            int desiredInt = Math.Max(bfLeft.CleanString.Length, bfRight.CleanString.Length);
            var leftList   = BigIntegerToIntList(bfLeft, desiredInt);
            var rightList  = BigIntegerToIntList(bfRight, desiredInt);
            var resultList = leftList.SumWithList(rightList);

            NormalizeList(resultList);

            BigInteger bfAns = new BigInteger(IntListToString(resultList));

            if (Sign < 0)
            {
                bfAns.Negate();
            }
            return(bfAns);
        }
Example #3
0
        /// <summary>Method that is calculating (this * op) for BigInteger objects.
        /// Overrided from parent</summary>
        /// <param name="op">Second operand</param>
        /// <returns>BigInteger equal to (this * op)  upcasted to BigNumber</returns>
        public override BigNumber Multiply(BigNumber op)
        {
            if (!(op is BigInteger))
            {
                throw new ArgumentException("Cannot Add BigInteger and " + op.GetType());
            }

            BigInteger bfLeft  = this;
            BigInteger bfRight = (BigInteger)op;

            if (bfLeft.CleanString.Length < bfRight.CleanString.Length)
            {
                Swap(ref bfLeft, ref bfRight);
            }
            var leftList   = BigIntegerToIntList(bfLeft);
            var rightList  = BigIntegerToIntList(bfRight);
            var resultList = leftList.MulWithList(rightList);

            NormalizeList(resultList);

            BigInteger bfAns = new BigInteger(IntListToString(resultList));

            if (bfLeft.Sign * bfRight.Sign < 0)
            {
                bfAns.Negate();
            }
            return(bfAns);
        }
        /// <summary>Method that is calculating (this / op) for BigDecimal objects.
        /// Overrided from parent</summary>
        /// <param name="op">Second operand</param>
        /// <returns>BigDecimal equal to (this / op)  upcasted to BigNumber</returns>
        public override BigNumber Divide(BigNumber op)
        {
            if (!(op is BigDecimal))
            {
                throw new ArgumentException("Cannot Add BigDecimal and " + op.GetType());
            }

            if (op.CleanString == "0")
            {
                throw new DivideByZeroException();
            }
            BigDecimal bfLeft  = this;
            BigDecimal bfRight = (BigDecimal)op;

            int multiplier = Math.Max(bfLeft.Fractional, bfRight.Fractional);
            var leftList   = BigDecimalToIntList(bfLeft, 0, multiplier + FracPrecision);
            var rightList  = BigDecimalToIntList(bfRight, 0, multiplier);

            leftList.RemoveTailingZeros();
            rightList.RemoveTailingZeros();

            List <int> resultList = leftList.DivByList(rightList, NormalizeList, out List <int> subList);
            int        dotPos     = resultList.Count - FracPrecision;

            BigDecimal bfAns = new BigDecimal(IntListToString(resultList, dotPos));

            if (bfLeft.Sign * bfRight.Sign < 0)
            {
                bfAns.Negate();
            }
            return(bfAns);
        }
        /// <summary>Method that is calculating (this * op) for BigDecimal objects.
        /// Overrided from parent</summary>
        /// <param name="op">Second operand</param>
        /// <returns>BigDecimal equal to (this * op)  upcasted to BigNumber</returns>
        public override BigNumber Multiply(BigNumber op)
        {
            if (!(op is BigDecimal))
            {
                throw new ArgumentException("Cannot Add BigDecimal and " + op.GetType());
            }

            BigDecimal bfLeft  = this;
            BigDecimal bfRight = (BigDecimal)op;

            if (bfLeft.Integer + bfLeft.Fractional < bfRight.Integer + bfRight.Fractional)
            {
                Swap(ref bfLeft, ref bfRight);
            }
            int newDot     = bfLeft.Fractional + bfRight.Fractional;
            var leftList   = BigDecimalToIntList(bfLeft);
            var rightList  = BigDecimalToIntList(bfRight);
            var resultList = leftList.MulWithList(rightList);

            NormalizeList(resultList);

            BigDecimal bfAns = new BigDecimal(IntListToString(resultList, resultList.Count - newDot));

            if (bfLeft.Sign * bfRight.Sign < 0)
            {
                bfAns.Negate();
            }
            return(bfAns);
        }
        /// <summary>Method that is calculating (this + op) for BigDecimal objects.
        /// Overrided from parent</summary>
        /// <param name="op">Second operand</param>
        /// <returns>BigDecimal equal to (this + op)  upcasted to BigNumber</returns>
        public override BigNumber Add(BigNumber op)
        {
            if (!(op is BigDecimal))
            {
                throw new ArgumentException("Cannot Add BigDecimal to " + op.GetType());
            }

            BigDecimal bfLeft  = this;
            BigDecimal bfRight = (BigDecimal)op;

            if (bfLeft.Sign != bfRight.Sign)
            {
                return(bfLeft.Substract(-bfRight));
            }

            int desiredInt  = Math.Max(bfLeft.Integer, bfRight.Integer);
            int desiredFrac = Math.Max(bfLeft.Fractional, bfRight.Fractional);
            var leftList    = BigDecimalToIntList(bfLeft, desiredInt, desiredFrac);
            var rightList   = BigDecimalToIntList(bfRight, desiredInt, desiredFrac);
            var resultList  = leftList.SumWithList(rightList);

            NormalizeList(resultList);

            BigDecimal bfAns = new BigDecimal(IntListToString(resultList, resultList.Count - desiredFrac));

            if (Sign < 0)
            {
                bfAns.Negate();
            }
            return(bfAns);
        }
        /// <summary>Method that is calculating (this - op) for BigDecimal objects.
        /// Overrided from parent</summary>
        /// <param name="op">Second operand</param>
        /// <returns>BigDecimal equal to (this - op)  upcasted to BigNumber</returns>
        public override BigNumber Substract(BigNumber op)
        {
            if (!(op is BigDecimal))
            {
                throw new ArgumentException("Cannot Add BigDecimal and " + op.GetType());
            }

            BigDecimal bfLeft  = this;
            BigDecimal bfRight = (BigDecimal)op;

            if (bfLeft.Sign > 0 && bfRight.Sign < 0)
            {
                return(bfLeft.Add(-bfRight));
            }
            if (bfLeft.Sign < 0 && bfRight.Sign > 0)
            {
                return(-(BigDecimal)bfRight.Add(-bfLeft));
            }
            if (bfLeft.Sign < 0 && bfRight.Sign < 0)
            {
                return((-bfRight).Substract(-bfLeft));
            }
            //both operands are > 0 here
            int sign = 1;

            if (bfLeft < bfRight)
            {
                sign = -sign;
                Swap(ref bfLeft, ref bfRight);
            }
            int desiredInt  = Math.Max(bfLeft.Integer, bfRight.Integer);
            int desiredFrac = Math.Max(bfLeft.Fractional, bfRight.Fractional);
            var leftList    = BigDecimalToIntList(bfLeft, desiredInt, desiredFrac);
            var rightList   = BigDecimalToIntList(bfRight, desiredInt, desiredFrac);
            var resultList  = leftList.SubByList(rightList);

            NormalizeList(resultList);

            BigDecimal bfAns = new BigDecimal(IntListToString(resultList, resultList.Count - desiredFrac));

            if (sign < 0)
            {
                bfAns.Negate();
            }
            return(bfAns);
        }
Example #8
0
        /// <summary>Method that is calculating (this % op) for BigInteger objects.
        /// Overrided from parent</summary>
        /// <param name="op">Second operand</param>
        /// <returns>BigInteger equal to (this % op)  upcasted to BigNumber</returns>
        public override BigNumber Mod(BigNumber op)
        {
            if (!(op is BigInteger))
            {
                throw new ArgumentException("Cannot Add BigInteger and " + op.GetType());
            }

            if (op.CleanString == "0")
            {
                throw new ArgumentException("Cannot calculate BigInteger % 0");
            }
            BigInteger bfLeft  = this;
            BigInteger bfRight = (BigInteger)op;

            BigInteger bfDiv = bfLeft / bfRight;
            BigInteger bfAns = bfLeft - bfDiv * bfRight;

            return(bfAns);
        }
        /// <summary>Method that is calculating (this % op) for BigDecimal objects.
        /// Overrided from parent</summary>
        /// <param name="op">Second operand</param>
        /// <returns>BigDecimal equal to (this % op)  upcasted to BigNumber</returns>
        public override BigNumber Mod(BigNumber op)
        {
            if (!(op is BigDecimal))
            {
                throw new ArgumentException("Cannot Add BigDecimal and " + op.GetType());
            }

            if (op.CleanString == "0")
            {
                throw new ArgumentException("Cannot calculate BigDecimal % 0");
            }
            BigDecimal bfLeft  = this;
            BigDecimal bfRight = (BigDecimal)op;

            int temp = FracPrecision;

            FracPrecision = 0;
            BigDecimal bfDiv = bfLeft / bfRight;

            FracPrecision = temp;
            BigDecimal bfAns = bfLeft - bfDiv * bfRight;

            return(bfAns);
        }