如何写出PI的近似值?

时间:2017-10-01 18:23:27

标签: c++

代码可以正常工作,但只要我在其中放入2或更高的值,结果就会越来越大

#include <iostream>
using namespace std;

int    main()     {
    double pi =   0;
    long i;
    long n;
    cout << "Enter the value of n: ";
    cin >> n;
    cout << endl;

    for (i = 0; i < n; i++)
                {
    if (i % 2 == 0)
    pi = pi + (1 / (2 * i + 1));}
     else
    pi = pi  - (1 / (2 * i + 1));}
     pi = 4 * pi;

}  


    cout << endl << "pi = " << pi << endl;
    return 0;
    }

4 个答案:

答案 0 :(得分:0)

因为你的{和}错了。我认为括号将如下所示

如果公式为PI = 4/1 - 4/3 + 4/5 - 4/7 + ...(莱布尼兹系列),那么您可以按以下方式进行格式化

#include <iostream>
using namespace std;


int main()     {

    double n, i;       // Number of iterations and control variable
    double s = 1;      //Signal for the next iteration
    double pi = 0.0;

    cout << "Enter the value of n: ";
    cin >> n;
    cout << endl;

   cout << "Approximation of the number PI through the Leibniz's series\n";     

   for(i = 1; i <= (n * 2); i += 2){
     pi = pi + s * (4 / i);
     s = -s;
       cout << "Step (" << (i-1)/2 << "):" << pi << endl;
   }

    cout << endl << "pi = " << pi << endl;
    return 0;
}

然后结果就好像n = 13

Approximation of the number PI through the Leibniz's series
Step (0):4
Step (1):2.66667
Step (2):3.46667
Step (3):2.89524
Step (4):3.33968
Step (5):2.97605
Step (6):3.28374
Step (7):3.01707
Step (8):3.25237
Step (9):3.04184
Step (10):3.23232
Step (11):3.0584
Step (12):3.2184

pi = 3.2184

答案 1 :(得分:0)

对于1574年John Wallis在欧洲发现的Wallis系列(PI = 2/1 x 2/3 x 4/3 x 4/5 x ....),代码将为

#include <iostream>
using namespace std;

int main()
{

   double n, i = 0 ;         // Number of iterations and control variable
   double pi = 4.;

   cout << "Approximation of the number pi through the Wallis's series\n";
   cin >> n;
   cout << endl;    

    for(i = 3; i <= (n + 2); i+=2)   { 
      pi = pi * ((i - 1) / i) * (( i + 1) / i);
        cout << "Step(" << (i-3)/2 << "):" << pi << endl;
    }

   cout << "\nAproximated value of PI = " << pi << endl; 
}

然后结果将是

Approximation of the number pi through the Wallis's series

Step(0):3.55556
Step(1):3.41333
Step(2):3.34367
Step(3):3.30239
Step(4):3.2751
Step(5):3.25572
Step(6):3.24125
Step(7):3.23004
Step(8):3.22109
Step(9):3.21378
Step(10):3.20771
Step(11):3.20258
Step(12):3.19818
Step(13):3.19438
Step(14):3.19106
Step(15):3.18813
Step(16):3.18552
Step(17):3.1832
Step(18):3.1811
Step(19):3.17921

Aproximated value of PI = 3.17921 

答案 2 :(得分:0)

对于Nilakantha的系列PI = 3 + 4 /(2x3x4) - 4 /(4x5x6)+ 4 /(6x7x8) - ...然后代码将被给出

#include <iostream>
using namespace std;

int main()
{

   double n, i;    // Number of iterations and control variable
   double s = 1;   //Signal for the next operation
   double pi = 3;

   cout << "Approximation of the number PI through the sequence of the Nilakantha's series\n" ;
   cin >> n;
   cout << endl;    

   for(i = 2; i <= n*2; i += 2){
     pi = pi +  s * (4 / (i * (i + 1) * (i + 2)));
     s = -s;
     cout << "Step(" << (i-2)/2 << "):" << pi << endl;
   }
   cout << "\nAproximated value of PI = " << pi << endl; 
}

然后结果将是

Approximation of the number PI through the sequence of the Nilakantha's series

Step(0):3.16667
Step(1):3.13333
Step(2):3.14524
Step(3):3.13968
Step(4):3.14271
Step(5):3.14088
Step(6):3.14207
Step(7):3.14125
Step(8):3.14184
Step(9):3.14141
Step(10):3.14174
Step(11):3.14148
Step(12):3.14168
Step(13):3.14152
Step(14):3.14165
Step(15):3.14154
Step(16):3.14164
Step(17):3.14156
Step(18):3.14162
Step(19):3.14157

Aproximated value of PI = 3.14157

答案 3 :(得分:0)

莱布尼兹系列的另一个实现:

double pi = 0.0;
double first_denominator = 1.0;
double second_denominator = 3.0;
for (i = 0; i < N; ++i)
{
  pi += 4.0 / first_denominator;
  pi -= 4.0 / second_denominator;
  first_denominator += 2;
  second_denominator += 2;
}

通过成对添加术语,删除了切换标志的需要。