whatdoineed...@yahoo.co.uk wrote:
#include <iostream>
using namespace std;
void convert(const double& d)
{
cout << "before=" << d << " after=";
cout.precision(15);
}
int main()
{
convert(0.1234567890123456789);
convert(1.06144e-305);
return 0;
}
i'm using the above code to demonstrate a problem we've got. the
first call to convert does what i expect, however the second call
doesnt -- it just looks like it didnt even bother. below is the
ouptput:
before=0.123457 after=0.123456789012346
before=1.06144e-305 after=1.06144e-305
That's what you asked for:-). C++ formatting is defined in
terms of C's printf format specifiers: by default, floating
point values are formatted as "%g" (or more correctly, as
"%.<prec>g", where <precis the specified precision). And
according to the C standard, the "%g" format does not output
trailing 0's, regardless. If you output in scientific or fixed
format (corresponding to "%e" or "%f"), you should see them (but
I would not recommend fixed format for numbers like the second,
above). Otherwise (and you're going to love this), set the
showpoint formatting flag, e.g.:
std::cout.setf( std::ios::showpoint ) ;
The reason (for the name) is simple: C++ formatting is defined
in terms of C's printf, and the showpoint flag corresponds to
the '#' flag in printf: use alternative format. In the case of
"%#.0f", this means to output the point, whence the name. In
the case of "%#.<prec>g", it means to output the point AND any
trailing 0's. (Try outputting something like 12.0, and you'll
see the difference immediately: "12" without showpoint,
"12.0000" with, using the default precision of 6.)
--
James Kanze (GABI Software) mailto:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34