Topic: C++ Lib - setprecision


Author: "SteveL" <steve.love@tnt.co.uk>
Date: 1999/09/06
Raw View
John Potter wrote in message <37cf1268.17962549@news.csrlink.net>...
>
>On 2 Sep 1999 15:41:09 GMT, "SteveL" <steve.love@tnt.co.uk> wrote:
>
>[On precision and significant or decimal places]
>
>But it follows C exactly <g>.  %10.4g gives four significant digits
>while %10.4f and %20.4e give four decimal places.  In C++ if you do not
>set either fixed or scientific (or you set both), you get general and
>it is significant digits.  Set one of them and you get decimal places.

Yes I'm beginning to see through the smoke here <g>. I'm glad you guys are
here, because NONE of the texts I've read make it terribly clear!

Many thanks

>John
>
>P.S.  I don't think that either Stroustrup or Lippman were very
>interested in writing the I/O parts of their books.  Do you think
>a little COBOL instruction would help ;-)

Not too sure how ...  >:-)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/09/03
Raw View
"SteveL" <steve.love@tnt.co.uk> writes:

>I have become embroiled in a discussion about the use of the word
>'precision' in connection with the standard library iostreams. According to
>Bjarne's 3rd Edition C++ lang., std::setprecision(int) defines the number of
>decimal places, e.g.

> << std::setprecision(4) <<

>Should output 4.14543 as 4.1454

>This is also the opinion of Borlands BCB4 docs. The reason for the
>discussion is that the above was actually output as 4.145, i.e. four
>*significant digits*, by BCB4.

>Interestingly, the C++ Primer (Lippman & Lajoie, 3rd Ed) also says (by
>example) that setprecision is significant digits, not decimal places.

>I would be most interested to hear your views. IIRC, 'C' and printf uses
>'precision' to mean decimal places, at least in most of the docs I read. I
>would therefore expect C++ to follow by not astonishing its C converts too
>much <g>.

The section on iostream formatting in the standard defers the
description of formatting to section 22.2.2, where locales are discussed.

I/O formatting is defined in terms of the printf and scanf formatting
directives. Depending on the "floatfield" settings, the output
formatting follows the %e, %f, or %g specifier -- %E, %f, or %G if
"uppercase" is set.

Precision is thus either the number of significant figures, or
the number of digits to the right of the decimal point, depending
on the "floatfield" setting.

--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "SteveL" <steve.love@tnt.co.uk>
Date: 1999/09/02
Raw View
Hi All,

I have become embroiled in a discussion about the use of the word
'precision' in connection with the standard library iostreams. According to
Bjarne's 3rd Edition C++ lang., std::setprecision(int) defines the number of
decimal places, e.g.

 << std::setprecision(4) <<

Should output 4.14543 as 4.1454

This is also the opinion of Borlands BCB4 docs. The reason for the
discussion is that the above was actually output as 4.145, i.e. four
*significant digits*, by BCB4.

Interestingly, the C++ Primer (Lippman & Lajoie, 3rd Ed) also says (by
example) that setprecision is significant digits, not decimal places.

I would be most interested to hear your views. IIRC, 'C' and printf uses
'precision' to mean decimal places, at least in most of the docs I read. I
would therefore expect C++ to follow by not astonishing its C converts too
much <g>.


--
Steve Love
/////////////////////////////////////////////
// SMILE -
// And everyone will wonder what you're up to!
/////////////////////////////////////////////




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James.Kanze@dresdner-bank.com
Date: 1999/09/03
Raw View
In article <c1.2b8.2S2tq4$063@intranet.tnt.co.uk>,
  "SteveL" <steve.love@tnt.co.uk> wrote:

> I have become embroiled in a discussion about the use of the word
> 'precision' in connection with the standard library
> iostreams. According to Bjarne's 3rd Edition C++ lang.,
> std::setprecision(int) defines the number of decimal places, e.g.

>  << std::setprecision(4) <<

> Should output 4.14543 as 4.1454

What it should output depends on other flags as well:-).

> This is also the opinion of Borlands BCB4 docs. The reason for the
> discussion is that the above was actually output as 4.145, i.e. four
> *significant digits*, by BCB4.

> Interestingly, the C++ Primer (Lippman & Lajoie, 3rd Ed) also says (by
> example) that setprecision is significant digits, not decimal places.

> I would be most interested to hear your views. IIRC, 'C' and printf
> uses 'precision' to mean decimal places, at least in most of the docs
> I read. I would therefore expect C++ to follow by not astonishing its
> C converts too much <g>.

C++ defines the conversions in terms of printf specifiers, with
reference to the C standard, so C converts should not be astonished.

The C standard does *not* use precision to mean decimal places.  The
meaning of the precision in a conversion specifier varies according to
the type of conversion being done.  I quote (from C9x, because it all I
have handy, but I'm pretty sure this hasn't been changed):

  - An optional precision that gives the minimum number of digits to
    appear for the d, i, o, u, x and X conversions, the number of digits
    to appear after the decimal-point character for the a, A, e, E, f
    and F conversions, the maximum number of sygnificant digits for the
    g and G conversions, or the maximum number of bytes to be written
    for s conversions. [...]

For floating point output, the C++ standard defines the conversion used
as a function of ios::floatfield, as follows: 0 (the default), g
conversion; ios::fixed, f conversion, and ios::scientific, e or E
conversion.  So if you write:

    cout << setprecision( 4 ) << 4.14543 ;

you should get 4.145, unless someone before you has set ios::floatfield,
and forgotten to reset it.

Normally, you would also set the floatfield -- most of the time, you
want f format, and not g.  And if you write:

    cout << fixed << setprecision( 4 ) << 4.14543 ;

which should result in 4.1454.

I usually wrap all this is a simple, home-made manipulator; a class FFmt
with a constructor which takes width and precision, and a friend
function:

    ostream&
    operator<<( ostream& dest , FFmt const& fmt )
    {
        dest.setf( ios::fixed , ios::floatfield ) ;
        dest.width( fmt.width ) ;
        dest.precision( fmt.precision ) ;
        return dest ;
    }

That way, I can simply write:

    cout << FFmt( 6 , 4 ) << 4.14543 ;

without worrying about the rest.

(My actual implementation is slightly more complicated, as it restores
the initial state of the stream in the destructor, which will be called
at the end of the full expression.)

--
James Kanze                   mailto: James.Kanze@dresdner-bank.com
Conseils en informatique orient   e objet/
                  Beratung in objekt orientierter Datenverarbeitung
Ziegelh   ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1999/09/03
Raw View
On 2 Sep 1999 15:41:09 GMT, "SteveL" <steve.love@tnt.co.uk> wrote:

[On precision and significant or decimal places]

: I would be most interested to hear your views. IIRC, 'C' and printf uses
: 'precision' to mean decimal places, at least in most of the docs I read. I
: would therefore expect C++ to follow by not astonishing its C converts too
: much <g>.

But it follows C exactly <g>.  %10.4g gives four significant digits
while %10.4f and %20.4e give four decimal places.  In C++ if you do not
set either fixed or scientific (or you set both), you get general and
it is significant digits.  Set one of them and you get decimal places.

John

P.S.  I don't think that either Stroustrup or Lippman were very
interested in writing the I/O parts of their books.  Do you think
a little COBOL instruction would help ;-)


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]