### Help With Looping

I need to compute a pi approximation with the MadhavaLeibniz series. http://en.wikipedia.org/wiki/Leibniz it takes in user input and iterates the series that many times for precision. I need to output it as a double to 15 digets but the only thing I get out is 4 every time. I know the issue is in the mySum function because when I debug it, sum never changes after the first iteration.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768`` ``````#include using std::cin; using std::cout; using std::endl; #include // To access cout formatting settings using std::setprecision; // Function prototypes, used to make the main function "clean" double mySum(int user_input); int myProduct(int k); int myPrompt(void); void myPrint(double result); int main() { int user_input; double result; do { user_input = myPrompt(); }while (user_input < 0); result = mySum(user_input); myPrint(result); return 0; } int myPrompt(void) { int user_input; cout << "Enter a non-negative integer: "; cin >> user_input; return user_input; } double mySum(int user_input) { double sum = 0; int k; for(int k = 0; k <= user_input; k++) { sum = sum + (4 * (((myProduct(k))/((2 * k) + 1)))); } return sum; } int myProduct(int k) { if (k % 2 == 0) { return 1; } else { return -1; } } void myPrint(double result) { cout << "Approximation of pi is "<< setprecision(15) << result << endl; }``````
Presumably, the user_input is supposed to a positive number. But look at your do... while loop: what is that loop condition doing? When will that loop ever execute more than once?
That's just error checking. Not what my issue is
the reason sum is always 4 is because your formula must always be equal to 1. Like this...

sum = sum + (4 * (((myProduct(k))/((2 * k) + 1))));
sum = sum + (4 * 1);
sum = 4;

check this to see what is going on...
Why is that always going to be 1? For example when the user inputs 1...the first loop will be
sum = sum + (4 * ((1)/(2*0+1))
sum = sum + (4 * 1)
but the second iteration would be
sum = sum + (4 * ((-1)/(2*1+1))
sum = sum + (4 * (-1/3))
sum = sum - 4/3
and since the previous sum was 4...it should be equal to the result of 4 - 4/3. Why isn't it that?
At line 47 there is an integer division. Anything after the decimal point is discarded.
How would I fix that to be double division?
Make sure that at least one of the values (either numerator or denominator, or both) is of type `double`.
So can I just declare the product function as double instead of int?
Did you try that?
Not yet. I won't be able to for a couple hours. We've been learning about static casting so maybe I could use that?
That would work too. When you get the chance, try each approach, to reassure yourself of what does or does not work.
Okay thanks. Also how do I output it to 15 figures? Currently it displays one
`setprecision(15)` tells the computer to output the value to 15 significant figures. But, if there are trailing zeros, they will be suppressed.

Once you get the calculation working correctly, you should see more digits. However, you could additionally use `cout << std::fixed` which changes the behaviour, so that instead it will always print 15 digits (in this case) after the decimal point, regardless of how many digits appear in front of the decimal point, and even when there are trailing zeros.
One other comment. There are too many parentheses. These don't do any harm, but sure make the code hard to read. You could cut it down to (something like) this:
` sum = sum + 4 * myProduct(k) / (2 * k + 1); `

(Of course that still includes the original error, but we already discussed that.)
Last edited on
So I would need using::std fixed; and then I would put fixed << before setprecision(15)?
And yeah I realized that. They were just inserted to make sure my error wasn't coming from that
The order doesn't matter, the cout stream will remember the last-used setting, except for `std::setw()` which only applies to the next item.
Thank you
Topic archived. No new replies allowed.