I understand there are probably much better ways of calculating PI, but since I found the Leibniz formula:
I decided to implement it in C using recursion:
double pi(int n){
if(n==1)return 4;
return 4*pow(-1,n+1)*(1/(2*n-1))+pi(n-1);
}
What I always get when I call the function is just 4.000000, meaning it works for number 1 only; Every other number gives me the same result.
What I'm wondering is why this solution isn't working properly. I've tried writing every step down on paper for different numbers and the logic behind it seems correct.
EDIT: Except for what was provided in the answer (thank you!) it seems like (1/(double)(2*n-1)) instead of just (1/(2*n-1)) fixed the problem as well.