This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Possible regression from gcc-3.0.x to gcc-3.2.x with std::setprecision
- From: Andrew Pollard <andrewp at andypo dot net>
- To: gcc at gcc dot gnu dot org
- Cc: libstdc++ at gcc dot gnu dot org
- Date: Fri, 6 Dec 2002 18:48:43 GMT
- Subject: Possible regression from gcc-3.0.x to gcc-3.2.x with std::setprecision
Hi All,
I'm not sure whether this a standard mandated behaviour, but there has
been a change in how std::setprecision works with doubles/floats and
output (bits/locale_facets.tcc:do_put)
Basically, gcc-3.0 limits the range of the setprecision to
std::numeric_limits<float,double>+3 (9, 18)
and gcc-3.2 and above limit it to
std::numeric_limits<float,double>+1 (7, 16)
This means that certain numbers cannot be streamed into a stream, and then
streamed back out... getting the exact same number out. I see the reasoning
behing using +1, since the precision of a double is between 15 and 16
decimal digits, thus 16 should 'always' work... but it appears that some
numbers require up to 18 digits.
I used the following program, and tried gcc-3.0.4, gcc-3.2.1 and
gcc-3.3 (20021201)
double.cpp:
=============================================================================
#include <limits>
#include <iostream>
#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::string st(ss1.str());
std::stringstream ss2(st);
ss2 >> val2;
std::cout << std::setprecision(prec) << val1 << "\n" << val2 << std::endl;
std::cout << std::boolalpha << (val1 == val2) << std::endl;
return (val1 == val2);
}
=============================================================================
% g++30 double.cxx ; a.out ; echo $?
2.22507385850720138e-308
2.22507385850720138e-308
true
1
% g++32 double.cxx ; a.out ; echo $?
2.225073858507201e-308
2.225073858507201e-308
false
0
% g++33 double.cxx ; a.out ; echo $?
2.225073858507201e-308
2.225073858507201e-308
false
0
Has anyone got any comments?
Andrew.
--
Andrew Pollard, Brooks-PRI Automation | home: andrew@andypo.net
670 Eskdale Road, Winnersh Triangle, UK | work: Andrew.Pollard@brooks-pri.com
Tel/Fax:+44 (0)118 9215603 / 9215660 | http://www.andypo.net