Beispiel #1
0
        static public void Add(BigNumber src, BigNumber dst, BigNumber result)
        {
            int       j, carry, aexp, bexp, adigits, bdigits;
            sbyte     sign;
            BigNumber A = 0, B = 0;

            if (src.signum == 0)
            {
                BigNumber.Copy(dst, result);
                return;
            }

            if (dst.signum == 0)
            {
                BigNumber.Copy(src, result);
                return;
            }

            if (src.signum == 1 && dst.signum == -1)
            {
                dst.signum = 1;
                Sub(src, dst, result);
                dst.signum = -1;
                return;
            }

            if (src.signum == -1 && dst.signum == 1)
            {
                src.signum = 1;
                Sub(dst, src, result);
                src.signum = -1;
                return;
            }

            sign = src.signum;
            aexp = src.exponent;
            bexp = dst.exponent;

            Copy(src, A);
            Copy(dst, B);

            if (aexp == bexp)
            {
                // scale (shift) 2 digits
                BigNumber.Scale(A, 2);
                BigNumber.Scale(B, 2);
            }
            else
            {
                // scale to larger exponent
                if (aexp > bexp)
                {
                    BigNumber.Scale(A, 2);
                    BigNumber.Scale(B, (aexp - bexp + 2));
                }
                else
                {
                    BigNumber.Scale(B, 2);
                    BigNumber.Scale(A, (bexp - aexp + 2));
                }
            }

            adigits = A.dataLength;
            bdigits = B.dataLength;

            if (adigits >= bdigits)
            {
                Copy(A, result);

                j = (bdigits + 1) >> 1;

                carry = 0;

                while (true)
                {
                    j--;
                    result.mantissa[j] += (byte)(carry + B.mantissa[j]);

                    if (result.mantissa[j] >= 100)
                    {
                        result.mantissa[j] -= 100;
                        carry = 1;
                    }
                    else
                    {
                        carry = 0;
                    }

                    if (j == 0)
                    {
                        break;
                    }
                }
            }
            else
            {
                Copy(B, result);

                j = (adigits + 1) >> 1;

                carry = 0;

                while (true)
                {
                    j--;
                    result.mantissa[j] += (byte)(carry + A.mantissa[j]);

                    if (result.mantissa[j] >= 100)
                    {
                        result.mantissa[j] -= 100;
                        carry = 1;
                    }
                    else
                    {
                        carry = 0;
                    }

                    if (j == 0)
                    {
                        break;
                    }
                }
            }

            result.signum = sign;
            Normalize(result);
        }