std::ios_base::show_pos 
Author Message
 std::ios_base::show_pos

std::showpos always truncates the number of digits to 8
even if I change the width to 10.

Code Used:

        (std::cout).width(10);
        std::cout << std::showpos << 123456.78;

Output:
   +123457
instead of +123456.78

Please tell me as to why this is happening?

Thanks
Sujatha



Tue, 13 Dec 2005 21:37:51 GMT  
 std::ios_base::show_pos

Quote:
> std::showpos always truncates the number of digits to 8
> even if I change the width to 10.

> Code Used:

> (std::cout).width(10);
> std::cout << std::showpos << 123456.78;

> Output:
>    +123457
> instead of +123456.78

> Please tell me as to why this is happening?

The problem is not width, it's precision. Width is 10 all right - note
three spaces in front of the number. But you've never changed precision,
and it's 6 significant digits by default. Try this:

cout << setw(10) << setprecision(8) << showpos << 123456.78

or this

cout << setw(10) << fixed << setprecision(2) << showpos << 123456.78

The differece is that the first prints in general format (assuming the
format was not changed earlier), where precision is the number of
significant digits. The second prints in fixed format where precision
gives the number of digits after the decimal point.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Wed, 14 Dec 2005 00:46:20 GMT  
 std::ios_base::show_pos
According to the ANSI standard precision is the number of
digits after the decimal point. So I thought that there
is some problem.
Thanks for informing that in default format precision is
the number of significant digits and not the number of
digits after decimal point.

Quote:
>-----Original Message-----


>> std::showpos always truncates the number of digits to 8
>> even if I change the width to 10.

>> Code Used:

>> (std::cout).width(10);
>> std::cout << std::showpos << 123456.78;

>> Output:
>>    +123457
>> instead of +123456.78

>> Please tell me as to why this is happening?

>The problem is not width, it's precision. Width is 10 all
right - note
>three spaces in front of the number. But you've never
changed precision,
>and it's 6 significant digits by default. Try this:

>cout << setw(10) << setprecision(8) << showpos <<
123456.78

>or this

>cout << setw(10) << fixed << setprecision(2) << showpos
<< 123456.78

>The differece is that the first prints in general format
(assuming the
>format was not changed earlier), where precision is the
number of
>significant digits. The second prints in fixed format
where precision
>gives the number of digits after the decimal point.
>--
>With best wishes,
>    Igor Tandetnik

>"For every complex problem, there is a solution that is
simple, neat,
>and wrong." H.L. Mencken

>.



Sat, 17 Dec 2005 20:02:50 GMT  
 std::ios_base::show_pos

Quote:
> According to the ANSI standard precision is the number of
> digits after the decimal point.

Wrong. ostream::operator<<() for floating point numbers defers to
num_put locale facet. C++ standard 22.2.2.2.2 defines num_put::do_put in
terms of printf and specifies how a printf conversion specifier is
determined by ios_base flags. Fixed format becomes %f, scientific
becomes %e, and general format (the default) becomes %g. Precision is
added to the specifier. ios_base::showpos is represented by + modifier.
So, in your case with showpos and precision, say, 6, the format becomes
%+.6g in general format and %+.6f in fixed format.

Now, C++ standard does not define the behavior of printf, but instead
defers to C standard. C99 7.19.6.1/4 says:

An optional precision that gives ... the number of digits to appear
after the decimal-point character for a, A, e, E, f, and F conversions,
the maximum number of significant digits for the g and G conversions ...

Note how the meaning of precision field differs between %f and %g.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Sat, 17 Dec 2005 21:45:17 GMT  
 std::ios_base::show_pos
I only saw 27.4.2.2 - 9 in the standard which tells about
ios_base::precision. Thanks a lot for sharing all the
details.

Quote:
>-----Original Message-----


>> According to the ANSI standard precision is the number
of
>> digits after the decimal point.

>Wrong. ostream::operator<<() for floating point numbers
defers to
>num_put locale facet. C++ standard 22.2.2.2.2 defines
num_put::do_put in
>terms of printf and specifies how a printf conversion
specifier is
>determined by ios_base flags. Fixed format becomes %f,
scientific
>becomes %e, and general format (the default) becomes %g.
Precision is
>added to the specifier. ios_base::showpos is represented
by + modifier.
>So, in your case with showpos and precision, say, 6, the
format becomes
>%+.6g in general format and %+.6f in fixed format.

>Now, C++ standard does not define the behavior of printf,
but instead
>defers to C standard. C99 7.19.6.1/4 says:

>An optional precision that gives ... the number of digits
to appear
>after the decimal-point character for a, A, e, E, f, and
F conversions,
>the maximum number of significant digits for the g and G
conversions ...

>Note how the meaning of precision field differs between %
f and %g.
>--
>With best wishes,
>    Igor Tandetnik

>"For every complex problem, there is a solution that is
simple, neat,
>and wrong." H.L. Mencken

>.



Sun, 18 Dec 2005 18:52:57 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. ios_base::beg

2. ios_base

3. When should an input stream throw std::ios_base::failure ?

4. When should an input stream throw std::ios_base::failure ?

5. No std::min or std::max?

6. bug: VS7.0 (6.0) C++ std::auto_ptr conflict with std::vector

7. convert between std::string and std::wstring

8. convert non std::string to std::string

9. Warnings for std::vector<std::string>

10. export classes using std namespace (ex std::vector) in a DLL

11. typedef std::vector<std::string> Vector_String

12. VC6, STL, std::set_new_handler and std::bad_alloc

 

 
Powered by phpBB® Forum Software