Topic: how to set number of decimals as precision


Author: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1995/06/28
Raw View
In article <Pine.SUN.3.91.950624094805.12577A-100000@babbage> Stan
Mulder <mulder@csee.usf.edu> writes:

|> #include <iomanip.h>

|> int main()
|> {
|>    // save original flags and precision settings and set new values at
|>    // the same time
|>    long orig_flags = cout.flags(ios::fixed | ios::showpoint);

A small point: the type of ios::flags is implementation dependant.
The draft standard says it is a `fmtflags', but none of the compilers
I use define this symbol.  (So much for portable code.  All of my
compilers use long, as above.)

The real point of this posting, however:

On my system, the flags field also contains bits to control buffering,
flushing, etc.  Simply setting the flags, as above, will modify these
flags, with probably undesired results.

A quick glance at the draft did not reveal any language that forbids
this.  (The flags in questions are *not* standard, and are not
mentioned in the draft, so the question is, is an implementation
allowed to make such additions?)

Independantly of the standard, given the behavior of current
compilers, I would be inclined to limit the parameters of ios::flags
to values that were returned from this function (or from ios::setf or
ios::unsetf).

In view of this, I would write the above line:

 long orig_flags = cout.setf( ios::fixed | ios::showpoint ,
                              ios::floatfield ) ;

It might also be wise to reset flags like left, right, internal, or
uppercase.  (This can be done by or'ing them into the second parameter
of ios::setf.)  Long term, it might be wise to add words to the draft
to explicitly ban such extra flags.

I've cross-posted this to comp.std.c++, since there is a standards
question involved.
--
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                              -- Beratung in industrieller Datenverarbeitung