Why does cout.setf(ios::fixed) change my floats to hexadecimal?

梦想的初衷 提交于 2019-12-31 01:11:10

问题


I experienced this weird issue recently having to do with cout.setf(ios::fixed). Took me quite a while to track down the cause and thought I'd ask here to learn more.

The issue is this - all floating point numbers were printed as hexadecimal numbers when using cout.setf(ios::fixed). Why does this happen? The documentation of ios::base doesn't seem to imply that this will happen (at least to me). I am using g++ 5.3.0 and pasted below is a minimal example and the output.

   #include <iostream>
   #include <complex>

   using namespace std;

   int main(int argc, char const *argv[])
   {
    complex<double> I(0.0, 1.0);
    double pi = M_PI;

    cout.setf(ios::scientific);
    cout<<" I is "<<I<<endl;
    cout<<" Exp(I Pi) "<<exp(I*pi)<<endl;
    cout<<" Cos(Pi) "<<cos(pi)<<endl<<endl;

    cout.setf(ios::fixed);
    cout<<" I is "<<I<<endl;
    cout<<" Exp(I Pi) "<<exp(I*pi)<<endl;
    cout<<" Cos(Pi) "<<cos(pi)<<endl<<endl;

    return 0;
   }

Output

 I is (0.000000e+00,1.000000e+00)
 Exp(I Pi) (-1.000000e+00,1.224647e-16)
 Cos(Pi) -1.000000e+00

 I is (0x0p+0,0x1p+0)
 Exp(I Pi) (-0x1p+0,0x1.1a62633145c07p-53)
 Cos(Pi) -0x1p+0

See the live sample here

Note that the issue goes away when I change

 cout.setf(ios::fixed);

to

 cout.setf(ios::fixed, ios::floatfield);

回答1:


Because you told it to.

From setf documentation on cppreference.com:

scientific - generate floating point types using scientific notation, or hex notation if combined with fixed: see std::scientific
fixed - generate floating point types using fixed notation, or hex notation if combined with scientific: see std::fixed

So, when setting std::fixed, you need to unset std::scientific (which is what your unmasking of std::floatfield does, because std::floatfield is std::scientific|std::fixed|(std::scientific|std::fixed)|0) to avoid the hex notation.



来源:https://stackoverflow.com/questions/36362077/why-does-cout-setfiosfixed-change-my-floats-to-hexadecimal

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