/// <summary>Adds the values of two specified <see cref="BigInteger" /> objects.</summary> /// <returns>The sum of <paramref name="left" /> and <paramref name="right" />.</returns> /// <param name="left">The first value to add.</param> /// <param name="right">The second value to add.</param> public static BigInteger operator +(BigInteger left, BigInteger right) { if (right.IsZero) { return(left); } if (left.IsZero) { return(right); } var signLeft = 1; var signRight = 1; var regLeft = new BigIntegerBuilder(left, ref signLeft); var regRight = new BigIntegerBuilder(right, ref signRight); if (signLeft != signRight) { regLeft.Sub(ref signLeft, ref regRight); } else { regLeft.Add(ref regRight); } return(regLeft.GetInteger(signLeft)); }
/// <summary> /// Subtracts a <see cref="BigInteger" /> value from another /// <see cref="BigInteger" /> value. /// </summary> /// <returns>The result of subtracting <paramref name="right" /> from <paramref name="left" />.</returns> /// <param name="left">The value to subtract from (the minuend).</param> /// <param name="right">The value to subtract (the subtrahend).</param> public static BigInteger operator -(BigInteger left, BigInteger right) { if (right.IsZero) { return(left); } if (left.IsZero) { return(-right); } var leftSign = 1; var rightSign = -1; var regLeft = new BigIntegerBuilder(left, ref leftSign); var regRight = new BigIntegerBuilder(right, ref rightSign); if (leftSign != rightSign) { regLeft.Sub(ref leftSign, ref regRight); } else { regLeft.Add(ref regRight); } return(regLeft.GetInteger(leftSign)); }