私の学校は私に円周率を計算する課題を出します。
結果は次のようになります。
Question 4
Accuracy set at : 1000
term pi
1 4
100 3.13159
200 3.13659
300 3.13826
400 ...
... ...
私のプログラムの結果:
term pi
1 4
100 3
200 3
300 3
400 ...
... ...
(4 / 分母) を実行すると、データ型の宣言を int から double に変更しましたが、結果は 10 進数を失うと思います。(一部の Web サイトでは、これを行うように指示されています。) たぶん、私のやり方が間違っています。
どうすればこの問題に対処できますか?
以下は私のプログラムです。
#include <iostream>
using namespace std;
class Four
{
private:
int inputedAccuracy;
double pi;
int denominator;
int doneTermCounter;
double oneTerm;
int negativeController;
public:
double question4()
{
cout << "Accuracy set at : " ;
cin >> inputedAccuracy;
cout << endl;
pi = 0.0;
denominator = 1.0;
doneTermCounter = 0;
negativeController = 1;
cout << "Term" << " " << "pi" << endl;
cout << "1 " << " " << "4" << endl;
for (inputedAccuracy; inputedAccuracy > 0; inputedAccuracy -= 100)
{
for (int doneTerm = 0; doneTerm < 100; doneTerm++)
{
pi = pi + (negativeController * 4 / denominator);
negativeController *= -1;
denominator += 2;
doneTermCounter++;
}
if (doneTermCounter >= 10000)
cout << doneTermCounter << " " << pi << endl;
else
if (doneTermCounter >= 1000)
cout << doneTermCounter << " " << pi << endl;
else
cout << doneTermCounter << " " << pi << endl;
}
return 0.0;
}
};
ご清聴ありがとうございました!