Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only 2 digits in exponent in scientific ofstream

So according to cplusplus.com when you set the format flag of an output stream to scientific notation via

of.setf(ios::scientific)

you should see 3 digits plus and a sign in the exponent. However, I only seem to get 2 in my output. Any ideas? Compiled on Mac OS using GCC 4.0.1.

Here's the actual code I am using:

of.setf(ios::scientific);
of.precision(6);
for (int i=0;i<dims[0];++i) {
    for (int j=0;j<dims[1];++j) {
    of << setw(15) << data[i*dims[1]+j];                    
    }
    of << endl;
}

and an example line of output:

   1.015037e+00   1.015037e+00   1.395640e-06  -1.119544e-06  -8.333264e-07

Thanks

like image 875
Ian Buss Avatar asked Aug 10 '09 20:08

Ian Buss


2 Answers

I believe cplusplus.com is incorrect, or at least is documenting a particular implementation - I can't see any other online docs which specifically state the number of exponent digits which are displayed - I can't even find it in the C++ specification.

Edit:

The C++ Standard Library: A Tutorial and Reference doesn't explicitly state the number of exponent digits; but all it's examples display two exponent digits.

like image 124
DaveR Avatar answered Nov 15 '22 11:11

DaveR


It's implementation specific.

like image 23
Martin Beckett Avatar answered Nov 15 '22 13:11

Martin Beckett