Ich hatte dieses seltsame Problem vor kurzem mit cout.setf (ios :: fixed) zu tun. Es dauerte eine ganze Weile, bis ich die Sache aufgespürt hatte und dachte, ich würde hier nach weiteren Informationen fragen.Warum ändert cout.setf (ios :: fixed) meine Floats in hexadezimale?
Das Problem ist das - alle Gleitkommazahlen wurden als Hexadezimalzahlen gedruckt, wenn Sie cout.setf (ios :: fixed) verwenden. Warum passiert das? Die Dokumentation von ios :: base scheint nicht zu implizieren, dass dies (zumindest für mich) passieren wird. 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;
}
Ausgabe
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
Beachten Sie, dass das Problem weggeht, wenn ich
cout.setf(ios::fixed);
zu
cout.setf(ios::fixed, ios::floatfield);
ändern
Blitz schlägt wieder: D, +1 obwohl :) –
Was für _reading_! –
Danke! Peinlicher Fehler. – vijayshankarv