gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:58:54 25 4
gpt4 key购买 nike

考虑以下代码片段:

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 digitsto be written on insertion operations to express floating-pointvalues. How this is interpreted depends on whether the floatfieldformat flag is set to a specific notation (either fixed or scientific)or it is unset (using the default notation, which is not necessarilyequivalent 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 intotal counting both those before and those after the decimalpoint. Notice that it is not a minimum, and therefore it does notpad the displayed number with trailing zeros if the number can bedisplayed 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 decimalpoint, even if this includes trailing decimal zeros. The digitsbefore the decimal point are not relevant for the precision in thiscase.

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com