"overflow in multiplication");
static_assert(__a0 * __b1 + __b0 * __a1 < (__c >> 1),
"overflow in multiplication");
- static_assert(__b0 * __a0 <= INTMAX_MAX,
+ static_assert(__b0 * __a0 <= __INTMAX_MAX__,
"overflow in multiplication");
static_assert((__a0 * __b1 + __b0 * __a1) * __c <=
- INTMAX_MAX - __b0 * __a0, "overflow in multiplication");
+ __INTMAX_MAX__ - __b0 * __a0, "overflow in multiplication");
public:
static const intmax_t value = _Pn * _Qn;
// Helpers for __safe_add
template<intmax_t _Pn, intmax_t _Qn, bool>
struct __add_overflow_check_impl
- : integral_constant<bool, (_Pn <= INTMAX_MAX - _Qn)>
+ : integral_constant<bool, (_Pn <= __INTMAX_MAX__ - _Qn)>
{ };
template<intmax_t _Pn, intmax_t _Qn>
struct __add_overflow_check_impl<_Pn, _Qn, false>
- : integral_constant<bool, (_Pn >= -INTMAX_MAX - _Qn)>
+ : integral_constant<bool, (_Pn >= -__INTMAX_MAX__ - _Qn)>
{ };
template<intmax_t _Pn, intmax_t _Qn>
static const intmax_t value = _Pn + _Qn;
};
+ /**
+ * @brief Provides compile-time rational arithmetic.
+ *
+ * This class template represents any finite rational number with a
+ * numerator and denominator representable by compile-time constants of
+ * type intmax_t. The ratio is simplified when instantiated.
+ *
+ * For example:
+ * @code
+ * std::ratio<7,-21>::num == -1;
+ * std::ratio<7,-21>::den == 3;
+ * @endcode
+ *
+ */
template<intmax_t _Num, intmax_t _Den = 1>
struct ratio
{
static_assert(_Den != 0, "denominator cannot be zero");
- static_assert(_Num > INTMAX_MIN && _Den > INTMAX_MIN, "out of range");
-
+ static_assert(_Num >= -__INTMAX_MAX__ && _Den >= -__INTMAX_MAX__,
+ "out of range");
+
// Note: sign(N) * abs(N) == N
static const intmax_t num =
_Num * __static_sign<_Den>::value / __static_gcd<_Num, _Den>::value;
{ };
template<typename _R1, typename _R2>
- struct ratio_less
+ struct __ratio_less_simple_impl
: integral_constant<bool,
- (__safe_multiply<_R1::num, _R2::den>::value <
- __safe_multiply<_R2::num, _R1::den>::value)>
+ (__safe_multiply<_R1::num, _R2::den>::value
+ < __safe_multiply<_R2::num, _R1::den>::value)>
+ { };
+
+ // If the denominators are equal or the signs differ, we can just compare
+ // numerators, otherwise fallback to the simple cross-multiply method.
+ template<typename _R1, typename _R2>
+ struct __ratio_less_impl
+ : conditional<(_R1::den == _R2::den
+ || (__static_sign<_R1::num>::value
+ != __static_sign<_R2::num>::value)),
+ integral_constant<bool, (_R1::num < _R2::num)>,
+ __ratio_less_simple_impl<_R1, _R2>>::type
+ { };
+
+ template<typename _R1, typename _R2>
+ struct ratio_less
+ : __ratio_less_impl<_R1, _R2>::type
{ };
template<typename _R1, typename _R2>
--- /dev/null
+// { 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;
+}