0

So I have to find approximate value of pi using a formula recursively but when I pass a value into function it returns 4 as answer.

Formula: π = 4 ∗ (1−1/3+1/5−1/7+1 /9−1/11+1/13+...) for odd value

My code:

#include <iostream>
#include <cmath>
using namespace std;

float PiValue(int n){
 if (n == 0)
    return  4*1;
 else {

    return (4*(pow(-1,n)*(1/(2*n+1)))) + PiValue(n-1);
}
}

int main(){
 int odd;
 float ans;
 while(odd % 2 == 0){
     cout << "Enter odd number: ";
     cin >> odd;
     if (odd % 2 == 0)
         cout << "**It is not a odd number**" << endl;
 }
 ans = PiValue(odd);    
 cout << ans;
 return 0;

 }

If I pass 1875, I should be getting 3.14052655581 as my answer.

  • 6
    `(1/(2*n+1))` results in the integer `0` for any integer `n`, try `(1.0/(2*n+1))`. – mch Mar 23 '22 at 11:48
  • 1
    You use the variable `odd` before it has been initialized. That means it will have an *indeterminate* value, and using such values in any way leads to *undefined behavior*. You should probably have an `do ... while(...)` loop instead. – Some programmer dude Mar 23 '22 at 11:49
  • 1
    Also a recursion depth of `1875` is a little bit too much. Probably, you should think about a non-recursive algorithm. – mch Mar 23 '22 at 11:50
  • 1
    Using `pow` for calculating -1^n is a pretty bad idea... Much more efficient: If n is even, sign is positive, otherwise negative. If you want to avoid if/else, then you could have `(n & 1) * 2 - 1`. – Aconcagua Mar 23 '22 at 12:04
  • And what about *negative* `n` (someone have the initial call with one)? You would recurse until signed integer overflow (undefined behaviour!). So either accept `unsigned int`, which shows in code already what type of input is expected, or check for negative values explicitly (you could just have `<=` instead of `==`). – Aconcagua Mar 23 '22 at 12:11
  • Side note: About [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Mar 23 '22 at 12:14
  • There's no need to require an odd number, by the way. 4 for 0 is fine, 3.466.. for 2 as well, just as are 3.339..., 3.283738... and all the others. – Aconcagua Mar 23 '22 at 12:26
  • About recursion depths (see @mch): The problem is that as is, the function cannot be tail-call optimised. To be able to do so, the recursive call must be the very last thing you do in your function – in your case, an addition yet follows, though. Without TCO each function call occupies space on the stack for parameters and return value, so you risk running out of stack pretty soon! – Aconcagua Mar 23 '22 at 12:38
  • @OP Instead of trying to write the entire formula in one line `return (4*(pow(-1,n)*(1/(2*n+1)))) + PiValue(n-1);` and getting the wrong results, you could have simply broken up that one line into several lines, put those results in a variable, and inspect the values of those variables, then ask about why one value is incorrect. I'm amazed that new programmers seldom do this, and try and make sense of a (long) line of additions, subtractions, and divisions without first inspecting what the pieces of that formula result in. Next time, please do this, otherwise you will be risking a downvote. – PaulMcKenzie Mar 23 '22 at 13:19

0 Answers0