Why use showpoint when you can use setprecision fixed?

user97662 picture user97662 · Feb 17, 2015 · Viewed 15.4k times · Source

I don't quite understand the purpose of showpoint, i know it forces to show a decimal point, but having "cout << setprecision << fixed" is enough without the use of showpoint.

Can you show me an example where showpoint is a must have?

Answer

Dietmar K&#252;hl picture Dietmar Kühl · Feb 17, 2015

When covering a large range of values it may be desirable to have the formatting logic to switch between the use of fixed point and scientific notation while still requiring a decimal point. If you look at the output of this example you'll see that it isn't just fixed point notation:

#include <iostream>

int main() {
    for (double value(1e15), divisor(1); divisor < 1e20; divisor *= 10) {
        std::cout << std::noshowpoint << (value / divisor) << '\t'
                  << std::showpoint << (value / divisor)
                  << '\n';
    }
}

This yields the output

1e+15   1.00000e+15
1e+14   1.00000e+14
1e+13   1.00000e+13
1e+12   1.00000e+12
1e+11   1.00000e+11
1e+10   1.00000e+10
1e+09   1.00000e+09
1e+08   1.00000e+08
1e+07   1.00000e+07
1e+06   1.00000e+06
100000  100000.
10000   10000.0
1000    1000.00
100 100.000
10  10.0000
1   1.00000
0.1 0.100000
0.01    0.0100000
0.001   0.00100000
0.0001  0.000100000