diff mbox

[v2] libstdc++: Add hexfloat/defaultfloat io manipulators.

Message ID 1901588.qodTErPe42@descartes
State New
Headers show

Commit Message

Rüdiger Sonderfeld March 27, 2014, 4 p.m. UTC
Hello Jonathan,

thanks for your comments.

> N.B. patches to the ChangeLog rarely apply cleanly (because someone
> else may have changed the ChangeLog since the patch was created) so
> the convention is to send the ChangeLog entry in the email body, or as
> a separate attachment, or by using 'git log -p @{u}..' so the commit
> log shows it, rather than as part of the patch.

Yes, ChangeLog's can be a bit of a pain.  I removed the ChangeLog from the
patch.  But FYI there is a ChangeLog merge driver hidden in gnulib, which
can be helpful when dealing with ChangeLog files in git (and potentially
other version control systems)

http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/git-merge-changelog.c

> We could document that (fixed|scientific) has that effect in c++98
> mode.

Where should it be documented?

Regards,
Rüdiger

-- 8< ---------------------------------------------------------- >8 --

* include/bits/ios_base.h (hexfloat): New function.
(defaultfloat): New function.
* src/c++98/locale_facets.cc (__num_base::_S_format_float):
Support hexadecimal floating point format.
* testsuite/27_io/basic_ostream/inserters_arithmetic/char/hexfloat.cc:
New file.

Signed-off-by: Rüdiger Sonderfeld <ruediger@c-plusplus.de>
---
 libstdc++-v3/include/bits/ios_base.h               |  21 +++
 libstdc++-v3/src/c++98/locale_facets.cc            |   4 +
 .../inserters_arithmetic/char/hexfloat.cc          | 141 +++++++++++++++++++++
 3 files changed, 166 insertions(+)
 create mode 100644 libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/char/hexfloat.cc

Comments

Jonathan Wakely March 27, 2014, 4:27 p.m. UTC | #1
On 27/03/14 17:00 +0100, Rüdiger Sonderfeld wrote:
>Hello Jonathan,
>
>thanks for your comments.
>
>> N.B. patches to the ChangeLog rarely apply cleanly (because someone
>> else may have changed the ChangeLog since the patch was created) so
>> the convention is to send the ChangeLog entry in the email body, or as
>> a separate attachment, or by using 'git log -p @{u}..' so the commit
>> log shows it, rather than as part of the patch.
>
>Yes, ChangeLog's can be a bit of a pain.  I removed the ChangeLog from the
>patch.  But FYI there is a ChangeLog merge driver hidden in gnulib, which
>can be helpful when dealing with ChangeLog files in git (and potentially
>other version control systems)
>
>http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/git-merge-changelog.c

Yes, I use that myself, and generate patches with the 'git lgp'
command shown at http://gcc.gnu.org/wiki/GitMirror

>> We could document that (fixed|scientific) has that effect in c++98
>> mode.
>
>Where should it be documented?

Probably somewhere in doc/xml/manual/io.xml but I'm happy to do that
once the patch is committed if you like.

Thanks for the updated patch, I will try to remember to commit it when
stage 1 starts. If you don't get a mail from me then please ping me as
a reminder.
Jonathan Wakely April 15, 2014, 10:13 a.m. UTC | #2
On 27 March 2014 23:56, Luke Allardyce wrote:
> It looks like the new standard also requires the precision to be
> ignored for hexfloat
>
>>For conversion from a floating-point type, if floatfield != (ios_base::fixed | ios_base:: scientific), str.precision() is specified as precision in the conversion specification. Otherwise, no precision is specified.

Thanks for pointing out that difference. We'll need a test for that.

> Also the old standard seems to require that ios_base::fixed |
> ios_base::scientific (or any other combination) falls through to the
> uppercase test; I was trying to use abi_tag for a solution as not only
> would two versions of _S_format_float be necessary, but also num_get
> due to the pre-instantiated templates for <char> and <wchar>, which
> led me to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60642. It might
> just be more trouble than it's worth.

I don't think we need to worry about that, if I understand correctly
the combination of fixed|scientific has unspecified behaviour in
C++03, so we can make our implementation do exactly what it does in
C++11.
Luke Allardyce April 16, 2014, 5:26 a.m. UTC | #3
>> Also the old standard seems to require that ios_base::fixed |
>> ios_base::scientific (or any other combination) falls through to the
>> uppercase test; I was trying to use abi_tag for a solution as not only
>> would two versions of _S_format_float be necessary, but also num_get
>> due to the pre-instantiated templates for <char> and <wchar>, which
>> led me to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60642. It might
>> just be more trouble than it's worth.
>
> I don't think we need to worry about that, if I understand correctly
> the combination of fixed|scientific has unspecified behaviour in
> C++03, so we can make our implementation do exactly what it does in
> C++11.

