//freebie add courtesy of professor amack public InfInt Add(InfInt addValue) { InfInt temp = new InfInt(); if (Positive == addValue.Positive) { temp = AddPositives(addValue); } //addvalue is negative else if (Positive && (!addValue.Positive)) { addValue.Positive = true; if (IsGreaterThan(addValue)) { temp = SubtractPositives(addValue); } else { temp = addValue.SubtractPositives(this); temp.Positive = false; } addValue.Positive = false; } else if (!Positive && addValue.Positive) { addValue.Positive = false; if (IsGreaterThan(addValue)) { temp = addValue.SubtractPositives(this); } else { temp = SubtractPositives(addValue); temp.Positive = false; } addValue.Positive = true; } return(temp); }
/// <summary> /// Subtracts from InfInt by looping through array at DIGITS-1 to 0 /// uses helper functions like greaterthan and subtractpositives /// </summary> /// <param name="subtractValue"></param> /// <returns>negative or positive InfInt Value</returns> public InfInt Subtract(InfInt subtractValue) { InfInt temp = new InfInt(); // temporary result // subtractValue is negative if (Positive && (!subtractValue.Positive)) { temp = AddPositives(subtractValue); } // this InfInt is negative else if (!Positive && subtractValue.Positive) { temp = AddPositives(subtractValue); } // at this point, both InfInts have the same sign else { bool isPositive = Positive; // original sign bool resultPositive = Positive; // sign of the result // set both to positive so we can compare absolute values Positive = true; subtractValue.Positive = true; if (this.IsGreaterThan(subtractValue)) { temp = this.SubtractPositives(subtractValue); } else { temp = subtractValue.SubtractPositives(this); resultPositive = !isPositive; // flip the sign } Positive = isPositive; subtractValue.Positive = isPositive; temp.Positive = resultPositive; } return(temp); }