问题
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: seestd::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