#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 发布
本文介绍了一种使用arctan函数计算π值的C++实现方法。通过定义arctan函数并设置精度限制,文章展示了如何调用该函数来求得π的近似值。这种方法利用了级数展开的原理,通过不断迭代直到达到预设的精度要求。
7855

被折叠的 条评论
为什么被折叠?



