円周率を計算するプログラムを作成しました。ただし、1,000万回の反復を行っても、私の結果は少しずれています。私は3.1415927535897831を取得しますが、すでに早い段階でそれは間違っています。3.141592653589793238 ..。_
だから私の質問は:少なくとも10^-16までの正確な答えを得るために必要な反復の量は何ですか
誰かが興味を持っているなら、これが私のコードです:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
long double pi = 4.0;
long double tempPi;
for (int i = 1, j = 3; i <= 10000000; i++, j+=2)
{
tempPi = static_cast<double>(4)/j;
if (i%2 != 0)
{
pi -= tempPi;
}
else if (i%2 == 0)
{
pi += tempPi;
}
}
cout << "Pi has the value of: " << setprecision(16) << fixed << pi << endl;
system("pause");
return 0;
}
パフォーマンス関連のヒントもいただければ幸いです。