I have a simple C++ program that I thought would print out a double value defined in f and g as doubles ... but C++ is printing them out as integers. I checked the default
This happens because of the distinction between the default notation and the fixed notation.
According to documentation,
- 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 (emphasis is added).
The stream uses the default floating-point notation until (or unless) you switch it to fixed or scientific notation. Since your number has six digit before the decimal point, and the default precision is also set to six, the floating-point numbers look like integers.
There are three floating-point notations:
The default notation just uses the precision value to count all digits of the number, not just the decimal part. If you use a short number (like 3.1415) you can see that the number is displayed correctly.
It's also interesting to know, as cplusplus.com documentation cites, that:
The default notation can be selected by calling str.unsetf(ios_base::floatfield).
P.S.: for C++11 there are new notations: hexfloat for hexadecimal and defaultfloat for the so called (none) before.