3

I understand what the fixed and showpoint manipulators do on their own, but why would you want to use them both at the same time? This is what DS Malik's C++ Programming textbook says:

Of course, the following statement sets the output of a floating-point number in a fixed decimal format with the decimal point and trailing zeros on the standard output device:
cout << fixed << showpoint;

But doesn't fixed alone set the output to be "fixed decimal format with the decimal point and trailing zeros"?

I tried running my own experiment:

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    cout << setprecision(3);
    cout << "only fixed" << "\t" << "fixed + showpoint" << endl;
    for (double i = 0.000001; i <= 100000; i *= 10) {
        cout << fixed     << i << "\t\t";
        cout << showpoint << i << endl;
        cout.unsetf(ios::fixed);     
        cout.unsetf(ios::showpoint);
    }
}

But I fail to see the difference between using only fixed and using fixed and showpoint together, in that order:

only fixed      fixed + showpoint
0.000           0.000
0.000           0.000
0.000           0.000
0.001           0.001
0.010           0.010
0.100           0.100
1.000           1.000
10.000          10.000
100.000         100.000
1000.000                1000.000
10000.000               10000.000
100000.000              100000.000

Any insight would be greatly appreciated!

ewzuhnaem
  • 33
  • 1
  • 5
  • You're asking about the interaction between `fixed` and `showpoint`, but your code also contains `setw`, which adds complexity to the issue. Take a look at https://stackoverflow.com/questions/28552005/why-use-showpoint-when-you-can-use-setprecision-fixed, this might answer your question. – cigien Oct 31 '20 at 14:24
  • I updated the question to include the same example without ```setw```. As for the question that you have referred to, I did read it and wanted to know if there were any other differences in behaviour except for the case of 0 precision. – ewzuhnaem Oct 31 '20 at 15:06
  • 1
    This is better, but now it seems you could remove the first half of the question with the `setw` part. – cigien Oct 31 '20 at 15:20
  • Yeah, you're right, I should do that. – ewzuhnaem Oct 31 '20 at 15:23
  • Even better :) Now I'm wondering about the line "*But I fail to see the difference between...*". Doesn't the output show there's a difference? Perhaps you mean, "Can someone explain the difference in output"? – cigien Oct 31 '20 at 15:33
  • That's the thing - there is no difference between the two columns. I meant to say that maybe it is my experiment fails to show the difference. – ewzuhnaem Oct 31 '20 at 15:40
  • Ok, the alignment mismatch makes that a bit confusing though. It *looks* like a difference in output. – cigien Oct 31 '20 at 15:44

2 Answers2

4

With fixed you set a number of decimals that you want to see in all cases. So you will always see the decimal point. Combining with showpoint makes no difference, unless...

Unless... You setprecision(0), in which case you'll never see a decimal and hence no decimal point. In this case showpoint will have the effect of showing a trailing decimal separator with nothing behind:

               0                  0.
               1                  1.
              10                 10.
             100                100.

Now, is this combination useful? I doubt it makes sense. So, no, there is no reason to combine those two.

Christophe
  • 68,716
  • 7
  • 72
  • 138
2

In your case, the showpoint flag is no effects. Since all printouts had 3 digits after dicimal point, and had decimal points shown. But in some cases, these two flags can give different results. For exmaple, if you set precision(0) in you code. The decimal point will not shown in the case of fixed.

    cout << setprecision(0);

print out:

      only fixed   fixed + showpoint
               0                  0.
               0                  0.
               0                  0.
               0                  0.
               0                  0.
               0                  0.
               1                  1.
              10                 10.
             100                100.
            1000               1000.
           10000              10000.
          100000             100000.
ytlu
  • 412
  • 4
  • 9