Topic: ios_base::precision


Author: Bill Dimm <billd@gim.net>
Date: 1996/07/18
Raw View
Section 27.4.3.2 of the April 28, 1995 ANSI C++ Draft
describes the ios_base::precision function saying:
"The precision (number of digits after the decimal point) to generate
on certain output conversions."
However, the stream libraries that I have used (g++ 2.7.0 and
Visual C++ 4.0) interpret the precision as the number of digits
after the decimal PLUS ONE with a precision setting of 0 denoting
that a default value should be used.  For example:
  cout.precision(3);
  cout << 3.14159 << endl;
will print "3.14".  Does the proposed ANSI standard deviate from
current practice here or am I just reading the draft incorrectly?

Thanks,
  Bill Dimm    billd@gim.net
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: clamage@Eng.sun.com (Steve Clamage)
Date: 1996/07/19
Raw View
In article 5A81@gim.net, Bill Dimm <billd@gim.net> writes:
>Section 27.4.3.2 of the April 28, 1995 ANSI C++ Draft
>describes the ios_base::precision function saying:
>"The precision (number of digits after the decimal point) to generate
>on certain output conversions."

The iostreams chapter of the draft has been extensively revised since
April 1995.

The iostreams formatting options are now described in terms of equivalent
printf formatting flags. Setting the value of ios_base::precision
is supposed to have the same effect as the "precision" format effector
of a printf directive.

That is, if you would otherwise write "%W.Pe" for floating-point
output with printf, you would with iostreams set width to W,
precision to P, and set the floatfield to "scientific".

---
Steve Clamage, stephen.clamage@eng.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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: jching@aloha.com (Jimen Ching)
Date: 1996/07/20
Raw View
Steve Clamage (clamage@Eng.sun.com) wrote:
>That is, if you would otherwise write "%W.Pe" for floating-point
>output with printf, you would with iostreams set width to W,
>precision to P, and set the floatfield to "scientific".

Does the ANSI standard (C or C++) specify what the output looks
like if W == P?  The following program prints 1.235e5, instead of
the .123e6 that I expect.

#include <stdio.h>
int main() { printf("%3.3e\n", 123456.7890); return 0; }

I got this result from both Borland C++ 4.51 and GCC 2.7.2 (libc 4.7.2
on Linux).  Is this a bug?
--jc
--
Jimen Ching (WH6BRR)      jching@aloha.com     wh6brr@uhm.ampr.org
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/07/21
Raw View
jching@aloha.com (Jimen Ching) writes:

>Steve Clamage (clamage@Eng.sun.com) wrote:
>>That is, if you would otherwise write "%W.Pe" for floating-point
>>output with printf, you would with iostreams set width to W,
>>precision to P, and set the floatfield to "scientific".

>Does the ANSI standard (C or C++) specify what the output looks
>like if W == P?  The following program prints 1.235e5, instead of
>the .123e6 that I expect.

>#include <stdio.h>
>int main() { printf("%3.3e\n", 123456.7890); return 0; }

In stdio and iostreams output, width always specifies the MINIMUM
field width, not the exact field width. The precision says you want
3 digits to the right of the decimal, and you will always get the decimal
and the digit to the left of the decimal pointer as well, not to
mention the exponent field. (There are a few exceptions involving
special flags and zero values.)

--
Steve Clamage, stephen.clamage@eng.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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]