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);
The flag is set with the following statement: cout.setf(ios::fixed); Once the flag has been set, the number you pass to setprecision( ) is the number of decimal places you want displayed.
std::fixed – Fixed Floating-point notation : It write floating-point values in fixed-point notation. The value is represented with exactly as many digits in the decimal part as specified by the precision field (precision) and with no exponent part.
It is used to set specific format flags. The format flags of a stream affect the way data is interpreted in certain input functions and how it is written by certain output functions.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With