From ea31932d5340a2c7d3c3e54ddc3b7b7bb45b8b3e Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Sat, 5 Jul 2008 10:24:08 +0000 Subject: [multiple changes] 2008-07-05 Paolo Carlini * include/std/ratio: Prefer __INTMAX_MAX__ to INTMAX_MAX (INTMAX_MIN). 2008-07-05 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. From-SVN: r137504 --- libstdc++-v3/include/std/ratio | 49 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 9 deletions(-) (limited to 'libstdc++-v3/include/std/ratio') diff --git a/libstdc++-v3/include/std/ratio b/libstdc++-v3/include/std/ratio index 6dcefa5b931..9980571f160 100644 --- a/libstdc++-v3/include/std/ratio +++ b/libstdc++-v3/include/std/ratio @@ -96,10 +96,10 @@ namespace std "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; @@ -108,12 +108,12 @@ namespace std // Helpers for __safe_add template struct __add_overflow_check_impl - : integral_constant + : integral_constant { }; template struct __add_overflow_check_impl<_Pn, _Qn, false> - : integral_constant= -INTMAX_MAX - _Qn)> + : integral_constant= -__INTMAX_MAX__ - _Qn)> { }; template @@ -130,12 +130,27 @@ namespace std 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 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; @@ -211,10 +226,26 @@ namespace std { }; template - struct ratio_less + struct __ratio_less_simple_impl : integral_constant::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 + struct __ratio_less_impl + : conditional<(_R1::den == _R2::den + || (__static_sign<_R1::num>::value + != __static_sign<_R2::num>::value)), + integral_constant, + __ratio_less_simple_impl<_R1, _R2>>::type + { }; + + template + struct ratio_less + : __ratio_less_impl<_R1, _R2>::type { }; template -- cgit v1.2.3