圆周率的求法.cpp

来自「是各种常用算法的集合」· C++ 代码 · 共 30 行

CPP
30
字号
//利用级数公式近似求解PI
#include<iostream.h>
#include<iomanip.h>
#include<math.h>

void main()
{
	double pi=0;
	double x=1;
	long k=1;
	int sign=1;

	while(fabs(x)>1e-8)
		//fabs():Calculates the absolute value of the floating-point argument
	{
		pi=pi+x;
		k=k+2;
		sign=-sign;
		x=sign/double(k);
	}

	pi=pi*4;
	cout<<"The value of PI is :"
		<<setiosflags(ios::fixed)  
		//Insert floating-point values in fixed-point format 
		//(with no exponent field)
		<<setprecision(10)
		//Set the stream's internal floating-point precision 
		<<pi<<endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?