C++ 对π;在C++;
我的任务是询问用户,与pi的实际值相比,他们希望求和迭代多少位小数精度。因此,当循环达到3.14时,小数点后2位将停止。我有一个完整的程序,但我不确定它是否真的按预期工作。我用计算器检查了小数点后0位和1位,它们似乎都能工作,但我不想假设它们都能工作。此外,我的代码可能有点笨拙,因为我们仍在学习基础知识。我们只是学习了循环和嵌套循环。如果有任何明显的错误或部分可以清理,我将感谢任何意见。 编辑:我只需要把这个作品保留到小数点后五位。这就是为什么我的pi值不精确的原因。对不起,误会了C++ 对π;在C++;,c++,loops,pi,C++,Loops,Pi,我的任务是询问用户,与pi的实际值相比,他们希望求和迭代多少位小数精度。因此,当循环达到3.14时,小数点后2位将停止。我有一个完整的程序,但我不确定它是否真的按预期工作。我用计算器检查了小数点后0位和1位,它们似乎都能工作,但我不想假设它们都能工作。此外,我的代码可能有点笨拙,因为我们仍在学习基础知识。我们只是学习了循环和嵌套循环。如果有任何明显的错误或部分可以清理,我将感谢任何意见。 编辑:我只需要把这个作品保留到小数点后五位。这就是为什么我的pi值不精确的原因。对不起,误会了 #inclu
#include <iostream>
#include <cmath>
using namespace std;
int main() {
const double PI = 3.141592;
int n, sign = 1;
double sum = 0,test,m;
cout << "This program determines how many iterations of the infinite series for\n"
"pi is needed to get with 'n' decimal places of the true value of pi.\n"
"How many decimal places of accuracy should there be?" << endl;
cin >> n;
double p = PI * pow(10.0, n);
p = static_cast<double>(static_cast<int>(p) / pow(10, n));
int counter = 0;
bool stop = false;
for (double i = 1;!stop;i = i+2) {
sum = sum + (1.0/ i) * sign;
sign = -sign;
counter++;
test = (4 * sum) * pow(10.0,n);
test = static_cast<double>(static_cast<int>(test) / pow(10, n));
if (test == p)
stop = true;
}
cout << "The series was iterated " << counter<< " times and reached the value of pi\nwithin "<< n << " decimal places." << endl;
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
常数双PI=3.141592;
int n,符号=1;
双和=0,测试,m;
cout n;
双p=PI*pow(10.0,n);
p=静态施法(静态施法(p)/功率(10,n));
int计数器=0;
bool-stop=false;
对于(双i=1;!停止;i=i+2){
总和=总和+(1.0/i)*符号;
符号=-符号;
计数器++;
试验=(4*总和)*功率(10.0,n);
测试=静态施法(静态施法(测试)/功率(10,n));
如果(测试==p)
停止=真;
}
cout您的程序永远不会终止,因为test==p
永远不会为真。这是两个计算不同的双精度数字之间的比较。由于舍入错误,即使您运行无限次迭代,它们也不会相同,并且您的数学是正确的(现在不是,因为程序中的PI
的值不准确)
为了帮助您了解发生了什么,请打印每次迭代中的test
值,以及test
和pi
之间的距离,如下所示:
#include<iostream>
using namespace std;
void main() {
double pi = atan(1.0) * 4; // Make sure you have a precise value of PI
double sign = 1.0, sum = 0.0;
for (int i = 1; i < 1000; i += 2) {
sum = sum + (1.0 / i) * sign;
sign = -sign;
double test = 4 * sum;
cout << test << " " << fabs(test - pi) << "\n";
}
}
莱布尼兹求和的一个问题是,它的收敛速度极低,因为它表现出次线性收敛。在你的程序中,你还将计算出的π的外推与给定值(6位数近似值)进行比较,而求和的目的应该是找出正确的数字
如果想要的数字在迭代之间没有变化,您可以稍微修改代码,使其终止计算(我还添加了最大迭代次数检查)。请记住,您使用的不是无限精度的数字,舍入误差迟早会影响计算。事实上,此代码的真正限制是迭代次数(2428700925获得3.141592653
)
你的程序是否按预期运行?这是我的问题。我想是的,但我不确定如何检查精度何时为小数点后4位或5位。我曾尝试在每次迭代后打印出总和的值,但我的计算机却死机了。有趣的是,只需添加cout
for (int i=1; fabs(test-pi)>epsilon; i+=2)
#include <iostream>
#include <cmath>
#include <iomanip>
using std::cout;
// this will take a long long time...
const unsigned long long int MAX_ITER = 100000000000;
int main() {
int n;
cout << "This program determines how many iterations of the infinite series for\n"
"pi is needed to get with 'n' decimal places of the true value of pi.\n"
"How many decimal places of accuracy should there be?\n";
std::cin >> n;
// precalculate some values
double factor = pow(10.0,n);
double inv_factor = 1.0 / factor;
double quad_factor = 4.0 * factor;
long long int test = 0, old_test = 0, sign = 1;
unsigned long long int count = 0;
double sum = 0;
for ( long long int i = 1; count < MAX_ITER; i += 2 ) {
sum += 1.0 / (i * sign);
sign = -sign;
old_test = test;
test = static_cast<long long int>(sum * quad_factor);
++count;
// perform the test on integer values
if ( test == old_test ) {
cout << "Reached the value of Pi within "<< n << " decimal places.\n";
break;
}
}
double pi_leibniz = static_cast<double>(inv_factor * test);
cout << "Pi = " << std::setprecision(n+1) << pi_leibniz << '\n';
cout << "The series was iterated " << count << " times\n";
return 0;
}
digits Pi iterations
---------------------------------------
0 3 8
1 3.1 26
2 3.14 628
3 3.141 2,455
4 3.1415 136,121
5 3.14159 376,848
6 3.141592 2,886,751
7 3.1415926 21,547,007
8 3.14159265 278,609,764
9 3.141592653 2,428,700,925
10 3.1415926535 87,312,058,383