簡體   English   中英

打印 pi 的值。 在您第一次獲得 3.14 之前,您必須使用這個系列的多少項? 3.141? 3.1415? 3.14159?

[英]Print Value of pi. How many terms of this series do you have to use before you first get 3.14? 3.141? 3.1415? 3.14159?

#include <stdio.h>
#include <math.h>

int main(void) {
    // Set the initial value of pi to 0
    double pi = 0.0;

    // Set the initial value of the term to 1
    double term = 1.0;

    // Set the initial value of the divisor to 1
    double divisor = 1.0;

    // Print the table header
    printf("%10s%25s\n", "Number of terms", "Approximation of pi");

    // Calculate and print the approximations of pi
    for (int i = 1; i <= 20; i++) {
        pi += term / divisor;
        printf("%10d%25.10f\n", i, pi*4.0);
        term *= -1.0;
        divisor += 2.0;
    }

    return 0;
}

我試圖更正代碼,但仍然無法接近我的老師在作業中要求的值......

問題是.. 從無限級數中計算 π 的值。 打印一張表格,顯示用該級數的一項、兩項、三項等近似的 π 值。 在您第一次獲得 3.14 之前,您必須使用這個系列的多少項? 3.141? 3.1415? 3.14159?

在您第一次獲得 3.14 之前,您必須使用這個系列的多少項? 3.141? 3.1415? 3.14159?

“first get 3.14”的細節有點不清楚。 下面嘗試類似於 OP 的目標,並說明了緩慢的收斂,因為計算時間與項數成正比。

大量的項,每一項都會在除法和加法中產生舍入誤差,最終導致此計算對於高項計數而言過於不准確。

int main(void) {
  double pi_true = 3.1415926535897932384626433832795;
  double threshold = 0.5;
  int dp = 0;

  // Set the initial value of pi to 0
  double pi = 0.0;

  // Set the initial value of the term to 1
  double term = 1.0;

  // Set the initial value of the divisor to 1
  double divisor = 1.0;

  // Print the table header
  printf("%7s %12s %-25.16f\n", "", "", pi_true);
  printf("%7s %12s %-25s\n", "", "# of terms", "Approximation of pi");

  // Calculate and print the approximations of pi
  for (long long i = 1; ; i++) {
    pi += term / divisor;
    double diff = fabs(4*pi - pi_true);
    if (diff <= threshold) {
      printf("%7.1e %12lld %-25.16f %-25.*f\n", diff, i, pi * 4.0, dp++, pi * 4.0);
      fflush(stdout);
      threshold /= 10;
      if (4*pi == pi_true) {
        break;
      }
    }
    term *= -1.0;
    divisor += 2.0;
  }
  puts("Done");
  return 0;
}


輸出

                     3.1415926535897931       
          # of terms Approximation of pi      
4.7e-01            2 2.6666666666666670        3                        
5.0e-02           20 3.0916238066678399        3.1                      
5.0e-03          200 3.1365926848388161        3.14                     
5.0e-04         2000 3.1410926536210413        3.141                    
5.0e-05        20000 3.1415426535898248        3.1415                   
5.0e-06       200001 3.1415976535647618        3.14160                  
5.0e-07      2000001 3.1415931535894743        3.141593                 
5.0e-08     19999992 3.1415926035897974        3.1415926                
5.0e-09    199984633 3.1415926585897931        3.14159266               
5.0e-10   1993125509 3.1415926540897927        3.141592654   
5.0e-11  19446391919 3.1415926536397927        3.1415926536 
...
Ref                  3.1415926535897931        


在第二次嘗試中,也許這更接近 OP 的目標

int main(void) {
  double pi_true = 3.1415926535897932384626433832795;

  double threshold_lo = 2.5;
  double threshold_hi = 3.5;
  double error_band = 0.5;
  int dp = 0;

  // Set the initial value of pi to 0
  double pi = 0.0;

  // Set the initial value of the term to 4
  double term = 4.0;

  // Set the initial value of the divisor to 1
  double divisor = 1.0;

  // Print the table header
  printf("%12s %-25.16f\n", "", pi_true);
  printf("%12s %-25s\n", "# of terms", "Approximation of pi");

  // Calculate and print the approximations of pi
  for (long long i = 1;; i++) {
    pi += term / divisor;
    if (pi > threshold_lo && pi < threshold_hi) {
      printf("%12lld %-25.16f %-25.*f\n", i, pi, dp++, pi);
      fflush(stdout);

      char buf[100] = "3.1415926535897932384626433832795";
      buf[dp + 2] = 0;
      error_band /= 10.0;
      double target = atof(buf);
      threshold_lo = target - error_band;
      threshold_hi = target + error_band;
    }
    term *= -1.0;
    divisor += 2.0;
  }
  puts("Done");
  return 0;
}

輸出

             3.1415926535897931       
  # of terms Approximation of pi      
           2 2.6666666666666670        3                        
          12 3.0584027659273332        3.1                      
         152 3.1350137774059244        3.14                     
         916 3.1405009508583017        3.141                    
        7010 3.1414500002381582        3.1415                   
      130658 3.1415850000208838        3.14159                  
      866860 3.1415915000009238        3.141592                 
     9653464 3.1415925500000141        3.1415926                
   116423306 3.1415926450000007        3.14159265               
   919102060 3.1415926525000004        3.141592653              
  7234029994 3.1415926534500005        3.1415926535  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM