#include<iostream>
using namespace std;
// 定义函数
double arctan( double x )
{
double sqr = x * x ;
double e = x ;
double r = 0 ;
int i = 1 ;
while (e / i > 1e-15) // 约束精度
{
double f = e / i ;
r = (i % 4 == 1) ? r + f: r - f ;
e = e * sqr ;
i += 2 ;
}
return r;
}
// 调用函数求值
int main(){
double a = 16.0 * arctan(1 / 5.0) ;
double b = 4.0 * arctan(1/239.0) ;
cout<<"PI = "<< a - b << endl;
return 0 ;
}
运用c++求PI的值(定义函数法)
最新推荐文章于 2024-04-15 21:08:00 发布