libstdc++/9151: [3.2/3.3/3.4 regression] std::setprecision limited to 16 digits when outputting a double to a stream

andrew@andypo.net andrew@andypo.net
Fri Jan 3 06:16:00 GMT 2003


>Number:         9151
>Category:       libstdc++
>Synopsis:       [3.2/3.3/3.4 regression] std::setprecision limited to 16 digits when outputting a double to a stream
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Jan 02 22:16:01 PST 2003
>Closed-Date:
>Last-Modified:
>Originator:     Andrew Pollard
>Release:        gcc-3.2 and above
>Organization:
>Environment:
i686-pc-linux-gnu on a PIII system
>Description:
[ Re: http://gcc.gnu.org/ml/libstdc++/2002-12/msg00064.html ]

libstdc++-v3 in gcc-3.2 and above limits the precision in a std::setprecision() call to 16 digits when outputting a double to a stream. This (for some reason) causes the output of std::numeric_limits<double>::min() to be such that when you read the string value back into a double, the double is not the same as the double output.

For some reason double::min() requires 18 digits for this to
work.

A test case follows...

double.c:
----------------------------
#include <limits>
#include <sstream>
#include <iomanip>

int
main()
{
    int    prec = std::numeric_limits<double>::digits10+3;
    double val1 = std::numeric_limits<double>::min();
    double val2 = 0.0;

    std::stringstream ss1;
    ss1 << std::setprecision(prec) << val1 << std::ends;
    std::stringstream ss2(ss1.str());
    ss2 >> val2;

    return (val1 == val2);
}
--------------------------------

% g++30 double.cxx; ./a.out; echo $?
1
% g++32 double.cxx; ./a.out; echo $?
0
% g++33 double.cxx; ./a.out; echo $?
0
% g++34 double.cxx; ./a.out; echo $?
0
>How-To-Repeat:

>Fix:
The 'obvious fix' is to change the +1 to a +3 in include/bits/locale_facets.tcc:_M_convert_float line 627
[seems to be the same code in 3.2/3.3 and 3.4]

Quite why this needs 18 digits here, I haven't got a clue though.
>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-prs mailing list