c++ - visual studio 2010 和 gcc/g++ 4.6.3 之间 iostream 行为的意外差异

标签 c++ visual-studio-2010 visual-studio gcc iostream

考虑以下代码片段:

ofstream o("myFile.txt");

o.precision(14);
o.width(20);
o.setf(ios::showpoint);
o.setf(ios::internal);
o.fill(' ');

double zero = 0.0;

o << zero;

结果是:

0.00000000000000// Visual Studio 2010

0.0000000000000//g++

这种差异是否可以接受,或者它是其中一个编译器中的错误?

最佳答案

precision 的文档阅读(强调我的):

The floating-point precision determines the maximum number of digits to be written on insertion operations to express floating-point values. How this is interpreted depends on whether the floatfield format flag is set to a specific notation (either fixed or scientific) or it is unset (using the default notation, which is not necessarily equivalent to either fixed nor scientific).

For the default locale:

  • Using the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display in total counting both those before and those after the decimal point. Notice that it is not a minimum, and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision.

  • In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The digits before the decimal point are not relevant for the precision in this case.

听起来编译器使用了不同的 format flags .

关于c++ - visual studio 2010 和 gcc/g++ 4.6.3 之间 iostream 行为的意外差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26147822/

相关文章:

c++ - 如何编译静态库? ("-static-lib..."等效?)

c++ - 预期的声明说明符或 '...' 之前的 'MLD_PACKET'

C# 错误 : Operator '==' cannot be applied to operands of type 'System.Windows.Forms.DialogResult' and 'bool'

windows - 驱动程序/代码签名

visual-studio - 调试时,调试按钮移至 Visual Studio 中的左侧

c++ - 我应该为 C++ 中的序列中的无重复数字选择哪个随机生成器

c++ - C++中异常的调用栈

c++ - 无法链接我自己的静态库

c# - .xaml.cs 文件中的 "Go To Definition"将我带到 .g.i.cs 文件而不是 Visual Studio 2015 中的 .xaml 文件

visual-studio - Visual Studio 中的 Apple ID 双重身份验证