* Possible regression from gcc-3.0.x to gcc-3.2.x with std::setprecision
@ 2002-12-06 11:03 Andrew Pollard
0 siblings, 0 replies; only message in thread
From: Andrew Pollard @ 2002-12-06 11:03 UTC (permalink / raw)
To: gcc; +Cc: libstdc++
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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2002-12-06 18:48 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-06 11:03 Possible regression from gcc-3.0.x to gcc-3.2.x with std::setprecision Andrew Pollard
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).