/// <summary> /// calling function is divided by value passed in. /// /// </summary> /// <param name="divValue"></param> /// <returns></returns> public InfInt Divide(InfInt divValue) { InfInt quotient = new InfInt(); InfInt Dividend = this; // assigned to new infint because 'this' is read only try { if (divValue.ToString() == "0" || divValue.ToString() == "-0") //cannot divide by 0 { Console.WriteLine("Cannot Divide by 0"); return(null); } else { // if the divisor is not bigger than dividend the divide while (Dividend.IsGreaterThan(divValue)) { // if the divisor is not bigger than dividend the divide Dividend = Dividend.Subtract(divValue); quotient = quotient.Add(new InfInt("1")); // increment quotient } return(quotient); } } catch (FormatException badDivisor) { throw badDivisor; } }
public InfInt Multiply(InfInt multValue) { InfInt temp = new InfInt("0"); int carry = 0; if ((multValue.ToString() == "0" || multValue.ToString() == "-0") || (this.ToString() == "0" || this.ToString() == "-0")) { temp = new InfInt("0"); return(temp); } else { for (int i = (DIGITS - 1); i >= 0; i--) { int offset = i; carry = 0; for (int j = (DIGITS - 1); j >= 0; j--) { temp.Integer[j + offset] = temp.Integer[j + offset] + (this.Integer[i] * multValue.Integer[j]) + carry; if (temp.Integer[j] > 9) { carry = temp.Integer[j] / 10; temp.Integer[j] %= 10; } else { carry = 0; } } } return(temp); } }
public int CompareTo(object obj) { try { InfInt otherInt = (InfInt)obj; if (Integer.ToString() == otherInt.ToString()) //check if exact same value before looping { return(0); } else { for (int i = 0; i < DIGITS; i++) { if (Integer[i] > otherInt.Integer[i]) { return(1); } else if (Integer[i] < otherInt.Integer[i]) { return(-1); } } return(0); } } catch (Exception e) { throw e; } }