I have this assignment
The value of Pi can be determined by the following product
2 * 2 4 * 4 6 * 6 N * N
Pi = 2 * ----- * ----- * ----- * ... * -----------------
1 * 3 3 * 5 5 * 7 (N - 1) * (N + 1)
Write a C program that calculates the approximated value of Pi as long as the general term is greater than 1 + 10-9.
To solve it, I wrote this code:
#include <stdio.h>
#include <stdlib.h>
int main() {
double pi;
const double End =
1.0 + (1.0 / (10.0 * 10.0 * 10.0 * 10.0 * 10.0 * 10.0 * 10.0 * 10.0 * 10.0));
double N = 2.0;
pi = 2.0 * ((N * N) / ((N - 1.0) * (N + 1.0)));
while (pi > End) {
N += 2.0;
pi *= ((N * N) / ((N - 1.0) * (N + 1.0)));
}
printf("%lf", pi);
return 0;
}
I can't really understand how things work. I managed to use only double
variables and added .0
to all the number literals, but the program is stuck and doesn't give any value when I launch it.
Why?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…