Why does std::setprecision(6) stream more than six digits in fixed-width mode?

删除回忆录丶 提交于 2019-12-06 04:56:43

问题


The output of the following code:

#include <limits>
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <sstream>

using namespace std;

inline string lexical_cast(const float arg)
{
    stringstream ss;
    ss << fixed << setprecision(numeric_limits<float>::digits10) << arg;
    if (!ss)
        throw "Conversion failed";

    return ss.str();
}

int main()
{
    cout << numeric_limits<float>::digits10 << '\n';
    cout << lexical_cast(32.123456789) << '\n';
}

is:

6
32.123455

I expected, and wanted:

6
32.1234

because, to the best of my knowledge, that's the extent of what a float can reliably give me on my system.

How can I persuade IOStreams to behave as I want?


回答1:


In fixed-width mode the "precision" setting is used as the number of decimal places, in contrast with scientific mode where it is used as the number of significant digits. IOStreams provides no mechanism to use "precision" as the number of significant digits without using scientific mode.

There is a third mode, which in C++11 is activated with std::defaultfloat. This "default" mode is what you get if you don't set either fixed or scientific mode. You can re-activate it in C++03 by resetting the float flags with s.unsetf(std::ios_base::floatfield). This mode is a bit of a mix between scientific and some sort of "fixed without trailing zeros".



来源:https://stackoverflow.com/questions/22150400/why-does-stdsetprecision6-stream-more-than-six-digits-in-fixed-width-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!