4

私は、double 型の多くの数値 (小数点の右側にわずか数桁の数百万と数十億の値) を持つ C++ プログラムに取り組んでいます。これらの数値に対して計算を実行し、結果をテキスト/CSV ファイルに出力しています。テキスト ファイルで、すべての数値が (6 桁に) 丸められているように見えることに気付きました。したがって、13,169,911 という値は、出力ファイルでは 13,169,900 として表示されます。

この丸めは印刷物でのみ発生しますか? 変数の全桁数を取得するには、ファイルに書き込むときに何かを指定する必要がありますか? 以下に、ファイルへの書き込みコードのサンプルを含めました。

void PrintPropFinance(vector<PropFinance>& PF, int NumProps, int Iterations, int ForecastLength, 
          string CurDeal, string ModelRunID, string ScenName, Assumptions& Ass) {

string filename;
ofstream OutFile;
ostringstream s1;

s1 << BASEPATH << "Output/" << CurDeal << "_" << ModelRunID << "_" << 
            ScenName << "_PropFinance" << ".csv";
filename = s1.str();

OutFile.open(filename);

// Put in the column headers first
OutFile << "PropID" << "," 
        << "Item" << ","
        << "StartDate" << ","
        << "NumOfPeriod" << ","
        << "Result" << ","
        << "Isap" << ","
        << "CurLoanBal" << ","

for (int i=1; i<=NumProps; ++i) {
    // Populate the single-vector variables
    OutFile << PF[i].PropID << "," 
            << PF[i].Item << ","
            << PF[i].StartDate << ","
            << PF[i].NumOfPeriod << ","
            << PF[i].Result << ","
            << PF[i].Isap << ","
            << PF[i].CurLoanBal << ","
                                    << endl;
}
OutFile.close();
}

// Prop finance class definition
class PropFinance {
public:
    string PropID;
    int Item;
    string StartDate;

    int NumOfPeriod;
    string Isap;
    double CurLoanBal;
}
4

2 に答える 2

5

doubleこの問題は、出力ストリームがsの出力を生成する方法に関係している可能性があります。 13169911「科学表記法」で出力されると、 のようになります1.31699E7。Excel はこの表記を問題なく読み取りますが、「表示」されない桁にはゼロを挿入し、数値が のように見えるようにし13,169,900ます。

この問題を解決するfixedには、出力時にマニピュレータを追加doubleして、すべての数字が出力されるようにします。

OutFile << PF[i].PropID << "," 
        << PF[i].Item << ","
        << PF[i].StartDate << ","
        << PF[i].NumOfPeriod << ","
        << fixed << PF[i].Result << ","
        << PF[i].Isap << ","
        << fixed << PF[i].CurLoanBal << ","
        << endl;
于 2013-08-08T15:58:46.617 に答える
4

std::setprecisionストリームの精度を上げるために使用する必要があります。デフォルトでは、 aniostreamの精度は 6 桁のみです。

これを試して:

OutFile << std::setprecision(std::numeric_limits<long double>::digits10 << PF[i].CurLoanBal;

これは、ストリームでの後続のすべての操作に影響することに注意してください。正直なところ、それはおそらくあなたが望んでいることです!

std::setprecisionとの比較としてstd::fixed、このプログラム:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <limits>
int main()
{
    const long double test_value = 13169911.7777777;
    std::cout << "default precision (6):              " << test_value << '\n'
              << "std::fixed:                         " << std::fixed << test_value << '\n'
              << "std::precision(10):                 " << std::defaultfloat << std::setprecision(10) << test_value << '\n'
              << "std::precision(10) & std::fixed:    " << std::fixed        << std::setprecision(10) << test_value << '\n'
              << "max precision:                      " << std::defaultfloat << std::setprecision(std::numeric_limits<long double>::digits10) << test_value << '\n'
              << "max precision & std::fixed:         " << std::fixed        << std::setprecision(std::numeric_limits<long double>::digits10) << test_value << '\n'
    ;
}

次の出力が生成されます。

default precision (6):              1.31699e+007
std::fixed:                         13169911.777778
std::precision(10):                 13169911.78
std::precision(10) & std::fixed:    13169911.7777777000
max precision:                      13169911.7777777
max precision & std::fixed:         13169911.777777700000000

std::setprecisionだから私はあなたがむしろ望むかもしれないと思いますstd::fixed。とにかく小数点以下2桁しかないと思いますが、おそらくそれは問題ではありません.

詳細はこちら: http://en.cppreference.com/w/cpp/io/manip/setprecision

于 2013-08-08T16:02:05.333 に答える