aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott Snyder <snyder@fnal.gov>2002-03-09 02:01:34 +0000
committerBenjamin Kosnik <bkoz@gcc.gnu.org>2002-03-09 02:01:34 +0000
commit96854fe254e5d4758bab1bd3f6a3b68b6c8ff616 (patch)
tree2776f7a9be6f9f84b9e2b17edbefe0efdfa9d447
parent3070dd00e9eb9530a18a340a0fbfddd8492f5020 (diff)
downloadgcc-96854fe254e5d4758bab1bd3f6a3b68b6c8ff616.zip
gcc-96854fe254e5d4758bab1bd3f6a3b68b6c8ff616.tar.gz
gcc-96854fe254e5d4758bab1bd3f6a3b68b6c8ff616.tar.bz2
locale_facets.tcc (num_put::_M_convert_float): Allow one more digit of precision.
2002-03-08 scott snyder <snyder@fnal.gov> libstdc++/5875 * include/bits/locale_facets.tcc (num_put::_M_convert_float): Allow one more digit of precision. * testsuite/27_io/ostream_inserter_arith.cc: Test that we can write a double and read back in the same value. From-SVN: r50470
-rw-r--r--libstdc++-v3/include/bits/locale_facets.tcc4
-rw-r--r--libstdc++-v3/testsuite/27_io/ostream_inserter_arith.cc20
2 files changed, 22 insertions, 2 deletions
diff --git a/libstdc++-v3/include/bits/locale_facets.tcc b/libstdc++-v3/include/bits/locale_facets.tcc
index e08dc36..3c8fea1 100644
--- a/libstdc++-v3/include/bits/locale_facets.tcc
+++ b/libstdc++-v3/include/bits/locale_facets.tcc
@@ -609,7 +609,9 @@ namespace std
_M_convert_float(_OutIter __s, ios_base& __io, _CharT __fill, char __mod,
_ValueT __v) const
{
- const int __max_digits = numeric_limits<_ValueT>::digits10;
+ // Note: digits10 is rounded down. We need to add 1 to ensure
+ // we get the full available precision.
+ const int __max_digits = numeric_limits<_ValueT>::digits10 + 1;
streamsize __prec = __io.precision();
// Protect against sprintf() buffer overflows.
if (__prec > static_cast<streamsize>(__max_digits))
diff --git a/libstdc++-v3/testsuite/27_io/ostream_inserter_arith.cc b/libstdc++-v3/testsuite/27_io/ostream_inserter_arith.cc
index bbe2759..041f314 100644
--- a/libstdc++-v3/testsuite/27_io/ostream_inserter_arith.cc
+++ b/libstdc++-v3/testsuite/27_io/ostream_inserter_arith.cc
@@ -1,7 +1,7 @@
// 1999-11-15 Kevin Ediger <kediger@licor.com>
// test the floating point inserters (facet num_put)
-// Copyright (C) 1999 Free Software Foundation, Inc.
+// Copyright (C) 1999, 2002 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
@@ -20,6 +20,8 @@
// USA.
#include <cstdio> // for sprintf
+#include <cmath> // for abs
+#include <cfloat> // for DBL_EPSILON
#include <iostream>
#include <iomanip>
#include <locale>
@@ -355,6 +357,21 @@ test04()
return 0;
}
+int
+test05()
+{
+ double pi = 3.14159265358979323846;
+ ostringstream ostr;
+ ostr.precision(20);
+ ostr << pi;
+ string sval = ostr.str();
+ istringstream istr (sval);
+ double d;
+ istr >> d;
+ VERIFY (abs(pi-d)/pi < DBL_EPSILON);
+ return 0;
+}
+
int
main()
{
@@ -362,6 +379,7 @@ main()
test02();
test03();
test04();
+ test05();
#ifdef TEST_NUMPUT_VERBOSE
cout << "Test passed!" << endl;
#endif