0

I am trying to approximate pi using a for loop. The user inputs the number of iterations n, and the program is supposed to get an increasingly accruate value for pi with each additional iteration. I have a nested "if" control structure that checks if "i" is even or odd. However when my loop repeats after the first run, my value for pi is not changing. Pi is staying at 1 in the loop and the final output is 4. I have to use the approximation series:

pi = 4[ 1 - 1/3 + 1/5 +...+ 1/(2n-1) + 1/(2n+1) ].

What am I doing wrong?

int _tmain(int argc, _TCHAR* argv[])
{

double pi = 0;
long i;
int n;

cout << "Enter the value of n: ";   //prompt for input
cin >> n;                           //store input in "n"
cout << endl;                       //end line

for (i = 0; i < n; i++)
{
    if (i % 2 == 0)                     //if even
        pi = pi + (1 / (2 * i + 1));
    else                                //if odd
        pi = pi - (1 / (2 * i + 1));
}

pi = 4 * pi;                            //multiply by 4

cout << endl << "pi = " << pi << endl;  //display result

system("Pause");    //pause program
return 0;           //close program
Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
ryan carter
  • 13
  • 1
  • 2
  • 4
    Integer division.`x / y` is `0` for pair of integral numbers with `x < y`. Only `i = 0` actually contributes to `pi`. – Zeta Feb 24 '16 at 20:04
  • Some code stuff: a) Don't get used to that `_t` stuff, better forget about it completely. b) Indentation. – deviantfan Feb 24 '16 at 20:06
  • Based on the example series shown, the for loop should be | for (i = 0; i <= n; i++) | . It would be a bit more accurate if you did the smaller numbers first: | for(i = n; i >= 0; i--) | . – rcgldr Feb 24 '16 at 20:22
  • @DavidHeffernan, this is not really a dupp, because integer division was not part of the question. This is a code analysis/debug question. – ThomasMcLeod Feb 24 '16 at 20:35
  • @ThomasMcLeod I can see integer division in the question – David Heffernan Feb 24 '16 at 20:42
  • @DavidHeffernan I also don't think its an *exact* duplicate. While it is similar, I am not sure this is the right dupe to close this on. – Fantastic Mr Fox Feb 24 '16 at 21:09
  • @Ben It's an exact dupe, and there are probably thousands to choose from. – David Heffernan Feb 24 '16 at 21:12
  • @DavidHeffernan, you and I may immediately recognize integer division, but OP only knew that his loop result was repeating without apparent progress. So while the answer is relates to integer division, the question was about language semantics and code analysis. – ThomasMcLeod Feb 24 '16 at 21:53
  • @Thomas Your argument is that the asker has to know the answer beforehand, which makes no sense to me – David Heffernan Feb 24 '16 at 21:59
  • @DavidHeffernan, actually I'm saying the exact opposite. Since the OP didn't know and could not have known it was an integer division issue, the question cannot be a duplicate of a question that was explicitly about integer division. – ThomasMcLeod Feb 24 '16 at 22:11
  • @Thomas You've misread the other question. The asker of that question expected real division rather then integer division. – David Heffernan Feb 24 '16 at 22:17

1 Answers1

0

The problem is you are using ints when calculating a floating point number. Read about this here: Why can't I return a double from two ints being divided

So to avoid this you need to do the math with floating point numbers:

pi = pi + (1.0 / (2.0 * i + 1.0));

In fact it is a good idea to always explicitly specify when you intend to use floating point, even if it is unnecessary:

const float pi_real = 3.14159;
const float one = 1.0;

This makes your intention clear and avoids mistakes like this. Here is a live example.

Community
  • 1
  • 1
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175