Write a program to get an approximation of π with a precision of 10^(-5) using the following formula:
π=4.0×(1-1/3+1/5-1/7+⋯+〖(-1)〗^(n-1) 1/(2n-1))
hints:
initialize pi =0 and i=1
the absolute value of the adding item determines the precision you can get when putting more items to pi, thus
adding item: 〖(-1)〗^(i-1) 1/(2i-1), then
when abs(adding item) > precision,
update pi with pi += adding item
Собственно это вычисление числа Pi через ряд Лейбница:
#include <iostream>
using namespace std;
int main()
{
double pi = 0;
double sign = 1.;
const double eps = 1e-5;
int i = 0;
do
{
pi += sign / (2. * i + 1.);
sign = -sign;
i++;
} while (1. / (2. * i + 1.) > eps);
pi *= 4;
printf("pi=%10.5lf", pi);
return 0;
}