Author: Paolo Carlini Date: To: Chris Fairles CC: gcc-patches, libstdc++, bkoz Subject: Re: [v3] c++0x std::ratio implementation
Hi,
> Attached is a patch that short-cuts ratio_less when denominators
> are equal or signs differ. This prevents overflow in a few cases
> (test case provided).
Ok.
> Also, I added some doxygen documentation to std::ratio class.
> However, when I built the documentation (html) it didn't look like it
> was picking up the header. Does something need modification for
> doxygen to pick it up.
Let's ask Benjamin...
I'm committing the below, which avoids in <ratio> the use of the various limit macros, which, according to C99, in C++ are not defined in <stdint.h> when __STDC_LIMIT_MACROS is not defined. In fact we have a problem here, because, in C++0x, the availability of those limit macros in <cstdint> is supposed to be *independent* of the feature macro to be defined and since our <cstdint> includes <stdint.h> it suffices that the user includes once the latter without defining the feature macro for the following inclusion of <cstdint> to become a NOP, thus the limit macros remain forever unavailable... grunt. In fact this requirement in C++0x seems to me largely contrary to the resolution of DR 456, I have to study the issue a little more...
2008-07-05 Paolo Carlini <paolo.carlini@???>
* include/std/ratio: Prefer __INTMAX_MAX__ to INTMAX_MAX (and INTMAX_MIN).
2008-07-05 Chris Fairles <chris.fairles@???>
* include/std/ratio: Documentation for std::ratio class. Add conditions
to ratio_less to prevent overflow.
* testsuite/20_util/ratio/comparisons/comp2.cc: New.
* testsuite/20_util/ratio/cons/cons_overflow.cc: Update dg-error line
numbers.
#endif //_GLIBCXX_USE_C99_STDINT_TR1
Index: testsuite/20_util/ratio/comparisons/comp2.cc
===================================================================
--- testsuite/20_util/ratio/comparisons/comp2.cc (revision 0)
+++ testsuite/20_util/ratio/comparisons/comp2.cc (revision 0)
@@ -0,0 +1,56 @@
+// { dg-options "-std=gnu++0x" }
+
+// Copyright (C) 2008 Free Software Foundation
+//
+// 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 2, 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 COPYING. If not, write to
+// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+// Boston, MA 02110-1301, USA.
+
+#include <ratio>
+#include <testsuite_hooks.h>
+
+#ifdef _GLIBCXX_USE_C99_STDINT_TR1
+
+static const std::intmax_t M = INTMAX_MAX;
+
+void
+test01()
+{
+ bool test __attribute__((unused)) = true;
+
+ //no overflow with same denominator
+ VERIFY(( std::ratio_less<std::ratio<M - 2, M>,
+ std::ratio<M - 1, M>>::value == 1 ) );
+
+ VERIFY(( std::ratio_less<std::ratio<M - 1, M>,
+ std::ratio<M - 2, M>>::value == 0 ) );
+
+ //no overflow if signs differ
+ VERIFY(( std::ratio_less<std::ratio<-M, M - 1>,
+ std::ratio<M - 1, M - 2>>::value == 1 ) );
+
+ VERIFY(( std::ratio_less<std::ratio<M - 1, M - 2>,
+ std::ratio<-M, M - 1>>::value == 0 ) );
+}
+
+#endif //_GLIBCXX_USE_C99_STDINT_TR1
+
+int main()
+{
+#ifdef _GLIBCXX_USE_C99_STDINT_TR1
+ test01();
+#endif
+ return 0;
+}