//Divides two InfInt's public InfInt Divide(InfInt divValue) { InfInt zero = new InfInt(); InfInt one = new InfInt("1"); InfInt ten = new InfInt("10"); //Numerator smaller than denominator if (this.CompareTo(divValue) == -1) { return(zero); } //Numerator == Denominator if (this.CompareTo(divValue) == 0) { return(one); } //Divide by zero if (divValue.CompareTo(zero) == 0) { Console.WriteLine("Oops, you tried to divide by zero!"); return(zero); } //Numerator larger than denominator int i = 0; InfInt returnValue = new InfInt(); InfInt tempThis = new InfInt(); while (i < DIGITS) { //Drop down a value int tempNumber = this.Integer[i]; string tempString = Convert.ToString(tempNumber); InfInt tempInfInt = new InfInt(tempString); for (int l = 0; l < DIGITS; l++) { tempThis = tempThis.Add(tempInfInt); //divValue divides into tempThis if (tempThis.CompareTo(divValue) >= 0) //See how many times divValue goes into tempThis { InfInt tempReturnValue = new InfInt("1"); while (divValue.CompareTo(tempThis.Subtract(divValue.Multiply(tempReturnValue))) <= 0) { tempReturnValue = tempReturnValue.Add(one); } returnValue.Integer[i] = tempReturnValue.Integer[DIGITS - 1]; tempThis = tempThis.Subtract(tempReturnValue.Multiply(divValue)); } tempThis.Positive = true; tempThis = tempThis.Multiply(ten); i++; } } if (Positive != divValue.Positive) { returnValue.Positive = false; } return(returnValue); }
//Converts InfInt to string public override string ToString() { int i = 0; InfInt zero = new InfInt(); zero.Positive = false; if (zero.CompareTo(this) == 0) { return("0"); } string returnString = ""; if (!Positive) { returnString += '-'; } while (Integer[i] == 0 && i < DIGITS - 1) { i++; } while (i < DIGITS) { returnString += Convert.ToString(Integer[i]); i++; } return(returnString); }
//Multiplies 2 InfInt's public InfInt Multiply(InfInt multValue) { int lengthInput = DIGITS; int lengthMultValue = DIGITS; InfInt zero = new InfInt("0"); if (this.CompareTo(zero) == 0 || multValue.CompareTo(zero) == 0) { return(zero); } //Find lengths of both multiplied values while (Integer[DIGITS - lengthInput] == 0) { if (lengthInput == 0) { break; } lengthInput--; } while (multValue.Integer[DIGITS - lengthMultValue] == 0) { if (lengthMultValue == 0) { break; } lengthMultValue--; } InfInt returnValue = new InfInt(); //Iterate through each value in one array, multiplying it by each value in the other array for (int i = 0; i < lengthMultValue; i++) { returnValue = returnValue.Add(this.MultiplyThese(multValue.Integer[DIGITS - 1 - i], lengthInput, i)); } //If the multiplied numbers have different signs, the result is negative if (this.Positive != multValue.Positive) { returnValue.Positive = false; } return(returnValue); }