summaryrefslogtreecommitdiff
path: root/libstdc++-v3/include/std/ratio
diff options
context:
space:
mode:
authorPaolo Carlini <paolo@gcc.gnu.org>2008-07-05 10:24:08 +0000
committerPaolo Carlini <paolo@gcc.gnu.org>2008-07-05 10:24:08 +0000
commitea31932d5340a2c7d3c3e54ddc3b7b7bb45b8b3e (patch)
tree54b98c51fa82d085b2a0df4d28045dbfdb75dd14 /libstdc++-v3/include/std/ratio
parent752929c698155492e4a7ff954bda3bce544bd46e (diff)
[multiple changes]
2008-07-05 Paolo Carlini <paolo.carlini@oracle.com> * include/std/ratio: Prefer __INTMAX_MAX__ to INTMAX_MAX (INTMAX_MIN). 2008-07-05 Chris Fairles <chris.fairles@gmail.com> * 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
Diffstat (limited to 'libstdc++-v3/include/std/ratio')
-rw-r--r--libstdc++-v3/include/std/ratio49
1 files changed, 40 insertions, 9 deletions
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<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>
@@ -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<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;
@@ -211,10 +226,26 @@ namespace std
{ };
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>