求π的值

点击查看代码
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double arctanx(double x)
{
	double sum=0.0;
	double one=x;
	int n=1;
	while (fabs(one) >= 0.000001)
	{
		if (n % 4 == 1)
		{
			sum = sum + one;
		}
		else if (n % 4 == 3)
		{
			sum = sum - one;
		}
		one = one * n * x * x;
		n = n + 2;
		one = one / n;
	}
	return sum;
}
int main()
{
	double x=1.0/sqrt(3);
	double pi;
	pi = 6*arctanx(x);
	cout << fixed << setprecision(10) << pi << endl;
	return 0;
}
posted @ 2025-10-25 17:36  一只牛油果  阅读(5)  评论(0)    收藏  举报