These seem to be 5 questions rather than one. Anyway:
std::fixed
is used to indicate that you always want to have a fixed point format rather than using scientific notation where this notation is more appropriate. When there are many digits needed to represent the value reasonably, the format will switch usex.yyyyyyEee
(you can ask to always use scientific format usingstd::scientific
).std::setw()
doesn't care what value is formatted! When a value is formatted and there is a positiveout.width()
set, the output will be padded without.fill()
character to be at leastout.width()
characters wide. If the output is bigger thanout.width()
anyway, no padding will occur. After each output operation [which takesout.width()
into account] theout.width()
is reset to0
(all other formatting options are not automatically reset).- Any character counts towards the width, including the sign, thousands separators, decimal points, etc. The decimal point does not count towards the precision:
out.precision()
is the number of fractional digits (forstd::fixed
formatting) or the number of non-exponent digits (forstd::scientific
formatting). - The width is how many characters will be filled by the output, the precision specifies how many [fractional] digits are to be produced.
- Binary floating point values can represent very few decimal digits (for
float
it is normally 6; you can find out how many digits can be safely used by usingstd::numeric_limits<float>::digits10
). Trying to use any more digits than that will probably result in unexpected output when processing decimal values (when processing binary values you may be interested in up tostd:numeric_limits<float>::digits
places). You might want to have a look at What Every Computer Scientist Should Know About Floating-Point Arithmetic.