printf vs iostream

T

thomas

Hi,
A small problem about printf still bothers me.

printf("%.6llf", x); will print a "double" with precision
1e-6, i.e., 6 digits are shown after the point.

How to specify the parameter for iostream, i.e. "cout", to have the
same effect.


Thomas
 
A

Alf P. Steinbach

* thomas:
Hi,
A small problem about printf still bothers me.

printf("%.6llf", x); will print a "double"

Are you sure about that?

with precision
1e-6, i.e., 6 digits are shown after the point.

How to specify the parameter for iostream, i.e. "cout", to have the
same effect.

Precision 6 is the default for iostreams.


Cheers & hth.,

- Alf
 
J

James Kanze

* thomas:
Are you sure about that?

It might. It's undefined behavior. (The "ll" length modifier
can only appear with a d, i, o, u, x, or X conversion specifier.
Otherwise, it's undefined behavior, but I can easily imagine an
implementation just ignoring it---that is, after all, what is
simplest to implement.)
Precision 6 is the default for iostreams.

And there's a setprecision manipulator, and a precision member
function, to set it to other values. It's up to the programmer
to remember to reset it to the default, however.

In practice, of course, good code generally won't use either
directly, but will define a semantically significant manipulator
which sets the desired value, probably restoring the original
value at the end of the full expression. On the other hand,
when outputing tables and such, it might sometimes be reasonable
to set the value once, before looping over the table elements,
and only restoring it when the whole table has been output
(using RAII, of course).
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top