小编vij*_*arv的帖子

为什么cout.setf(ios :: fixed)将我的浮点数改为十六进制?

我最近经历了这个与cout.setf(ios :: fixed)有关的奇怪问题.我花了很长时间来追查原因,并认为我会在这里要求了解更多信息.

问题是这样 - 当使用cout.setf(ios :: fixed)时,所有浮点数都打印为十六进制数字.为什么会这样?ios :: base的文档似乎并不意味着会发生这种情况(至少对我而言).我使用g ++ 5.3.0并在下面粘贴是一个最小的例子和输出.

   #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;
   }
Run Code Online (Sandbox Code Playgroud)

产量

 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
Run Code Online (Sandbox Code Playgroud)

See the live …

c++ cout

4
推荐指数
1
解决办法
407
查看次数

标签 统计

c++ ×1

cout ×1