It seems to me that it is well defined, going from  [lib.facet.num.put.virtuals]

> 6 All tables used in describing stage 1 are ordered. That is, the first line whose condition is true applies.
> A line without a condition is the default behavior when none of the earlier lines apply.

So fixed|scientific would be equivalent to specifying neither
according to table 58, and the resulting specifier would be %g or %G
depending on whether uppercase is set or not.
Jonathan Wakely April 16, 2014, 10:21 a.m. UTC | #4
On 16/04/14 14:26 +0900, Luke Allardyce wrote:
>>> Also the old standard seems to require that ios_base::fixed |
>>> ios_base::scientific (or any other combination) falls through to the
>>> uppercase test; I was trying to use abi_tag for a solution as not only
>>> would two versions of _S_format_float be necessary, but also num_get
>>> due to the pre-instantiated templates for <char> and <wchar>, which
>>> led me to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60642. It might
>>> just be more trouble than it's worth.
>>
>> I don't think we need to worry about that, if I understand correctly
>> the combination of fixed|scientific has unspecified behaviour in
>> C++03, so we can make our implementation do exactly what it does in
>> C++11.
>
>It seems to me that it is well defined, going from  [lib.facet.num.put.virtuals]
>
>> 6 All tables used in describing stage 1 are ordered. That is, the first line whose condition is true applies.
>> A line without a condition is the default behavior when none of the earlier lines apply.
>
>So fixed|scientific would be equivalent to specifying neither
>according to table 58, and the resulting specifier would be %g or %G
>depending on whether uppercase is set or not.

Thanks, I was wrong about that.

Then I think we should just bite the bullet and provide the new
behaviour. If we do have an abi_tag on those types in the next release
then we can preserve the old behaviour in the old ABI and use the
C++11 semantics for the abi_tagged type, which will be used for both
C++03 and C++11 code. I am not too concerned that people who use a
meaningless modifier in C++03 code get the C++11 behaviour. If they
really want %g or %G then they shouldn't use fixed|scientific.
Luke Allardyce April 17, 2014, 12:56 a.m. UTC | #5
> Thanks, I was wrong about that.
>
> Then I think we should just bite the bullet and provide the new
> behaviour. If we do have an abi_tag on those types in the next release
> then we can preserve the old behaviour in the old ABI and use the
> C++11 semantics for the abi_tagged type, which will be used for both
> C++03 and C++11 code. I am not too concerned that people who use a
> meaningless modifier in C++03 code get the C++11 behaviour. If they
> really want %g or %G then they shouldn't use fixed|scientific.

Does that mean abi_tag will be enabled with separate compiler flag /
define rather than checking against the __cplusplus value?
diff mbox

Patch

diff --git a/libstdc++-v3/include/bits/ios_base.h b/libstdc++-v3/include/bits/ios_base.h
index ae856de..b7fae43 100644
--- a/libstdc++-v3/include/bits/ios_base.h
+++ b/libstdc++-v3/include/bits/ios_base.h
@@ -969,6 +969,27 @@  namespace std _GLIBCXX_VISIBILITY(default)
     return __base;
   }
 
+#if __cplusplus >= 201103L
+  // New C++11 floatfield manipulators
+
+  /// Calls base.setf(ios_base::fixed | ios_base::scientific,
+  ///   ios_base::floatfield).
+  inline ios_base&
+  hexfloat(ios_base& __base)
+  {
+    __base.setf(ios_base::fixed | ios_base::scientific, ios_base::floatfield);
+    return __base;
+  }
+
+  /// Calls base.unsetf(ios_base::floatfield).
+  inline ios_base&
+  defaultfloat(ios_base& __base)
+  {
+    __base.unsetf(ios_base::floatfield);
+    return __base;
+  }
+#endif // __cplusplus >= 201103L
+
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace
 
diff --git a/libstdc++-v3/src/c++98/locale_facets.cc b/libstdc++-v3/src/c++98/locale_facets.cc
index 3669acb..a21d665 100644
--- a/libstdc++-v3/src/c++98/locale_facets.cc
+++ b/libstdc++-v3/src/c++98/locale_facets.cc
@@ -82,6 +82,10 @@  namespace std _GLIBCXX_VISIBILITY(default)
       *__fptr++ = 'f';
     else if (__fltfield == ios_base::scientific)
       *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e';
