Home | History | Annotate | Line # | Download | only in include
ratio revision 1.1
      1  1.1  joerg // -*- C++ -*-
      2  1.1  joerg //===---------------------------- ratio -----------------------------------===//
      3  1.1  joerg //
      4  1.1  joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      5  1.1  joerg // See https://llvm.org/LICENSE.txt for license information.
      6  1.1  joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      7  1.1  joerg //
      8  1.1  joerg //===----------------------------------------------------------------------===//
      9  1.1  joerg 
     10  1.1  joerg #ifndef _LIBCPP_RATIO
     11  1.1  joerg #define _LIBCPP_RATIO
     12  1.1  joerg 
     13  1.1  joerg /*
     14  1.1  joerg     ratio synopsis
     15  1.1  joerg 
     16  1.1  joerg namespace std
     17  1.1  joerg {
     18  1.1  joerg 
     19  1.1  joerg template <intmax_t N, intmax_t D = 1>
     20  1.1  joerg class ratio
     21  1.1  joerg {
     22  1.1  joerg public:
     23  1.1  joerg     static constexpr intmax_t num;
     24  1.1  joerg     static constexpr intmax_t den;
     25  1.1  joerg     typedef ratio<num, den> type;
     26  1.1  joerg };
     27  1.1  joerg 
     28  1.1  joerg // ratio arithmetic
     29  1.1  joerg template <class R1, class R2> using ratio_add = ...;
     30  1.1  joerg template <class R1, class R2> using ratio_subtract = ...;
     31  1.1  joerg template <class R1, class R2> using ratio_multiply = ...;
     32  1.1  joerg template <class R1, class R2> using ratio_divide = ...;
     33  1.1  joerg 
     34  1.1  joerg // ratio comparison
     35  1.1  joerg template <class R1, class R2> struct ratio_equal;
     36  1.1  joerg template <class R1, class R2> struct ratio_not_equal;
     37  1.1  joerg template <class R1, class R2> struct ratio_less;
     38  1.1  joerg template <class R1, class R2> struct ratio_less_equal;
     39  1.1  joerg template <class R1, class R2> struct ratio_greater;
     40  1.1  joerg template <class R1, class R2> struct ratio_greater_equal;
     41  1.1  joerg 
     42  1.1  joerg // convenience SI typedefs
     43  1.1  joerg typedef ratio<1, 1000000000000000000000000> yocto;  // not supported
     44  1.1  joerg typedef ratio<1,    1000000000000000000000> zepto;  // not supported
     45  1.1  joerg typedef ratio<1,       1000000000000000000> atto;
     46  1.1  joerg typedef ratio<1,          1000000000000000> femto;
     47  1.1  joerg typedef ratio<1,             1000000000000> pico;
     48  1.1  joerg typedef ratio<1,                1000000000> nano;
     49  1.1  joerg typedef ratio<1,                   1000000> micro;
     50  1.1  joerg typedef ratio<1,                      1000> milli;
     51  1.1  joerg typedef ratio<1,                       100> centi;
     52  1.1  joerg typedef ratio<1,                        10> deci;
     53  1.1  joerg typedef ratio<                       10, 1> deca;
     54  1.1  joerg typedef ratio<                      100, 1> hecto;
     55  1.1  joerg typedef ratio<                     1000, 1> kilo;
     56  1.1  joerg typedef ratio<                  1000000, 1> mega;
     57  1.1  joerg typedef ratio<               1000000000, 1> giga;
     58  1.1  joerg typedef ratio<            1000000000000, 1> tera;
     59  1.1  joerg typedef ratio<         1000000000000000, 1> peta;
     60  1.1  joerg typedef ratio<      1000000000000000000, 1> exa;
     61  1.1  joerg typedef ratio<   1000000000000000000000, 1> zetta;  // not supported
     62  1.1  joerg typedef ratio<1000000000000000000000000, 1> yotta;  // not supported
     63  1.1  joerg 
     64  1.1  joerg   // 20.11.5, ratio comparison
     65  1.1  joerg   template <class R1, class R2> inline constexpr bool ratio_equal_v
     66  1.1  joerg     = ratio_equal<R1, R2>::value;                                       // C++17
     67  1.1  joerg   template <class R1, class R2> inline constexpr bool ratio_not_equal_v
     68  1.1  joerg     = ratio_not_equal<R1, R2>::value;                                   // C++17
     69  1.1  joerg   template <class R1, class R2> inline constexpr bool ratio_less_v
     70  1.1  joerg     = ratio_less<R1, R2>::value;                                        // C++17
     71  1.1  joerg   template <class R1, class R2> inline constexpr bool ratio_less_equal_v
     72  1.1  joerg     = ratio_less_equal<R1, R2>::value;                                  // C++17
     73  1.1  joerg   template <class R1, class R2> inline constexpr bool ratio_greater_v
     74  1.1  joerg     = ratio_greater<R1, R2>::value;                                     // C++17
     75  1.1  joerg   template <class R1, class R2> inline constexpr bool ratio_greater_equal_v
     76  1.1  joerg     = ratio_greater_equal<R1, R2>::value;                               // C++17
     77  1.1  joerg }
     78  1.1  joerg */
     79  1.1  joerg 
     80  1.1  joerg #include <__config>
     81  1.1  joerg #include <cstdint>
     82  1.1  joerg #include <climits>
     83  1.1  joerg #include <type_traits>
     84  1.1  joerg 
     85  1.1  joerg #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
     86  1.1  joerg #pragma GCC system_header
     87  1.1  joerg #endif
     88  1.1  joerg 
     89  1.1  joerg _LIBCPP_PUSH_MACROS
     90  1.1  joerg #include <__undef_macros>
     91  1.1  joerg 
     92  1.1  joerg 
     93  1.1  joerg _LIBCPP_BEGIN_NAMESPACE_STD
     94  1.1  joerg 
     95  1.1  joerg // __static_gcd
     96  1.1  joerg 
     97  1.1  joerg template <intmax_t _Xp, intmax_t _Yp>
     98  1.1  joerg struct __static_gcd
     99  1.1  joerg {
    100  1.1  joerg     static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value;
    101  1.1  joerg };
    102  1.1  joerg 
    103  1.1  joerg template <intmax_t _Xp>
    104  1.1  joerg struct __static_gcd<_Xp, 0>
    105  1.1  joerg {
    106  1.1  joerg     static const intmax_t value = _Xp;
    107  1.1  joerg };
    108  1.1  joerg 
    109  1.1  joerg template <>
    110  1.1  joerg struct __static_gcd<0, 0>
    111  1.1  joerg {
    112  1.1  joerg     static const intmax_t value = 1;
    113  1.1  joerg };
    114  1.1  joerg 
    115  1.1  joerg // __static_lcm
    116  1.1  joerg 
    117  1.1  joerg template <intmax_t _Xp, intmax_t _Yp>
    118  1.1  joerg struct __static_lcm
    119  1.1  joerg {
    120  1.1  joerg     static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp;
    121  1.1  joerg };
    122  1.1  joerg 
    123  1.1  joerg template <intmax_t _Xp>
    124  1.1  joerg struct __static_abs
    125  1.1  joerg {
    126  1.1  joerg     static const intmax_t value = _Xp < 0 ? -_Xp : _Xp;
    127  1.1  joerg };
    128  1.1  joerg 
    129  1.1  joerg template <intmax_t _Xp>
    130  1.1  joerg struct __static_sign
    131  1.1  joerg {
    132  1.1  joerg     static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
    133  1.1  joerg };
    134  1.1  joerg 
    135  1.1  joerg template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
    136  1.1  joerg class __ll_add;
    137  1.1  joerg 
    138  1.1  joerg template <intmax_t _Xp, intmax_t _Yp>
    139  1.1  joerg class __ll_add<_Xp, _Yp, 1>
    140  1.1  joerg {
    141  1.1  joerg     static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
    142  1.1  joerg     static const intmax_t max = -min;
    143  1.1  joerg 
    144  1.1  joerg     static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
    145  1.1  joerg public:
    146  1.1  joerg     static const intmax_t value = _Xp + _Yp;
    147  1.1  joerg };
    148  1.1  joerg 
    149  1.1  joerg template <intmax_t _Xp, intmax_t _Yp>
    150  1.1  joerg class __ll_add<_Xp, _Yp, 0>
    151  1.1  joerg {
    152  1.1  joerg public:
    153  1.1  joerg     static const intmax_t value = _Xp;
    154  1.1  joerg };
    155  1.1  joerg 
    156  1.1  joerg template <intmax_t _Xp, intmax_t _Yp>
    157  1.1  joerg class __ll_add<_Xp, _Yp, -1>
    158  1.1  joerg {
    159  1.1  joerg     static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
    160  1.1  joerg     static const intmax_t max = -min;
    161  1.1  joerg 
    162  1.1  joerg     static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
    163  1.1  joerg public:
    164  1.1  joerg     static const intmax_t value = _Xp + _Yp;
    165  1.1  joerg };
    166  1.1  joerg 
    167  1.1  joerg template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
    168  1.1  joerg class __ll_sub;
    169  1.1  joerg 
    170  1.1  joerg template <intmax_t _Xp, intmax_t _Yp>
    171  1.1  joerg class __ll_sub<_Xp, _Yp, 1>
    172  1.1  joerg {
    173  1.1  joerg     static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
    174  1.1  joerg     static const intmax_t max = -min;
    175  1.1  joerg 
    176  1.1  joerg     static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
    177  1.1  joerg public:
    178  1.1  joerg     static const intmax_t value = _Xp - _Yp;
    179  1.1  joerg };
    180  1.1  joerg 
    181  1.1  joerg template <intmax_t _Xp, intmax_t _Yp>
    182  1.1  joerg class __ll_sub<_Xp, _Yp, 0>
    183  1.1  joerg {
    184  1.1  joerg public:
    185  1.1  joerg     static const intmax_t value = _Xp;
    186  1.1  joerg };
    187  1.1  joerg 
    188  1.1  joerg template <intmax_t _Xp, intmax_t _Yp>
    189  1.1  joerg class __ll_sub<_Xp, _Yp, -1>
    190  1.1  joerg {
    191  1.1  joerg     static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
    192  1.1  joerg     static const intmax_t max = -min;
    193  1.1  joerg 
    194  1.1  joerg     static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");
    195  1.1  joerg public:
    196  1.1  joerg     static const intmax_t value = _Xp - _Yp;
    197  1.1  joerg };
    198  1.1  joerg 
    199  1.1  joerg template <intmax_t _Xp, intmax_t _Yp>
    200  1.1  joerg class __ll_mul
    201  1.1  joerg {
    202  1.1  joerg     static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
    203  1.1  joerg     static const intmax_t min = nan + 1;
    204  1.1  joerg     static const intmax_t max = -min;
    205  1.1  joerg     static const intmax_t __a_x = __static_abs<_Xp>::value;
    206  1.1  joerg     static const intmax_t __a_y = __static_abs<_Yp>::value;
    207  1.1  joerg 
    208  1.1  joerg     static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
    209  1.1  joerg public:
    210  1.1  joerg     static const intmax_t value = _Xp * _Yp;
    211  1.1  joerg };
    212  1.1  joerg 
    213  1.1  joerg template <intmax_t _Yp>
    214  1.1  joerg class __ll_mul<0, _Yp>
    215  1.1  joerg {
    216  1.1  joerg public:
    217  1.1  joerg     static const intmax_t value = 0;
    218  1.1  joerg };
    219  1.1  joerg 
    220  1.1  joerg template <intmax_t _Xp>
    221  1.1  joerg class __ll_mul<_Xp, 0>
    222  1.1  joerg {
    223  1.1  joerg public:
    224  1.1  joerg     static const intmax_t value = 0;
    225  1.1  joerg };
    226  1.1  joerg 
    227  1.1  joerg template <>
    228  1.1  joerg class __ll_mul<0, 0>
    229  1.1  joerg {
    230  1.1  joerg public:
    231  1.1  joerg     static const intmax_t value = 0;
    232  1.1  joerg };
    233  1.1  joerg 
    234  1.1  joerg // Not actually used but left here in case needed in future maintenance
    235  1.1  joerg template <intmax_t _Xp, intmax_t _Yp>
    236  1.1  joerg class __ll_div
    237  1.1  joerg {
    238  1.1  joerg     static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
    239  1.1  joerg     static const intmax_t min = nan + 1;
    240  1.1  joerg     static const intmax_t max = -min;
    241  1.1  joerg 
    242  1.1  joerg     static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
    243  1.1  joerg public:
    244  1.1  joerg     static const intmax_t value = _Xp / _Yp;
    245  1.1  joerg };
    246  1.1  joerg 
    247  1.1  joerg template <intmax_t _Num, intmax_t _Den = 1>
    248  1.1  joerg class _LIBCPP_TEMPLATE_VIS ratio
    249  1.1  joerg {
    250  1.1  joerg     static_assert(__static_abs<_Num>::value >= 0, "ratio numerator is out of range");
    251  1.1  joerg     static_assert(_Den != 0, "ratio divide by 0");
    252  1.1  joerg     static_assert(__static_abs<_Den>::value >  0, "ratio denominator is out of range");
    253  1.1  joerg     static _LIBCPP_CONSTEXPR const intmax_t __na = __static_abs<_Num>::value;
    254  1.1  joerg     static _LIBCPP_CONSTEXPR const intmax_t __da = __static_abs<_Den>::value;
    255  1.1  joerg     static _LIBCPP_CONSTEXPR const intmax_t __s = __static_sign<_Num>::value * __static_sign<_Den>::value;
    256  1.1  joerg     static _LIBCPP_CONSTEXPR const intmax_t __gcd = __static_gcd<__na, __da>::value;
    257  1.1  joerg public:
    258  1.1  joerg     static _LIBCPP_CONSTEXPR const intmax_t num = __s * __na / __gcd;
    259  1.1  joerg     static _LIBCPP_CONSTEXPR const intmax_t den = __da / __gcd;
    260  1.1  joerg 
    261  1.1  joerg     typedef ratio<num, den> type;
    262  1.1  joerg };
    263  1.1  joerg 
    264  1.1  joerg template <intmax_t _Num, intmax_t _Den>
    265  1.1  joerg _LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::num;
    266  1.1  joerg 
    267  1.1  joerg template <intmax_t _Num, intmax_t _Den>
    268  1.1  joerg _LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::den;
    269  1.1  joerg 
    270  1.1  joerg template <class _Tp>                    struct __is_ratio                     : false_type {};
    271  1.1  joerg template <intmax_t _Num, intmax_t _Den> struct __is_ratio<ratio<_Num, _Den> > : true_type  {};
    272  1.1  joerg 
    273  1.1  joerg typedef ratio<1LL, 1000000000000000000LL> atto;
    274  1.1  joerg typedef ratio<1LL,    1000000000000000LL> femto;
    275  1.1  joerg typedef ratio<1LL,       1000000000000LL> pico;
    276  1.1  joerg typedef ratio<1LL,          1000000000LL> nano;
    277  1.1  joerg typedef ratio<1LL,             1000000LL> micro;
    278  1.1  joerg typedef ratio<1LL,                1000LL> milli;
    279  1.1  joerg typedef ratio<1LL,                 100LL> centi;
    280  1.1  joerg typedef ratio<1LL,                  10LL> deci;
    281  1.1  joerg typedef ratio<                 10LL, 1LL> deca;
    282  1.1  joerg typedef ratio<                100LL, 1LL> hecto;
    283  1.1  joerg typedef ratio<               1000LL, 1LL> kilo;
    284  1.1  joerg typedef ratio<            1000000LL, 1LL> mega;
    285  1.1  joerg typedef ratio<         1000000000LL, 1LL> giga;
    286  1.1  joerg typedef ratio<      1000000000000LL, 1LL> tera;
    287  1.1  joerg typedef ratio<   1000000000000000LL, 1LL> peta;
    288  1.1  joerg typedef ratio<1000000000000000000LL, 1LL> exa;
    289  1.1  joerg 
    290  1.1  joerg template <class _R1, class _R2>
    291  1.1  joerg struct __ratio_multiply
    292  1.1  joerg {
    293  1.1  joerg private:
    294  1.1  joerg     static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>::value;
    295  1.1  joerg     static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>::value;
    296  1.1  joerg public:
    297  1.1  joerg     typedef typename ratio
    298  1.1  joerg         <
    299  1.1  joerg             __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,
    300  1.1  joerg             __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value
    301  1.1  joerg         >::type type;
    302  1.1  joerg };
    303  1.1  joerg 
    304  1.1  joerg #ifndef _LIBCPP_CXX03_LANG
    305  1.1  joerg 
    306  1.1  joerg template <class _R1, class _R2> using ratio_multiply
    307  1.1  joerg                                     = typename __ratio_multiply<_R1, _R2>::type;
    308  1.1  joerg 
    309  1.1  joerg #else  // _LIBCPP_CXX03_LANG
    310  1.1  joerg 
    311  1.1  joerg template <class _R1, class _R2>
    312  1.1  joerg struct _LIBCPP_TEMPLATE_VIS ratio_multiply
    313  1.1  joerg     : public __ratio_multiply<_R1, _R2>::type {};
    314  1.1  joerg 
    315  1.1  joerg #endif // _LIBCPP_CXX03_LANG
    316  1.1  joerg 
    317  1.1  joerg template <class _R1, class _R2>
    318  1.1  joerg struct __ratio_divide
    319  1.1  joerg {
    320  1.1  joerg private:
    321  1.1  joerg     static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
    322  1.1  joerg     static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
    323  1.1  joerg public:
    324  1.1  joerg     typedef typename ratio
    325  1.1  joerg         <
    326  1.1  joerg             __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
    327  1.1  joerg             __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
    328  1.1  joerg         >::type type;
    329  1.1  joerg };
    330  1.1  joerg 
    331  1.1  joerg #ifndef _LIBCPP_CXX03_LANG
    332  1.1  joerg 
    333  1.1  joerg template <class _R1, class _R2> using ratio_divide
    334  1.1  joerg                                       = typename __ratio_divide<_R1, _R2>::type;
    335  1.1  joerg 
    336  1.1  joerg #else  // _LIBCPP_CXX03_LANG
    337  1.1  joerg 
    338  1.1  joerg template <class _R1, class _R2>
    339  1.1  joerg struct _LIBCPP_TEMPLATE_VIS ratio_divide
    340  1.1  joerg     : public __ratio_divide<_R1, _R2>::type {};
    341  1.1  joerg 
    342  1.1  joerg #endif // _LIBCPP_CXX03_LANG
    343  1.1  joerg 
    344  1.1  joerg template <class _R1, class _R2>
    345  1.1  joerg struct __ratio_add
    346  1.1  joerg {
    347  1.1  joerg private:
    348  1.1  joerg     static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
    349  1.1  joerg     static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
    350  1.1  joerg public:
    351  1.1  joerg     typedef typename ratio_multiply
    352  1.1  joerg         <
    353  1.1  joerg             ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
    354  1.1  joerg             ratio
    355  1.1  joerg             <
    356  1.1  joerg                 __ll_add
    357  1.1  joerg                 <
    358  1.1  joerg                     __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
    359  1.1  joerg                     __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
    360  1.1  joerg                 >::value,
    361  1.1  joerg                 _R2::den
    362  1.1  joerg             >
    363  1.1  joerg         >::type type;
    364  1.1  joerg };
    365  1.1  joerg 
    366  1.1  joerg #ifndef _LIBCPP_CXX03_LANG
    367  1.1  joerg 
    368  1.1  joerg template <class _R1, class _R2> using ratio_add
    369  1.1  joerg                                          = typename __ratio_add<_R1, _R2>::type;
    370  1.1  joerg 
    371  1.1  joerg #else  // _LIBCPP_CXX03_LANG
    372  1.1  joerg 
    373  1.1  joerg template <class _R1, class _R2>
    374  1.1  joerg struct _LIBCPP_TEMPLATE_VIS ratio_add
    375  1.1  joerg     : public __ratio_add<_R1, _R2>::type {};
    376  1.1  joerg 
    377  1.1  joerg #endif // _LIBCPP_CXX03_LANG
    378  1.1  joerg 
    379  1.1  joerg template <class _R1, class _R2>
    380  1.1  joerg struct __ratio_subtract
    381  1.1  joerg {
    382  1.1  joerg private:
    383  1.1  joerg     static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
    384  1.1  joerg     static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
    385  1.1  joerg public:
    386  1.1  joerg     typedef typename ratio_multiply
    387  1.1  joerg         <
    388  1.1  joerg             ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
    389  1.1  joerg             ratio
    390  1.1  joerg             <
    391  1.1  joerg                 __ll_sub
    392  1.1  joerg                 <
    393  1.1  joerg                     __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
    394  1.1  joerg                     __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
    395  1.1  joerg                 >::value,
    396  1.1  joerg                 _R2::den
    397  1.1  joerg             >
    398  1.1  joerg         >::type type;
    399  1.1  joerg };
    400  1.1  joerg 
    401  1.1  joerg #ifndef _LIBCPP_CXX03_LANG
    402  1.1  joerg 
    403  1.1  joerg template <class _R1, class _R2> using ratio_subtract
    404  1.1  joerg                                     = typename __ratio_subtract<_R1, _R2>::type;
    405  1.1  joerg 
    406  1.1  joerg #else  // _LIBCPP_CXX03_LANG
    407  1.1  joerg 
    408  1.1  joerg template <class _R1, class _R2>
    409  1.1  joerg struct _LIBCPP_TEMPLATE_VIS ratio_subtract
    410  1.1  joerg     : public __ratio_subtract<_R1, _R2>::type {};
    411  1.1  joerg 
    412  1.1  joerg #endif // _LIBCPP_CXX03_LANG
    413  1.1  joerg 
    414  1.1  joerg // ratio_equal
    415  1.1  joerg 
    416  1.1  joerg template <class _R1, class _R2>
    417  1.1  joerg struct _LIBCPP_TEMPLATE_VIS ratio_equal
    418  1.1  joerg     : public _LIBCPP_BOOL_CONSTANT((_R1::num == _R2::num && _R1::den == _R2::den)) {};
    419  1.1  joerg 
    420  1.1  joerg template <class _R1, class _R2>
    421  1.1  joerg struct _LIBCPP_TEMPLATE_VIS ratio_not_equal
    422  1.1  joerg     : public _LIBCPP_BOOL_CONSTANT((!ratio_equal<_R1, _R2>::value)) {};
    423  1.1  joerg 
    424  1.1  joerg // ratio_less
    425  1.1  joerg 
    426  1.1  joerg template <class _R1, class _R2, bool _Odd = false,
    427  1.1  joerg           intmax_t _Q1 = _R1::num / _R1::den, intmax_t _M1 = _R1::num % _R1::den,
    428  1.1  joerg           intmax_t _Q2 = _R2::num / _R2::den, intmax_t _M2 = _R2::num % _R2::den>
    429  1.1  joerg struct __ratio_less1
    430  1.1  joerg {
    431  1.1  joerg     static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
    432  1.1  joerg };
    433  1.1  joerg 
    434  1.1  joerg template <class _R1, class _R2, bool _Odd, intmax_t _Qp>
    435  1.1  joerg struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0>
    436  1.1  joerg {
    437  1.1  joerg     static const bool value = false;
    438  1.1  joerg };
    439  1.1  joerg 
    440  1.1  joerg template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>
    441  1.1  joerg struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2>
    442  1.1  joerg {
    443  1.1  joerg     static const bool value = !_Odd;
    444  1.1  joerg };
    445  1.1  joerg 
    446  1.1  joerg template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1>
    447  1.1  joerg struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0>
    448  1.1  joerg {
    449  1.1  joerg     static const bool value = _Odd;
    450  1.1  joerg };
    451  1.1  joerg 
    452  1.1  joerg template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1,
    453  1.1  joerg                                                         intmax_t _M2>
    454  1.1  joerg struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2>
    455  1.1  joerg {
    456  1.1  joerg     static const bool value = __ratio_less1<ratio<_R1::den, _M1>,
    457  1.1  joerg                                             ratio<_R2::den, _M2>, !_Odd>::value;
    458  1.1  joerg };
    459  1.1  joerg 
    460  1.1  joerg template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>::value,
    461  1.1  joerg                                 intmax_t _S2 = __static_sign<_R2::num>::value>
    462  1.1  joerg struct __ratio_less
    463  1.1  joerg {
    464  1.1  joerg     static const bool value = _S1 < _S2;
    465  1.1  joerg };
    466  1.1  joerg 
    467  1.1  joerg template <class _R1, class _R2>
    468  1.1  joerg struct __ratio_less<_R1, _R2, 1LL, 1LL>
    469  1.1  joerg {
    470  1.1  joerg     static const bool value = __ratio_less1<_R1, _R2>::value;
    471  1.1  joerg };
    472  1.1  joerg 
    473  1.1  joerg template <class _R1, class _R2>
    474  1.1  joerg struct __ratio_less<_R1, _R2, -1LL, -1LL>
    475  1.1  joerg {
    476  1.1  joerg     static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
    477  1.1  joerg };
    478  1.1  joerg 
    479  1.1  joerg template <class _R1, class _R2>
    480  1.1  joerg struct _LIBCPP_TEMPLATE_VIS ratio_less
    481  1.1  joerg     : public _LIBCPP_BOOL_CONSTANT((__ratio_less<_R1, _R2>::value)) {};
    482  1.1  joerg 
    483  1.1  joerg template <class _R1, class _R2>
    484  1.1  joerg struct _LIBCPP_TEMPLATE_VIS ratio_less_equal
    485  1.1  joerg     : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R2, _R1>::value)) {};
    486  1.1  joerg 
    487  1.1  joerg template <class _R1, class _R2>
    488  1.1  joerg struct _LIBCPP_TEMPLATE_VIS ratio_greater
    489  1.1  joerg     : public _LIBCPP_BOOL_CONSTANT((ratio_less<_R2, _R1>::value)) {};
    490  1.1  joerg 
    491  1.1  joerg template <class _R1, class _R2>
    492  1.1  joerg struct _LIBCPP_TEMPLATE_VIS ratio_greater_equal
    493  1.1  joerg     : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R1, _R2>::value)) {};
    494  1.1  joerg 
    495  1.1  joerg template <class _R1, class _R2>
    496  1.1  joerg struct __ratio_gcd
    497  1.1  joerg {
    498  1.1  joerg     typedef ratio<__static_gcd<_R1::num, _R2::num>::value,
    499  1.1  joerg                   __static_lcm<_R1::den, _R2::den>::value> type;
    500  1.1  joerg };
    501  1.1  joerg 
    502  1.1  joerg #if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES)
    503  1.1  joerg template <class _R1, class _R2>
    504  1.1  joerg _LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_equal_v
    505  1.1  joerg     = ratio_equal<_R1, _R2>::value;
    506  1.1  joerg 
    507  1.1  joerg template <class _R1, class _R2>
    508  1.1  joerg _LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_not_equal_v
    509  1.1  joerg     = ratio_not_equal<_R1, _R2>::value;
    510  1.1  joerg 
    511  1.1  joerg template <class _R1, class _R2>
    512  1.1  joerg _LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_less_v
    513  1.1  joerg     = ratio_less<_R1, _R2>::value;
    514  1.1  joerg 
    515  1.1  joerg template <class _R1, class _R2>
    516  1.1  joerg _LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_less_equal_v
    517  1.1  joerg     = ratio_less_equal<_R1, _R2>::value;
    518  1.1  joerg 
    519  1.1  joerg template <class _R1, class _R2>
    520  1.1  joerg _LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_greater_v
    521  1.1  joerg     = ratio_greater<_R1, _R2>::value;
    522  1.1  joerg 
    523  1.1  joerg template <class _R1, class _R2>
    524  1.1  joerg _LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_greater_equal_v
    525  1.1  joerg     = ratio_greater_equal<_R1, _R2>::value;
    526  1.1  joerg #endif
    527  1.1  joerg 
    528  1.1  joerg _LIBCPP_END_NAMESPACE_STD
    529  1.1  joerg 
    530  1.1  joerg _LIBCPP_POP_MACROS
    531  1.1  joerg 
    532  1.1  joerg #endif // _LIBCPP_RATIO
    533