随笔 - 26  文章 - 0  评论 - 0  阅读 - 2582

用arctanx求∏的值

一、问题描述:

编写程序求∏的值,公式如下:

∏ = 16arctan(1/5) - 4arctan(1/239)

arctanx = x - x3/3 + x5/5 - x7/7 + …直到级数某项绝对值不大于10-15为止;∏和x均为double型

二、设计思路:

1.写一个arctan函数;

2.根据公式求∏;

3.输出∏。

三、流程图:

四、伪代码实现:

复制代码
//arctan函数
e<-x
i<-1
r<-0
while e/i>1e-15
do f=e/i
    if i%4==1
    then r=r+f
    else 
    then r=r-f
    e=e*x*x
    i=i+2
end
return r
//主函数
a=16arctan(1/5)
b=4arctan(1/239)
PI=a-b
    
复制代码

 

五、代码实现:

复制代码
 1 #include <iostream>
 2 using namespace std;
 3 
 4 double arctan(double x)
 5 {
 6     double sqr = x * x;
 7     double e = x;
 8     double r = 0;
 9     int i = 1;
10     while (e / i > 1e-15)
11     {
12         double f = e / i;
13         r = (i % 4 == 1) ? r + f : r - f;
14         e = e * sqr;
15         i += 2;
16     }
17     return r;
18 }
19 
20 int main()
21 {
22     double a = 16.0 * arctan(1 / 5.0);
23     double b = 4.0 * arctan(1 / 239.0);
24     cout << "PI=" << a - b << endl;
25     return 0;
26 }
复制代码

 

 

 

 

posted on   轻挼草色  阅读(162)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示