+#ifdef _GLIBCXX_USE_C99
+    else if (__fltfield == (ios_base::fixed | ios_base::scientific))
+      *__fptr++ = (__flags & ios_base::uppercase) ? 'A' : 'a';
+#endif
     else
       *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g';
     *__fptr = '\0';
diff --git a/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/char/hexfloat.cc b/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/char/hexfloat.cc
new file mode 100644
index 0000000..55c46ad
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/char/hexfloat.cc
@@ -0,0 +1,141 @@ 
+// { dg-options "-std=gnu++11" }
+
+// 2014-03-27 Rüdiger Sonderfeld
+// test the hexadecimal floating point inserters (facet num_put)
+
+// Copyright (C) 2014 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
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <iostream>
+#include <iomanip>
+#include <sstream>
+#include <limits>
+#include <testsuite_hooks.h>
+
+using namespace std;
+
+#ifndef _GLIBCXX_ASSERT
+#  define TEST_NUMPUT_VERBOSE 1
+#endif
+
+void
+test01()
+{
+  {
+    ostringstream os;
+    double d = 272.; // 0x1.1p+8;
+    cout << os.precision() << endl;
+    os << hexfloat << setprecision(1);
+    os << d;
+#ifdef TEST_NUMPUT_VERBOSE
+    cout << "got: " << os.str() << endl;
+#endif
+    VERIFY( os && os.str() == "0x1.1p+8" );
+    os.str("");
+    os << uppercase << d;
+#ifdef TEST_NUMPUT_VERBOSE
+    cout << "got: " << os.str() << endl;
+#endif
+    VERIFY( os && os.str() == "0X1.1P+8" );
+    os << nouppercase;
+    os.str("");
+    os << defaultfloat << setprecision(6);
+    os << d;
+#ifdef TEST_NUMPUT_VERBOSE
+    cout << "got: " << os.str() << endl;
+#endif
+    VERIFY( os && os.str() == "272" );
+
+    os.str("");
+    d = 15.; //0x1.ep+3;
+    os << hexfloat << setprecision(1);
+    os << d;
+#ifdef TEST_NUMPUT_VERBOSE
+    cout << "got: " << os.str() << endl;
+#endif
+    VERIFY( os && os.str() == "0x1.ep+3" );
+    os.str("");
+    os << uppercase << d;
+#ifdef TEST_NUMPUT_VERBOSE
+    cout << "got: " << os.str() << endl;
+#endif
+    VERIFY( os && os.str() == "0X1.EP+3" );
+    os << nouppercase;
+    os.str("");
+    os << defaultfloat << setprecision(6);
+    os << d;
+#ifdef TEST_NUMPUT_VERBOSE
+    cout << "got: " << os.str() << endl;
+#endif
+    VERIFY( os && os.str() == "15" );
+  }
+
+  {
+    ostringstream os;
+    long double d = 272.L; // 0x1.1p+8L;
+    os << hexfloat << setprecision(1);
+    os << d;
+#ifdef TEST_NUMPUT_VERBOSE
+    cout << "got: " << os.str() << endl;
+#endif
+    VERIFY( os && os.str() == "0x8.8p+5" );
+    os.str("");
+    os << uppercase << d;
+#ifdef TEST_NUMPUT_VERBOSE
+    cout << "got: " << os.str() << endl;
+#endif
+    VERIFY( os && os.str() == "0X8.8P+5" );
+    os << nouppercase;
+    os.str("");
+    os << defaultfloat << setprecision(6);
+    os << d;
+#ifdef TEST_NUMPUT_VERBOSE
+    cout << "got: " << os.str() << endl;
+#endif
+    VERIFY( os && os.str() == "272" );
+
+    os.str("");
+    os << hexfloat << setprecision(1);
+    d = 15.; //0x1.ep+3;
+    os << d;
+#ifdef TEST_NUMPUT_VERBOSE
+    cout << "got: " << os.str() << endl;
+#endif
+    VERIFY( os && os.str() == "0xf.0p+0" );
+    os.str("");
+    os << uppercase << d;
+#ifdef TEST_NUMPUT_VERBOSE
+    cout << "got: " << os.str() << endl;
+#endif
+    VERIFY( os && os.str() == "0XF.0P+0" );
+    os << nouppercase;
+    os.str("");
+    os << defaultfloat << setprecision(6);
+    os << d;
+#ifdef TEST_NUMPUT_VERBOSE
+    cout << "got: " << os.str() << endl;
+#endif
+    VERIFY( os && os.str() == "15" );
+  }
+}
+
+int
+main()
+{
+  test01();
+  return 0;
+}