C++ 如何写出PI的近似值?
代码可以工作,但每当我在代码中输入2或更高的值时,结果就会变得越来越大C++ 如何写出PI的近似值?,c++,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 =
#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;
}
#包括
使用名称空间std;
int main(){
双pi=0;
龙我;
长n;
cout>n;
cout因为你的{和}是错的。我认为括号如下
#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;
}
如果公式是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;
}
对于约翰·沃利斯1655年在欧洲发现的沃利斯系列(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 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
对于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;
}
莱布尼茨系列的另一个实现:
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;
}
double pi=0.0;
双第一分母=1.0;
双秒分母=3.0;
对于(i=0;i
通过成对添加术语,可以消除切换符号的需要。请检查答案。您的代码是PI值的Leibniz级数近似值。PI的所有值都是近似值。
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;
}