计算圆周率PI

  • 服务器地址 172.16.192.24

  • 用户名 2019301110060

  • 计算圆周率

#include <time.h>
    #include <stdio.h>
    double step;
    int main(){
        static long num_steps;
        printf("请输入一个1000以上的数字求取PI值\n>>");
        scanf("%ld",&num_steps);
        clock_t start_t, end_t;
        double total_t;
        int i;
        double x,pi,sum =0.0;
        start_t = clock();
        step=1.0/(double)num_steps; //将1平分成100000步
        for(i=1;i<=num_steps;i++){
            x=(i-0.5)*step;//获得0-1的连续值
            sum+=4.0/(1.0+x*x);
        }
        pi =step * sum; //
        end_t = clock();
        total_t=(double)(end_t - start_t)/CLOCKS_PER_SEC;
        printf("运行时间为%fs\t%.10f\r\n",total_t,pi);
        return 0;

    }
  • 加上时间和精度的控制

    #include <time.h>
    #include <stdio.h>
    double step;
    int main()
    {
        static long num_steps;
        // printf("请输入一个1000以上的数字求取PI值\n>>");
        // scanf("%ld",&num_steps);
        for (num_steps = 1000000; num_steps <= 10000000; num_steps += 100000)
        {
            int index;
            index = ((double)num_steps - 1000000) / 100000 + 4;
            clock_t start_t, end_t;
            double total_t;
            int i;
            double x, pi, sum = 0.0;
            start_t = clock();
            step = 1.0 / (double)num_steps; //将1平分成100000步
            for (i = 1; i <= num_steps; i++)
            {
                x = (i - 0.5) * step; //获得0-1的连续值
                sum += 4.0 / (1.0 + x * x);
            }
            pi = step * sum; //
            end_t = clock();
            total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
            printf("运行时间为%fs\t%.*f\r\n", total_t, pi, index);
        }
        return 0;
    }
  • 输出结果

    运行时间为0.020000s    3.1416
    运行时间为0.020000s    3.14159
    运行时间为0.010000s    3.141593
    运行时间为0.020000s    3.1415927
    运行时间为0.020000s    3.14159265
    运行时间为0.030000s    3.141592654
    运行时间为0.020000s    3.1415926536
    运行时间为0.030000s    3.14159265359
    运行时间为0.020000s    3.141592653590
    运行时间为0.030000s    3.1415926535899
    运行时间为0.030000s    3.14159265358999
    运行时间为0.030000s    3.141592653589834
    运行时间为0.040000s    3.1415926535896115
    运行时间为0.030000s    3.14159265358955730
    运行时间为0.040000s    3.141592653589722062
    运行时间为0.040000s    3.1415926535897442662
    运行时间为0.030000s    3.14159265358982242589
    运行时间为0.040000s    3.141592653589698969085
    运行时间为0.050000s    3.1415926535897624738425
    运行时间为0.040000s    3.14159265358961103942192
    运行时间为0.050000s    3.141592653589609707154295
    运行时间为0.040000s    3.1415926535896288029903189
    运行时间为0.050000s    3.14159265358963946113135535
    运行时间为0.050000s    3.141592653589633687971627296
    运行时间为0.050000s    3.1415926535896367965960962465
    运行时间为0.050000s    3.14159265358973316395463371009
    运行时间为0.060000s    3.141592653589739825292781461030
    运行时间为0.060000s    3.1415926535896865345875994535163
    运行时间为0.050000s    3.14159265358964034930977504700422
    运行时间为0.060000s    3.141592653589700301353104805457406
    运行时间为0.060000s    3.1415926535896829818739206530153751
    运行时间为0.070000s    3.14159265358976158566406411409843713
    运行时间为0.060000s    3.141592653590072448110959157929755747
    运行时间为0.060000s    3.1415926535893832216572718607494607568
    运行时间为0.070000s    3.14159265358984018945420757518149912357
    运行时间为0.070000s    3.141592653589813544101616571424528956413

Last updated