Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

cout

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);
like image 531
vijayshankarv Avatar asked Apr 01 '16 17:04

vijayshankarv


People also ask

What does cout Setf ios :: fixed mean?

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.

What is std:: fixed c++?

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.

What is setf () function in C++?

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.


1 Answers

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.

like image 82
Lightness Races in Orbit Avatar answered Nov 14 '22 21:11

Lightness Races in Orbit