printing double by using cout

  • Thread starter Alexander Dong Back Kim
  • Start date
A

Alexander Dong Back Kim

Dear all members,

I know this question might sound "beginner" but I can't find any handy
way of achieving "dynamic length of double precision ouput".

Let me show my example first...

double a = 1.123456789012334567890;
double b = 1.000000000000000000001;
double c = 1.123450000012345000000;

There are 3 different double values as you can see above. I wonder is
there any way that I can build strings of the double values without
unnecessary "0" at the end of the values.

the strings should be "1.1234567890123456789" for a,
"1.000000000000000000001" for B and "1.123450000012345" for C.

I tried to use io manipulators such as scientific, fixed and
setprecision but found out that none of them can achieve what I'm
trying to do here.

Any suggestions or hints will be much much appreciated!

Regards,
Alex
 
J

James Kanze

I know this question might sound "beginner" but I can't find
any handy way of achieving "dynamic length of double precision
ouput".

The length is always dynamic:). All you can do is set a
minimum.
Let me show my example first...
double a = 1.123456789012334567890;
double b = 1.000000000000000000001;
double c = 1.123450000012345000000;
There are 3 different double values as you can see above. I
wonder is there any way that I can build strings of the double
values without unnecessary "0" at the end of the values.

That's the default. If the output formatting has been changed,
you can restore it with something like:

std::cout.setf( std::ios::fmtflags(), std::ios::floatfield ) ;
the strings should be "1.1234567890123456789" for a,
"1.000000000000000000001" for B and "1.123450000012345" for C.

I doubt that that's possible. At least on my machine,
"1.000000000000000000001" isn't representable; the closest
representable value is "1". Neither of the other values is
representable exactly either.
I tried to use io manipulators such as scientific, fixed and
setprecision but found out that none of them can achieve what
I'm trying to do here.

I'm not too sure that what you're trying to do makes sense.
None of the values you give are actually representable in a
double, at least on most current architectures, so you're
starting with a slightly different value. And except for values
which are exactly representable, who knows where the 0's start
on these values.

Anyway, something like:
std::cout.precision( 22 ) ;
std::cout.setf( std::ios::fmtflags(), std::ios::floatfield ) ;
std::cout << " |" << a << "|\n" ;
std::cout << " |" << b << "|\n" ;
std::cout << " |" << c << "|\n" ;
gives:
|1.123456789012334589017|
|1|
|1.123450000012345073586|
for the definitions you give. The final digits in the first and
third don't mean anything, however; typically, the precision of
a double is only around 15 digits.
 
A

Alex Kim

The length is always dynamic:).  All you can do is set a
minimum.


That's the default.  If the output formatting has been changed,
you can restore it with something like:

    std::cout.setf( std::ios::fmtflags(), std::ios::floatfield ) ;

That's what I was trying to fine!!! Thanks!!! =)
I doubt that that's possible.  At least on my machine,
"1.000000000000000000001" isn't representable; the closest
representable value is "1".  Neither of the other values is
representable exactly either.


I'm not too sure that what you're trying to do makes sense.
None of the values you give are actually representable in a
double, at least on most current architectures, so you're
starting with a slightly different value.  And except for values
which are exactly representable, who knows where the 0's start
on these values.

Anyway, something like:
    std::cout.precision( 22 ) ;
    std::cout.setf( std::ios::fmtflags(), std::ios::floatfield ) ;
    std::cout << "    |" << a << "|\n" ;
    std::cout << "    |" << b << "|\n" ;
    std::cout << "    |" << c << "|\n" ;
gives:
    |1.123456789012334589017|
    |1|
    |1.123450000012345073586|
for the definitions you give.  The final digits in the first and
third don't mean anything, however; typically, the precision of
a double is only around 15 digits.

Thanks for letting me know about that!!!

It is a superb reply for my question =)
I appreciate your help!

Regards,
Alex
 
J

James Kanze

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 better.

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top