Exemple #1
0
        /// <summary>
        /// signed subtraction of 2 numbers.
        /// </summary>
        /// <param name="n1"></param>
        /// <param name="n2"></param>
        /// <returns></returns>
        public static BigInt Sub(BigInt n1, BigInt n2)
        {
            if (n1.digitArray.Length != n2.digitArray.Length) MakeSafe(ref n1, ref n2);
            BigInt result;

            if ((n1.sign && !n2.sign) || (!n1.sign && n2.sign))
            {
                result = new BigInt(n1);
                result.AddInternal(n2);
                result.sign = n1.sign;
            }
            else
            {
                bool lessThan = LtInt(n1, n2);

                if (lessThan)
                {
                    result = new BigInt(n2);
                    result.SubInternal(n1);
                    result.sign = !n1.sign;
                }
                else
                {
                    result = new BigInt(n1);
                    result.SubInternal(n2);
                    result.sign = n1.sign;
                }
            }

            return result;
        }