Warum ändert cout.setf (ios :: fixed) meine Gleitkommazahlen in hexadezimal?
Ich habe dieses seltsame Problem kürzlich mit cout.setf (ios :: fixed) erlebt. Ich habe eine ganze Weile gebraucht, um die Ursache aufzuspüren, und dachte, ich möchte hier mehr erfahren.
Das Problem ist: Alle Gleitkommazahlen wurden bei Verwendung von cout.setf (ios :: fixed) als Hexadezimalzahlen gedruckt. Warum passiert das? Die Dokumentation von ios :: base scheint nicht zu implizieren, dass dies passieren wird (zumindest für mich). Ich benutze g ++ 5.3.0 und unten eingefügt ist ein minimales Beispiel und die Ausgabe.
#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;
}
Ausgab
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
Bitte beachten Sie, dass das Problem verschwindet, wenn ich mich ändere
cout.setf(ios::fixed);
z
cout.setf(ios::fixed, ios::floatfield);