Unexpected difference in iostream behavior between visual studio 2010 and gcc / g ++ 4.6.3

Consider the following piece of code:

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;

      

Results:

0.00000000000000 // Visual studio 2010

0.0000000000000 // g ++

Is this difference acceptable, or is it a bug in one of the compilers?

+3


source to share


1 answer


The documentation for precision

reads (emphasis mine):

Floating point precision specifies the maximum number of digits to write insert operations for a floating point expression. How this is interpreted depends on whether the floatfield format flag is set to a specific notation (fixed or scientific) or not set (using standard notation, which is not necessarily equivalent to either fixed or scientific).

For the default locale:

  • Using standard floating point notation, the precision field specifies the maximum number of significant digits to display in total , counting both before and after the decimal point . Note that this is not a minimum, so enter the displayed number with trailing zeros if the number can be displayed with fewer digits than the precision.

  • In fixed and scientific notation, the precision field indicates exactly how many digits should be displayed after the decimal point , even if that includes trailing decimal zeros. The numbers before the decimal point are irrelevant for precision in this case.



It looks like the compilers use different flags.

+4


source







All Articles