Esempio n. 1
0
        public int Compare(BigNumber num, bool skipOrientation = false)
        {
            if (!skipOrientation)
            {
                if (this.IsPositive() && num.IsNegative())
                {
                    return(1);
                }

                if (this.IsNegative() && num.IsPositive())
                {
                    return(-1);
                }
            }

            int alen = this.Length();
            int blen = num.Length();

            int len = alen > blen ? alen : blen;

            for (int i = len - 1; i >= 0; i--)
            {
                if (this.get(i) > num.get(i))
                {
                    if (skipOrientation || this.IsPositive())
                    {
                        return(1);
                    }
                    else
                    {
                        return(-1);
                    }
                }

                if (this.get(i) < num.get(i))
                {
                    if (skipOrientation || this.IsPositive())
                    {
                        return(-1);
                    }
                    else
                    {
                        return(1);
                    }
                }
            }

            return(0);
        }
Esempio n. 2
0
        public BigNumber Subtraction(BigNumber num)
        {
            bool resultOrientation = true;
            bool switchOrder       = false;
            bool subtraction       = true;

            if (this.Compare(num, true) >= 0)
            {
                if (this.IsPositive() && num.IsPositive())
                {
                    subtraction       = true;
                    resultOrientation = POSITIVE;
                }

                if (this.IsPositive() && num.IsNegative())
                {
                    subtraction       = false;
                    resultOrientation = POSITIVE;
                }

                if (this.IsNegative() && num.IsPositive())
                {
                    subtraction       = false;
                    resultOrientation = NEGATIVE;
                }

                if (this.IsNegative() && num.IsNegative())
                {
                    subtraction       = true;
                    resultOrientation = NEGATIVE;
                }
            }
            else
            {
                switchOrder = true;

                if (this.IsPositive() && num.IsPositive())
                {
                    subtraction       = true;
                    resultOrientation = NEGATIVE;
                }

                if (this.IsPositive() && num.IsNegative())
                {
                    subtraction       = false;
                    resultOrientation = POSITIVE;
                }

                if (this.IsNegative() && num.IsPositive())
                {
                    subtraction       = false;
                    resultOrientation = NEGATIVE;
                }

                if (this.IsNegative() && num.IsNegative())
                {
                    subtraction       = true;
                    resultOrientation = POSITIVE;
                }
            }


            int alen = this.Length();
            int blen = num.Length();

            int len = alen > blen ? alen : blen;

            int t = 0;

            List <byte> output = new List <byte>();

            for (int i = 0; i < len; i++)
            {
                int c1 = this.get(i);
                int c2 = num.get(i);

                int n = 0;

                if (subtraction)
                {
                    if (switchOrder)
                    {
                        n = c2 - (c1 + t);
                    }
                    else
                    {
                        n = c1 - (c2 + t);
                    }

                    t = 0;
                    if (n < 0)
                    {
                        n = 10 + n;
                        t = 1;
                    }
                    output.Add((byte)(n));
                }
                else
                {
                    n = c1 + c2 + t;

                    t = 0;
                    if (n > 9)
                    {
                        t = n / 10;
                    }

                    output.Add((byte)(n % 10));
                }
            }

            return(new BigNumber(resultOrientation, output));
        }