鱼C论坛

 找回密码
 立即注册
查看: 503|回复: 2

为什么答案是错的

[复制链接]
发表于 2023-12-24 22:21:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<iostream>
#include<iostream>
#include<cmath>
using namespace std;
double arctan(double x)
{
        int i = 1;
        double sum = 0;
        double fx = x;
        while (fx > 1e-15 || fx < -1e-15)
        {
                int base = 1;
               
                fx = base * pow(x, i) / i;
        sum += fx;
                base = -base;
                i = i + 2;

        }
        return sum;
}

int main()
{
        double b;
        double c;
        double a;
        b = 16 * arctan(1 / 5.0);
        c = 4 * arctan(1 / 239.0);
        a = b - c;

        cout << arctan(1) << endl;
        cout << a << endl;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-24 22:21:30 | 显示全部楼层
这段代码的问题在于,在头文件的包含部分有错误,应该是 `#include <iostream>` ,`#include <cmath>` 和 `#include <iomanip>`。另外,`using namespace std;` 可以放在头文件的包含后面。

此外,在计算 `b` 和 `c` 的时候,使用了整数除法,应该使用浮点数除法,可以将 `1` 改为 `1.0` 或 `1.0f`,如:`b = 16 * arctan(1.0 / 5.0);`。

最后,注意在 `main()` 函数中结束 `cout` 语句的写法,应该是 `cout << a;`。

以下是已经修正的代码:
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. using namespace std;
  5. double arctan(double x)
  6. {
  7.     int i = 1;
  8.     double sum = 0;
  9.     double fx = x;
  10.     while (fx > 1e-15 || fx < -1e-15)
  11.     {
  12.         int base = 1;
  13.         fx = base * pow(x, i) / i;
  14.         sum += fx;
  15.         base = -base;
  16.         i = i + 2;
  17.     }
  18.     return sum;
  19. }
  20. int main()
  21. {
  22.     double b;
  23.     double c;
  24.     double a;
  25.     b = 16 * arctan(1.0 / 5.0);
  26.     c = 4 * arctan(1.0 / 239.0);
  27.     a = b - c;
  28.     cout << setprecision(16) << a;
  29.     return 0;
  30. }
复制代码

请注意,我还在输出结果之前添加了 `setprecision(16)` 来设置输出的精度,以便更准确地显示小数位数。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-24 22:49:03 | 显示全部楼层
本帖最后由 jackz007 于 2023-12-24 23:52 编辑
  1.         while (fx > 1e-15 || fx < -1e-15)
  2.         {
  3.                 int base = 1 ; // 不可以在循环体内定义这个变量 base, 因为,每次循环到这里,变量 base 都会被重新定义,其值都将被初始化为 1
  4.         . . . . . .
  5.                 base = -base ; // 如果选择在循环体内定义变量 base,那么,这一句代码就会完全失效。
复制代码


【版本1】:使用 pow() 函数
  1. #include<iostream>
  2. #include<cmath>

  3. using namespace std                           ;

  4. double arctan(double x)
  5. {
  6.         int i , base                          ;
  7.         double d , sum                        ;
  8.         for(i = base = 1 , sum = 0 ;; i += 2 , base = -base) {
  9.                 d = pow(x , i) / i            ;
  10.                 if(d > 1e-15) sum += base * d ;
  11.                 else break                    ;
  12.         }
  13.         return sum                            ;
  14. }

  15. int main()
  16. {
  17.         double a , b , c                      ;
  18.         b = 16 * arctan(1 / 5.0)              ;
  19.         c = 4 * arctan(1 / 239.0)             ;
  20.         a = b - c                             ;
  21.         cout << arctan(1) << endl             ;
  22.         cout << a << endl                     ;
  23. }
复制代码


【版本2】:不使用 pow() 函数
  1. #include<iostream>

  2. using namespace std                           ;

  3. double arctan(double x)
  4. {
  5.         int i , base                          ;
  6.         double d , e , sum                    ;
  7.         for(d = e = sum = x , base = -1 , i = 3 ;; i += 2 , base = -base) {
  8.                 e = e * x * x                 ;  // e = pow(x , i)
  9.                 d = e / i                     ;
  10.                 if(d > 1e-15) sum += base * d ;
  11.                 else break                    ;
  12.         }
  13.         return sum                            ;
  14. }

  15. int main()
  16. {
  17.         double a , b , c                      ;
  18.         b = 16 * arctan(1 / 5.0)              ;
  19.         c = 4 * arctan(1 / 239.0)             ;
  20.         a = b - c                             ;
  21.         cout << arctan(1) << endl             ;
  22.         cout << a << endl                     ;
  23. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 15:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表