Exemple #1
0
        public void Sub(BigIntWithUnit other)
        {
            if (this <= other)
            {
                _intArray = new List <ushort> {
                    0
                };
                return;
            }

            for (var i = 0; i < _intArray.Count; i++)
            {
                if (_intArray[i] < other.SafeGetPart(i))
                {
                    var j = 1;
                    while (_intArray[i + j] == 0)
                    {
                        _intArray[i + j] += 999;
                        j++;
                    }
                    _intArray[i + j] -= 1;
                    _intArray[i]     += 1000;
                }
                _intArray[i] -= other.SafeGetPart(i);
            }
        }
Exemple #2
0
        public void Add(BigIntWithUnit other)
        {
            // +1 for overflow
            if (other == null)
            {
                return;
            }
            Pad((other.Length() / 3) + 1);
            ushort overflow = 0;

            for (var i = 0; i < _intArray.Count; i++)
            {
                _intArray[i] += (ushort)(other.SafeGetPart(i) + overflow);
                overflow      = (ushort)(_intArray[i] / 1000);
                _intArray[i]  = (ushort)(_intArray[i] % 1000);
            }
            Trim();
        }
Exemple #3
0
        /// <summary>
        /// Divides this to elem2 with presision of two digits after comma
        /// </summary>
        public BigIntWithUnit Divide(BigIntWithUnit elem2)
        {
            if (elem2 == 0 || this == 0)
            {
                return(0);
            }
            Trim();
            elem2.Trim();
            if (_intArray.Count - elem2._intArray.Count > 1)
            {
                BigIntWithUnit recursionTemp = (BigIntWithUnit)Clone();
                recursionTemp.ShiftRight(3);
                BigIntWithUnit recursionResult = recursionTemp.Divide(elem2);
                recursionResult.ShiftLeft(3);
                return(recursionResult);
            }
            if (elem2._intArray.Count - _intArray.Count > 1)
            {
                return(0);
            }

            //Actual division by substraction
            //Only need the first two parts because of accuracy
            BigIntWithUnit tempElem1 = 0;
            BigIntWithUnit tempElem2 = 0;

            if (_intArray.Count == elem2._intArray.Count)
            {
                tempElem1.SafeSetPart(0, SafeGetPart(_intArray.Count - 2));
                tempElem1.SafeSetPart(1, SafeGetPart(_intArray.Count - 1));
                tempElem2.SafeSetPart(0, elem2.SafeGetPart(elem2._intArray.Count - 2));
                tempElem2.SafeSetPart(1, elem2.SafeGetPart(elem2._intArray.Count - 1));
            }
            else if (elem2._intArray.Count > _intArray.Count)
            {
                tempElem1.SafeSetPart(0, SafeGetPart(_intArray.Count - 1));
                tempElem2.SafeSetPart(0, elem2.SafeGetPart(elem2._intArray.Count - 2));
                tempElem2.SafeSetPart(1, elem2.SafeGetPart(elem2._intArray.Count - 1));
            }
            else
            {
                tempElem1.SafeSetPart(0, SafeGetPart(_intArray.Count - 2));
                tempElem1.SafeSetPart(1, SafeGetPart(_intArray.Count - 1));
                tempElem2.SafeSetPart(0, elem2.SafeGetPart(elem2._intArray.Count - 1));
            }

            BigIntWithUnit result = 0;
            int            j      = 0;

            if (tempElem2 == 0)
            {
                return(0);
            }
            while (tempElem2 < 100)
            {
                tempElem2.ShiftLeft(1);
                j++;
            }
            for (var i = j; i > -3; i--)
            {
                while (tempElem1 >= tempElem2 && tempElem2 != 0)
                {
                    tempElem1.Sub(tempElem2);
                    result += Math.Pow(10, i);
                }
                var toAdd = (tempElem2.SafeGetPart(1) * 100) % 1000;
                tempElem2.SafeSetPart(1, (ushort)(tempElem2.SafeGetPart(1) / 10));
                tempElem2.SafeSetPart(0, (ushort)(tempElem2.SafeGetPart(0) / 10 + toAdd));
            }

            return(result);
        }