Re: controlling float printed exponent - gcc vs vc
Am 22.10.2010 16:30, schrieb shahav:
Hi All,
consider
#include<iostream>
int main() {
double num=1e-8;
std::cout<<"1e-8==__"<<num<<"__\n";
return 0;
}
on gcc linux the result is:
1e-8==__1e-08__
on vc, xp, the result is:
1e-8==__1e-008__
How can i control the printed exponent length? locale?
Whats get it even more odd, is that on our real project, we get
different results: cygwin react as linux while wine reacts as native
windows...
This is a very unfortunate behavior, because the output
format should be well-defined in this case. Based on the
initial value of std::cout.flags(), the effective stdio format
string used for the output here is "%g" (see Table 89 ?
basic_ios::init() effects in sub-clause [lib.basic.ios.cons]
of C++03 and the rules defined in [lib.facet.num.put.virtuals])
and the meaning of this is clearly defined in C [I have only
the last corrigendum of the C99 standard available to check
that, so anyone who has a proper C standard relevant for C++03
is invited to check that this is the same]: 7.19.6.1 says
that %g will effectively be %e for 1e-8 (In the test case,
the precision is omitted) and here the wording says:
"The exponent always contains at least two digits, and only
as many more digits as necessary to represent the exponent."
This means, gcc does it correctly in the example above, but
vc doesn't.
I feel miserable to recommend it, but if you cannot convince
your supported vendors to fix that, you should consider to
use the stdio library directly. In similar situations I
defined an IO manipulator and just implemented it's behavior
via the C library, but through the manipulator the C++ IO library
could be used normally. In the situation I was referring to,
I needed a so-called "engineering" format that outputs the
floating point values with exponents that are always multiples
of three (most table calculators provide this format), so the
work was necessary anyway.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]