Re: printing double by using cout
On Mar 23, 5:01 pm, Vaclav Haisman <v.hais...@sh.cvut.cz> wrote:
James Kanze wrote, On 23.3.2009 10:35:>[...]
Anyway, something like:
std::cout.precision( 22 ) ;
Maybe something like
std::cout.precision(std::numeric_limits<double>::digits10); would be bett=
er.
For some definitions of better, probably. I chose 22 for the
simple reason that his examples had 22 decimal digits. Later in
my post, I did explain that the final digits didn't mean
anything.
Note that there are several definitions of precision which could
apply. On a machine with IEEE floating point, a double has 52
binary digits precision---that's roughly 15.65 decimal digits.
If you want to ensure that every decimal value can be converted
to a double and back without change, then the maximum precision
if 15. If your concerned about the opposite direction: that
every double can be converted to a decimal, then back to a
double without loss of value, then you need at least 17 digits
(dixit ISO/IEC 9899::1999). Without knowing exactly what he's
doing, it's difficult to say what is appropriate---if he's
trying to transmit data without loss in a text format, using as
few characters as possible, he needs a precision of
17---std::numeric_limits<double>::digits10 should be 15
(supposing IEEE in both case). The next version of the standard
will also provide max_digits10, which should be the number
required for this use. (And yes, the names stink.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34