public VeryLong DivideBy(VeryLong divisor, out VeryLong remainder) { string dividendString = ""; string divisorString = divisor.ToString(); string quotientDigit = ""; string quotient = ""; string mult = ""; string diff = ""; for (int i = 0; i < Length(); i++) { dividendString += GetFirstDigit(i); dividendString = VeryLong.RemoveLeadingZeros(dividendString); quotientDigit = DivideSimple(dividendString, divisorString); quotient += quotientDigit; mult = (new VeryLong(quotientDigit).Multiply(new VeryLong(divisorString))).ToString(); diff = (new VeryLong(dividendString).Subtract(new VeryLong(mult))).ToString(); dividendString = diff; } VeryLong quotientResult = new VeryLong(quotient); remainder = new VeryLong(diff); quotientResult.RemoveLeadingZeros(); return(quotientResult); }
public VeryLong Multiply(VeryLong other) { int numberDecimalPlaces = GetDecimalPlaces(); int numberDecimalPlacesOther = other.GetDecimalPlaces(); int resultDecialPlaces = numberDecimalPlaces + numberDecimalPlacesOther; other = RemoveDecimalPlaces(other); string integerStringCopy = _integerString; _integerString = _integerString.Replace(".", ""); List <string> multiplicationResults = new List <string>(); string multiplicationResult = GetZeros(1); int currentLastDigit = 0; int currentLastDigitOther = 0; int resultDigit = 0; int behalte = 0; int mult = 0; int indexOther = 0; int index = 0; while (Length() > index) { indexOther = 0; behalte = 0; multiplicationResult = GetZeros(index); while (other.Length() > indexOther || behalte != 0) { currentLastDigit = int.Parse(GetLastDigit(index)); currentLastDigitOther = int.Parse(other.GetLastDigit(indexOther)); mult = currentLastDigit * currentLastDigitOther + behalte; behalte = mult / 10; resultDigit = mult % 10; multiplicationResult = multiplicationResult.Insert(0, resultDigit.ToString()); indexOther++; } index++; multiplicationResults.Add(multiplicationResult); } VeryLong mainResult = VeryLong.AddSummands(multiplicationResults); mainResult.SetDecimalPlaces(resultDecialPlaces); mainResult.RemoveLeadingZeros(); _integerString = integerStringCopy; return(mainResult); }
public VeryLong DivideBy(VeryLong divisor, int numberOfDigits) { int numberDecimalPlacesDivisor = divisor.GetDecimalPlaces(); divisor = RemoveDecimalPlaces(divisor); FillUpDecimalPlacesWithZero(numberOfDigits + numberDecimalPlacesDivisor); string dividendString = ""; string divisorString = divisor.ToString(); string quotientDigit = ""; string quotient = ""; string mult = ""; string diff = ""; for (int i = 0; i < Length(); i++) { if (GetFirstDigit(i) == ".") { quotient += "."; continue; } dividendString += GetFirstDigit(i); dividendString = VeryLong.RemoveLeadingZeros(dividendString); quotientDigit = DivideSimple(dividendString, divisorString); quotient += quotientDigit; mult = (new VeryLong(quotientDigit).Multiply(new VeryLong(divisorString))).ToString(); diff = (new VeryLong(dividendString).Subtract(new VeryLong(mult))).ToString(); dividendString = diff; } VeryLong quotientResult = new VeryLong(quotient); int resultDecimalPlaces = quotientResult.GetDecimalPlaces(); quotientResult = RemoveDecimalPlaces(quotientResult); quotientResult.SetDecimalPlaces(resultDecimalPlaces - numberDecimalPlacesDivisor); quotientResult.RemoveLeadingZeros(); quotientResult.LimitPrecisionTo(numberOfDigits); return(quotientResult); }
public void RemoveLeadingZeros() { _integerString = VeryLong.RemoveLeadingZeros(_integerString); }