ccsmiles
2016-02-16 15:02
采纳率: 100%
浏览 1.4k

求助:c++求pi,编译通过,但运行后无任何结果?

图片说明
#include "stdafx.h"
#include
using namespace std;

int main()
{
double arctan(double);
cout << "pi= " << 16 * arctan(1 / 5.0) - 4 * arctan(1 / 239.0) << endl;
return 0;
}

double arctan(double x)
{
double result = 0;
int i = 1, k = 1;
do
{
for (k = 1; k <= (2 * i - 1); k++)
k *= x;
switch (i % 2)
{
case 1:
result += (k / (2 * i - 1)); break;
case 0:
result -= (k / (2 * i - 1)); break;
}
i++;
} while (k > 1e-15);
return result;
}

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • threenewbee 2016-02-16 15:13
    已采纳
     #include<iostream>
    using namespace std;
    
    double arctan(double);
    
    int main()
    {
        cout << "pi= " << 16 * arctan(1 / 5.0) - 4 * arctan(1 / 239.0) << endl;
        return 0;
    }
    
    double arctan(double x)
    {
        double result = 0;
        int i = 1, s = 1;
            double k = 1.0;
        do
        {
            double t = 1;
            for (int j = 0; j < (i * 2 - 1); j++)
                t *= x;
                    k = t / (double)(i * 2 - 1) * (double)s;
            result = result + k;
            s = -s;
            i++;
        } while (k > 1e-15);
        return result;
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • threenewbee 2016-02-16 15:13

    pi= 3.1406

    评论
    解决 无用
    打赏 举报
  • Eleven 2016-02-17 07:27

    什么无结果?你是怎样运行的?

    评论
    解决 无用
    打赏 举报
  • ccsmiles 2016-02-18 01:59
     #include "stdafx.h"
    #include<iostream>
    using namespace std;
    
    double arctan(double);
    
    int main()
    {
        double a = 16 * arctan(1 / 5.0);
        double b = 4 * arctan(1 / 239.0);
        cout << "pi= " << a-b << endl;
        return 0;
    }
    
    double arctan(double x)
    {
        double result = 0;
        int i = 1, j = 1;
        double k = 1;
        do
        {
            for (j = 1; j <= (2 * i - 1); j++)
                k *= x;
            switch (i % 2)
            {
            case 1:
                result += (k / (2 * i - 1)); break;
            case 0:
                result -= (k / (2 * i - 1)); break;
            }
            i++;
        } while (k > 1e-15);
        return result;
    }
    
    评论
    解决 无用
    打赏 举报
  • ccsmiles 2016-02-18 02:00

    vs2015运行的结果怎么是3.17473?

    评论
    解决 无用
    打赏 举报
  • ccsmiles 2016-02-18 02:22

    答案是这样的:

     #include "stdafx.h"
    #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;
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题