How can I create a recursive function to mimic long division in C++?

  • Context: C/C++ 
  • Thread starter Thread starter FallArk
  • Start date Start date
  • Tags Tags
    C++ Recursion
Click For Summary

Discussion Overview

The discussion revolves around creating a recursive function in C++ to mimic long division, particularly focusing on how to handle decimal outputs for fractions like 1/3. Participants explore the mechanics of long division, the appropriate data types, and the structure of the recursive function.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Exploratory

Main Points Raised

  • Some participants note that integer division in C++ outputs only the integer part, leading to confusion about how to represent repeating decimals.
  • There is a suggestion to use a recursive function to perform long division, with an emphasis on outputting decimal points after the integer division.
  • Participants discuss the need to output a specific number of decimal places and question the appropriate return type for the function, with suggestions for returning a string or using void.
  • One participant proposes a structure for the recursive function, emphasizing the need for a base case to stop recursion when the desired number of decimal places is reached.
  • Another participant suggests cleaning up the function by inverting the if statement for better readability and structure.

Areas of Agreement / Disagreement

Participants generally agree on the need for a recursive approach to mimic long division, but there are varying opinions on the best way to implement the function, including return types and output methods. The discussion remains unresolved regarding the optimal implementation details.

Contextual Notes

Participants express uncertainty about the correct data types for the function parameters and the implications of using different output methods. There are also discussions about the limitations of printing infinite decimal places and the potential for infinite loops.

Who May Find This Useful

This discussion may be useful for C++ programmers interested in numerical methods, particularly those looking to implement custom division algorithms or handle decimal representations in programming.

FallArk
Messages
127
Reaction score
0
When I do things like
Code:
cout << 1/3;
in C++, it will typically output 0.333333, but the actual answer should be 0.3333333 with an infinite amount of 3s, how should I make a function such that will mimic this division recursively?
 
Technology news on Phys.org
FallArk said:
When I do things like
Code:
cout << 1/3;
in C++, it will typically output 0.333333, but the actual answer should be 0.3333333 with an infinite amount of 3s, how should I make a function such that will mimic this division recursively?

Hey FallArk! ;)

Do you know how to do long divisions?
That's the way to do it - with a recursive function to make it happen.

(Btw, your example code will output [m]0[/m] instead of [m]0.3333333[/m]. Know why?)
 
I like Serena said:
Hey FallArk! ;)

Do you know how to do long divisions?
That's the way to do it - with a recursive function to make it happen.

(Btw, your example code will output [m]0[/m] instead of [m]0.3333333[/m]. Know why?)

I know it's int so it outputs 0. Since that's the integer part. But would you mind explaining the long division and how it works? Thank you!
 
FallArk said:
I know it's int so it outputs 0. Since that's the integer part. But would you mind explaining the long division and how it works? Thank you!

Well... divide 1 by 3 as integers and output the result 0.
The remainder is 1.
Multiply the remainder by 10 and repeat.

Oh, and we need to output the decimal point after the first division.

More specifically:
\begin{array}{ccccc}
Dividend & Divisor & Quotient & Remainder & Output \\
1 & 3 & 0 & 1 & 0. \\
10 & 3 & 3 & 1 & 0.3 \\
10 & 3 & 3 & 1 & 0.33 \\
10 & 3 & 3 & 1 & 0.333 \\
...
\end{array}
 
I like Serena said:
Well... divide 1 by 3 as integers and output the result 0.
The remainder is 1.
Multiply the remainder by 10 and repeat.

Oh, and we need to output the decimal point after the first division.

More specifically:
\begin{array}{ccccc}
Dividend & Divisor & Quotient & Remainder & Output \\
1 & 3 & 0 & 1 & 0. \\
10 & 3 & 3 & 1 & 0.3 \\
10 & 3 & 3 & 1 & 0.33 \\
10 & 3 & 3 & 1 & 0.333 \\
...
\end{array}

If I want to output to, let's say, 10 decimal places. And I do a recursive function:
Code:
double longDivision (int dividend, int divisor, int decimalplaces){} // Are the data types correct?
What would be the general case when the function returns an output?
I want to say that when the amount of numbers after the decimal matches
Code:
int decimalplaces
,
is that correct?
 
FallArk said:
If I want to output to, let's say, 10 decimal places. And I do a recursive function:
Code:
double longDivision (int dividend, int divisor, int decimalplaces){} // Are the data types correct?
What would be the general case when the function returns an output?
I want to say that when the amount of numbers after the decimal matches
Code:
int decimalplaces
,
is that correct?

Looks fine to me.
Keep going! (Nod)

Oh, and the function shouldn't return a double.
Either it should return a string, or it should simply output the result to cout (and return void).
 
I like Serena said:
Looks fine to me.
Keep going! (Nod)

Oh, and the function shouldn't return a double.
Either it should return a string, or it should simply output the result to count (and return void).

Cool! I will keep working on it!
 
Since making it print infinite decimal places would be pointless as that's the equivalent of an infinite loop why not just use the iomanip library or printf to output the number of decimal places you want to be output.
 
squidsk said:
Since making it print infinite decimal places would be pointless as that's the equivalent of an infinite loop why not just use the iomanip library or printf to output the number of decimal places you want to be output.

Because:
Code:
#include <stdio.h>
int main() {
    printf("%.20f\n", 1.0/3);
    return 0;
}
results in:
Code:
0.33333333333333331483
 
  • #10
I like Serena said:
Looks fine to me.
Keep going! (Nod)

Oh, and the function shouldn't return a double.
Either it should return a string, or it should simply output the result to cout (and return void).

I know how to do long division but how should I make it recursive?
Code:
void longDivision(int dividend, int divisor, int decimalplace){
    int x = dividend / divisor;
    cout << x;
    dividend = (dividend - (x * divisor)) * 10;
    cout << dividend / divisor;
}
 
  • #11
FallArk said:
I know how to do long division but how should I make it recursive?
Code:
void longDivision(int dividend, int divisor, int decimalplace){
    int x = dividend / divisor;
    cout << x;
    dividend = (dividend - (x * divisor)) * 10;
    cout << dividend / divisor;
}

Instead of the 2nd cout, we should have a call to longDivision(dividend, divisor, decimalPlaces -1).
Oh, and there should be an if to stop doing that when we have all the decimal places.
 
  • #12
I like Serena said:
Instead of the 2nd cout, we should have a call to longDivision(dividend, divisor, decimalPlaces -1).
Oh, and there should be an if to stop doing that when we have all the decimal places.

Duh! How did I not see that!
Thank you!
And here is my solution:
Code:
void longDivision(int dividend, int divisor, int decimalplace){
    if (decimalplace == 0) {
        return;
    }
    else {
        cout << dividend / divisor;
        longDivision((dividend % divisor) * 10, divisor, decimalplace - 1);
    }
}
:D
 
  • #13
You can clean up your solution by inverting the if statement as follows:

Code:
void longDivision(int dividend, int divisor, int decimalplace){
    if (decimalplace > 0) {
        cout << dividend / divisor;
        longDivision((dividend % divisor) * 10, divisor, decimalplace - 1);
    }
}

This eliminates the else and the empty return. In general it is better to have a single point of exit from a function.
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
12K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
81
Views
7K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K