こんにちは、私はC++の新機能です 2番目の慣性モーメントを計算し、小数点以下3桁で精度を設定する関数を記述しようとしています。 出力には最初のコールで小数点以下3桁は適用されませんが、次の4つのコールが適用されます。ここで私のコードは、私がエラーを見つけるのを助けてください可能であれば、いくつかの詳細を説明してください非常にありがとう!浮動小数点数の精度を設定する
double beamMoment(double b, double h) //the function that calculating the second moment of inertia
{
double I; //variables b=base, h=height, I= second moment of inertia
I = b * (pow(h, 3.0)/12); // formular of the second momeent of inertia
ofs << "b=" << b << "," << "h=" << h << "," << "I=" << I << setprecision(3) << fixed << endl;
ofs << endl;
return I;
}
int main()
{
beamMoment(10,100);
beamMoment(33, 66);
beamMoment(44, 88);
beamMoment(26, 51);
beamMoment(7, 19);
system("pause");
return 0;
}
私のテキストファイルで出力は以下の通りです:
b=10,h=100,I=833333
b=33.000,h=66.000,I=790614.000
b=44.000,h=88.000,I=2498730.667
b=26.000,h=51.000,I=287410.500
b=7.000,h=19.000,I=4001.083
試してみましょう "ofs << setprecision(3)<< fixed <<" b = "<< b <<"、 "<<" h = "<< h <<"、 "<<" I = "<
ああ私はそれを得た。ありがとうございました –