Question

I'm learning the setw and setprecision functions, so here is what I've been trying so far and I have a few questions.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    float y = 1.25;

    cout << fixed << setw(10) << setprecision(2) << y << endl;

    cout << "\n\n\nx\n";

    float x = 213565544.4826;
    cout << fixed << setw(13) << setprecision(3) << x << endl;
    cout << fixed << setw(14) << setprecision(3) << x << endl;
    cout << fixed << setw(15) << setprecision(3) << x << endl;
    cout << fixed << setprecision(3) << x;

    cout << "\n\n\nz\n";

    float z = 213565544.4826;
    cout << setw(11) << setprecision(1) << x << endl;
    cout << fixed << setw(12) << setprecision(1) << x << endl;
    cout << fixed << setw(11) << setprecision(1) << x << endl;
    cout << setw(12) << setprecision(1) << x << endl;

    cout << "\n\n\nm\n";

    float m = -344.275;
    cout << fixed << setprecision(1) << x << endl;
    cout << fixed << setw(8) << setprecision(1) << x << endl;
    cout << fixed << setw(7) << setprecision(1) << x << endl;
    cout << fixed << setw(6) << setprecision(1) << x << endl;

    return 0;
}

And the input is :

      1.25



x
213565552.000
 213565552.000
  213565552.000
213565552.000


z
213565552.0
 213565552.0
213565552.0
 213565552.0



m
213565552.0
213565552.0
213565552.0
213565552.0

So, now my questions are :

1) Why do we use "fixed" in first place?

If we look at this example:

cout << setw(11) << setprecision(1) << x << endl;
cout << fixed << setw(11) << setprecision(1) << x << endl;

They output the same value, so what does fixed really change?

2) How does setw work for negative numbers?

In the last example of m. The result is the same for all examples, what does the - sign change in the setw ?

213565552.0
213565552.0
213565552.0
213565552.0

Where do these numbers come from? The value of m is totally different from the ones outputted.

3) Does the . in the number counts as 1 place?

For example, we have number 1.23 and setw(10)

There would be 6 spaces before and then 1.23 (because the dot is counted as 1). Is that true?

4) Why does setprecision is used along with setw? Why does 0000s appear if it's not used? Does it appear as many 0s as the float can handle?

5) Why is the value for x

213565552.000
 213565552.000
  213565552.000
213565552.000

If x = 213565544.4826.

Where does the numbers 44.4826 get lost?

Was it helpful?

Solution

These seem to be 5 questions rather than one. Anyway:

  1. 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 use x.yyyyyyEee (you can ask to always use scientific format using std::scientific).
  2. std::setw() doesn't care what value is formatted! When a value is formatted and there is a positive out.width() set, the output will be padded with out.fill() character to be at least out.width() characters wide. If the output is bigger than out.width() anyway, no padding will occur. After each output operation [which takes out.width() into account] the out.width() is reset to 0 (all other formatting options are not automatically reset).
  3. 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 (for std::fixed formatting) or the number of non-exponent digits (for std::scientific formatting).
  4. The width is how many characters will be filled by the output, the precision specifies how many [fractional] digits are to be produced.
  5. 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 using std::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 to std:numeric_limits<float>::digits places). You might want to have a look at What Every Computer Scientist Should Know About Floating-Point Arithmetic.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top