Home | History | Annotate | Line # | Download | only in std
      1       1.1  mrg // ratio -*- C++ -*-
      2       1.1  mrg 
      3  1.1.1.14  mrg // Copyright (C) 2008-2024 Free Software Foundation, Inc.
      4       1.1  mrg //
      5       1.1  mrg // This file is part of the GNU ISO C++ Library.  This library is free
      6   1.1.1.5  mrg // software; you can redistribute it and/or modify it under the
      7   1.1.1.5  mrg // terms of the GNU General Public License as published by the
      8       1.1  mrg // Free Software Foundation; either version 3, or (at your option)
      9       1.1  mrg // any later version.
     10       1.1  mrg 
     11       1.1  mrg // This library is distributed in the hope that it will be useful,
     12       1.1  mrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13   1.1.1.5  mrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14       1.1  mrg // GNU General Public License for more details.
     15       1.1  mrg 
     16       1.1  mrg // Under Section 7 of GPL version 3, you are granted additional
     17       1.1  mrg // permissions described in the GCC Runtime Library Exception, version
     18       1.1  mrg // 3.1, as published by the Free Software Foundation.
     19       1.1  mrg 
     20       1.1  mrg // You should have received a copy of the GNU General Public License and
     21       1.1  mrg // a copy of the GCC Runtime Library Exception along with this program;
     22       1.1  mrg // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     23       1.1  mrg // <http://www.gnu.org/licenses/>.
     24       1.1  mrg 
     25   1.1.1.2  mrg /** @file include/ratio
     26       1.1  mrg  *  This is a Standard C++ Library header.
     27  1.1.1.10  mrg  *  @ingroup ratio
     28       1.1  mrg  */
     29       1.1  mrg 
     30       1.1  mrg #ifndef _GLIBCXX_RATIO
     31       1.1  mrg #define _GLIBCXX_RATIO 1
     32       1.1  mrg 
     33       1.1  mrg #pragma GCC system_header
     34       1.1  mrg 
     35   1.1.1.2  mrg #if __cplusplus < 201103L
     36       1.1  mrg # include <bits/c++0x_warning.h>
     37       1.1  mrg #else
     38       1.1  mrg 
     39       1.1  mrg #include <type_traits>
     40   1.1.1.9  mrg #include <cstdint>		// intmax_t, uintmax_t
     41       1.1  mrg 
     42  1.1.1.14  mrg #define __glibcxx_want_ratio
     43  1.1.1.14  mrg #include <bits/version.h>
     44  1.1.1.14  mrg 
     45   1.1.1.2  mrg namespace std _GLIBCXX_VISIBILITY(default)
     46       1.1  mrg {
     47   1.1.1.2  mrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
     48   1.1.1.2  mrg 
     49       1.1  mrg   /**
     50       1.1  mrg    * @defgroup ratio Rational Arithmetic
     51       1.1  mrg    * @ingroup utilities
     52       1.1  mrg    *
     53       1.1  mrg    * Compile time representation of finite rational numbers.
     54       1.1  mrg    * @{
     55       1.1  mrg    */
     56       1.1  mrg 
     57  1.1.1.10  mrg   /// @cond undocumented
     58  1.1.1.10  mrg 
     59       1.1  mrg   template<intmax_t _Pn>
     60       1.1  mrg     struct __static_sign
     61       1.1  mrg     : integral_constant<intmax_t, (_Pn < 0) ? -1 : 1>
     62       1.1  mrg     { };
     63       1.1  mrg 
     64       1.1  mrg   template<intmax_t _Pn>
     65       1.1  mrg     struct __static_abs
     66       1.1  mrg     : integral_constant<intmax_t, _Pn * __static_sign<_Pn>::value>
     67       1.1  mrg     { };
     68       1.1  mrg 
     69       1.1  mrg   template<intmax_t _Pn, intmax_t _Qn>
     70       1.1  mrg     struct __static_gcd
     71       1.1  mrg     : __static_gcd<_Qn, (_Pn % _Qn)>
     72       1.1  mrg     { };
     73       1.1  mrg 
     74       1.1  mrg   template<intmax_t _Pn>
     75       1.1  mrg     struct __static_gcd<_Pn, 0>
     76       1.1  mrg     : integral_constant<intmax_t, __static_abs<_Pn>::value>
     77       1.1  mrg     { };
     78       1.1  mrg 
     79       1.1  mrg   template<intmax_t _Qn>
     80       1.1  mrg     struct __static_gcd<0, _Qn>
     81       1.1  mrg     : integral_constant<intmax_t, __static_abs<_Qn>::value>
     82       1.1  mrg     { };
     83       1.1  mrg 
     84       1.1  mrg   // Let c = 2^(half # of bits in an intmax_t)
     85       1.1  mrg   // then we find a1, a0, b1, b0 s.t. N = a1*c + a0, M = b1*c + b0
     86       1.1  mrg   // The multiplication of N and M becomes,
     87       1.1  mrg   // N * M = (a1 * b1)c^2 + (a0 * b1 + b0 * a1)c + a0 * b0
     88       1.1  mrg   // Multiplication is safe if each term and the sum of the terms
     89       1.1  mrg   // is representable by intmax_t.
     90       1.1  mrg   template<intmax_t _Pn, intmax_t _Qn>
     91       1.1  mrg     struct __safe_multiply
     92       1.1  mrg     {
     93       1.1  mrg     private:
     94       1.1  mrg       static const uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
     95       1.1  mrg 
     96       1.1  mrg       static const uintmax_t __a0 = __static_abs<_Pn>::value % __c;
     97       1.1  mrg       static const uintmax_t __a1 = __static_abs<_Pn>::value / __c;
     98       1.1  mrg       static const uintmax_t __b0 = __static_abs<_Qn>::value % __c;
     99       1.1  mrg       static const uintmax_t __b1 = __static_abs<_Qn>::value / __c;
    100       1.1  mrg 
    101   1.1.1.5  mrg       static_assert(__a1 == 0 || __b1 == 0,
    102   1.1.1.2  mrg 		    "overflow in multiplication");
    103   1.1.1.5  mrg       static_assert(__a0 * __b1 + __b0 * __a1 < (__c >> 1),
    104   1.1.1.2  mrg 		    "overflow in multiplication");
    105   1.1.1.5  mrg       static_assert(__b0 * __a0 <= __INTMAX_MAX__,
    106   1.1.1.2  mrg 		    "overflow in multiplication");
    107   1.1.1.2  mrg       static_assert((__a0 * __b1 + __b0 * __a1) * __c
    108   1.1.1.2  mrg 		    <= __INTMAX_MAX__ -  __b0 * __a0,
    109   1.1.1.2  mrg 		    "overflow in multiplication");
    110       1.1  mrg 
    111       1.1  mrg     public:
    112       1.1  mrg       static const intmax_t value = _Pn * _Qn;
    113       1.1  mrg     };
    114       1.1  mrg 
    115   1.1.1.2  mrg   // Some double-precision utilities, where numbers are represented as
    116   1.1.1.2  mrg   // __hi*2^(8*sizeof(uintmax_t)) + __lo.
    117   1.1.1.2  mrg   template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2>
    118   1.1.1.2  mrg     struct __big_less
    119   1.1.1.2  mrg     : integral_constant<bool, (__hi1 < __hi2
    120   1.1.1.2  mrg 			       || (__hi1 == __hi2 && __lo1 < __lo2))>
    121       1.1  mrg     { };
    122       1.1  mrg 
    123   1.1.1.2  mrg   template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2>
    124   1.1.1.2  mrg     struct __big_add
    125   1.1.1.2  mrg     {
    126   1.1.1.2  mrg       static constexpr uintmax_t __lo = __lo1 + __lo2;
    127   1.1.1.2  mrg       static constexpr uintmax_t __hi = (__hi1 + __hi2 +
    128   1.1.1.2  mrg 					 (__lo1 + __lo2 < __lo1)); // carry
    129   1.1.1.2  mrg     };
    130       1.1  mrg 
    131   1.1.1.2  mrg   // Subtract a number from a bigger one.
    132   1.1.1.2  mrg   template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2>
    133   1.1.1.2  mrg     struct __big_sub
    134   1.1.1.2  mrg     {
    135   1.1.1.2  mrg       static_assert(!__big_less<__hi1, __lo1, __hi2, __lo2>::value,
    136   1.1.1.2  mrg 		    "Internal library error");
    137   1.1.1.2  mrg       static constexpr uintmax_t __lo = __lo1 - __lo2;
    138   1.1.1.2  mrg       static constexpr uintmax_t __hi = (__hi1 - __hi2 -
    139   1.1.1.2  mrg 					 (__lo1 < __lo2)); // carry
    140   1.1.1.2  mrg     };
    141       1.1  mrg 
    142   1.1.1.2  mrg   // Same principle as __safe_multiply.
    143   1.1.1.2  mrg   template<uintmax_t __x, uintmax_t __y>
    144   1.1.1.2  mrg     struct __big_mul
    145   1.1.1.2  mrg     {
    146   1.1.1.2  mrg     private:
    147   1.1.1.2  mrg       static constexpr uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
    148   1.1.1.2  mrg       static constexpr uintmax_t __x0 = __x % __c;
    149   1.1.1.2  mrg       static constexpr uintmax_t __x1 = __x / __c;
    150   1.1.1.2  mrg       static constexpr uintmax_t __y0 = __y % __c;
    151   1.1.1.2  mrg       static constexpr uintmax_t __y1 = __y / __c;
    152   1.1.1.2  mrg       static constexpr uintmax_t __x0y0 = __x0 * __y0;
    153   1.1.1.2  mrg       static constexpr uintmax_t __x0y1 = __x0 * __y1;
    154   1.1.1.2  mrg       static constexpr uintmax_t __x1y0 = __x1 * __y0;
    155   1.1.1.2  mrg       static constexpr uintmax_t __x1y1 = __x1 * __y1;
    156   1.1.1.2  mrg       static constexpr uintmax_t __mix = __x0y1 + __x1y0; // possible carry...
    157   1.1.1.2  mrg       static constexpr uintmax_t __mix_lo = __mix * __c;
    158   1.1.1.2  mrg       static constexpr uintmax_t __mix_hi
    159   1.1.1.2  mrg       = __mix / __c + ((__mix < __x0y1) ? __c : 0); // ... added here
    160   1.1.1.2  mrg       typedef __big_add<__mix_hi, __mix_lo, __x1y1, __x0y0> _Res;
    161   1.1.1.2  mrg     public:
    162   1.1.1.2  mrg       static constexpr uintmax_t __hi = _Res::__hi;
    163   1.1.1.2  mrg       static constexpr uintmax_t __lo = _Res::__lo;
    164   1.1.1.2  mrg     };
    165   1.1.1.2  mrg 
    166   1.1.1.2  mrg   // Adapted from __udiv_qrnnd_c in longlong.h
    167   1.1.1.2  mrg   // This version assumes that the high bit of __d is 1.
    168   1.1.1.2  mrg   template<uintmax_t __n1, uintmax_t __n0, uintmax_t __d>
    169   1.1.1.2  mrg     struct __big_div_impl
    170       1.1  mrg     {
    171   1.1.1.2  mrg     private:
    172   1.1.1.2  mrg       static_assert(__d >= (uintmax_t(1) << (sizeof(intmax_t) * 8 - 1)),
    173   1.1.1.2  mrg 		    "Internal library error");
    174   1.1.1.2  mrg       static_assert(__n1 < __d, "Internal library error");
    175   1.1.1.2  mrg       static constexpr uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
    176   1.1.1.2  mrg       static constexpr uintmax_t __d1 = __d / __c;
    177   1.1.1.2  mrg       static constexpr uintmax_t __d0 = __d % __c;
    178   1.1.1.2  mrg 
    179   1.1.1.2  mrg       static constexpr uintmax_t __q1x = __n1 / __d1;
    180   1.1.1.2  mrg       static constexpr uintmax_t __r1x = __n1 % __d1;
    181   1.1.1.2  mrg       static constexpr uintmax_t __m = __q1x * __d0;
    182   1.1.1.2  mrg       static constexpr uintmax_t __r1y = __r1x * __c + __n0 / __c;
    183   1.1.1.2  mrg       static constexpr uintmax_t __r1z = __r1y + __d;
    184   1.1.1.2  mrg       static constexpr uintmax_t __r1
    185   1.1.1.2  mrg       = ((__r1y < __m) ? ((__r1z >= __d) && (__r1z < __m))
    186   1.1.1.2  mrg 	 ? (__r1z + __d) : __r1z : __r1y) - __m;
    187   1.1.1.2  mrg       static constexpr uintmax_t __q1
    188   1.1.1.2  mrg       = __q1x - ((__r1y < __m)
    189   1.1.1.2  mrg 		 ? ((__r1z >= __d) && (__r1z < __m)) ? 2 : 1 : 0);
    190   1.1.1.2  mrg       static constexpr uintmax_t __q0x = __r1 / __d1;
    191   1.1.1.2  mrg       static constexpr uintmax_t __r0x = __r1 % __d1;
    192   1.1.1.2  mrg       static constexpr uintmax_t __n = __q0x * __d0;
    193   1.1.1.2  mrg       static constexpr uintmax_t __r0y = __r0x * __c + __n0 % __c;
    194   1.1.1.2  mrg       static constexpr uintmax_t __r0z = __r0y + __d;
    195   1.1.1.2  mrg       static constexpr uintmax_t __r0
    196   1.1.1.2  mrg       = ((__r0y < __n) ? ((__r0z >= __d) && (__r0z < __n))
    197   1.1.1.2  mrg 	 ? (__r0z + __d) : __r0z : __r0y) - __n;
    198   1.1.1.2  mrg       static constexpr uintmax_t __q0
    199   1.1.1.2  mrg       = __q0x - ((__r0y < __n) ? ((__r0z >= __d)
    200   1.1.1.2  mrg 				  && (__r0z < __n)) ? 2 : 1 : 0);
    201   1.1.1.2  mrg 
    202   1.1.1.2  mrg     public:
    203   1.1.1.2  mrg       static constexpr uintmax_t __quot = __q1 * __c + __q0;
    204   1.1.1.2  mrg       static constexpr uintmax_t __rem = __r0;
    205       1.1  mrg 
    206   1.1.1.2  mrg     private:
    207   1.1.1.2  mrg       typedef __big_mul<__quot, __d> _Prod;
    208   1.1.1.2  mrg       typedef __big_add<_Prod::__hi, _Prod::__lo, 0, __rem> _Sum;
    209   1.1.1.2  mrg       static_assert(_Sum::__hi == __n1 && _Sum::__lo == __n0,
    210   1.1.1.2  mrg 		    "Internal library error");
    211   1.1.1.2  mrg   };
    212   1.1.1.2  mrg 
    213   1.1.1.2  mrg   template<uintmax_t __n1, uintmax_t __n0, uintmax_t __d>
    214   1.1.1.2  mrg     struct __big_div
    215   1.1.1.2  mrg     {
    216   1.1.1.2  mrg     private:
    217   1.1.1.2  mrg       static_assert(__d != 0, "Internal library error");
    218   1.1.1.2  mrg       static_assert(sizeof (uintmax_t) == sizeof (unsigned long long),
    219   1.1.1.2  mrg 		    "This library calls __builtin_clzll on uintmax_t, which "
    220   1.1.1.2  mrg 		    "is unsafe on your platform. Please complain to "
    221   1.1.1.2  mrg 		    "http://gcc.gnu.org/bugzilla/");
    222   1.1.1.2  mrg       static constexpr int __shift = __builtin_clzll(__d);
    223   1.1.1.2  mrg       static constexpr int __coshift_ = sizeof(uintmax_t) * 8 - __shift;
    224   1.1.1.2  mrg       static constexpr int __coshift = (__shift != 0) ? __coshift_ : 0;
    225   1.1.1.2  mrg       static constexpr uintmax_t __c1 = uintmax_t(1) << __shift;
    226   1.1.1.2  mrg       static constexpr uintmax_t __c2 = uintmax_t(1) << __coshift;
    227   1.1.1.2  mrg       static constexpr uintmax_t __new_d = __d * __c1;
    228   1.1.1.2  mrg       static constexpr uintmax_t __new_n0 = __n0 * __c1;
    229   1.1.1.2  mrg       static constexpr uintmax_t __n1_shifted = (__n1 % __d) * __c1;
    230   1.1.1.2  mrg       static constexpr uintmax_t __n0_top = (__shift != 0) ? (__n0 / __c2) : 0;
    231   1.1.1.2  mrg       static constexpr uintmax_t __new_n1 = __n1_shifted + __n0_top;
    232   1.1.1.2  mrg       typedef __big_div_impl<__new_n1, __new_n0, __new_d> _Res;
    233   1.1.1.2  mrg 
    234   1.1.1.2  mrg     public:
    235   1.1.1.2  mrg       static constexpr uintmax_t __quot_hi = __n1 / __d;
    236   1.1.1.2  mrg       static constexpr uintmax_t __quot_lo = _Res::__quot;
    237   1.1.1.2  mrg       static constexpr uintmax_t __rem = _Res::__rem / __c1;
    238   1.1.1.2  mrg 
    239   1.1.1.2  mrg     private:
    240   1.1.1.2  mrg       typedef __big_mul<__quot_lo, __d> _P0;
    241   1.1.1.2  mrg       typedef __big_mul<__quot_hi, __d> _P1;
    242   1.1.1.2  mrg       typedef __big_add<_P0::__hi, _P0::__lo, _P1::__lo, __rem> _Sum;
    243   1.1.1.2  mrg       // No overflow.
    244   1.1.1.2  mrg       static_assert(_P1::__hi == 0, "Internal library error");
    245   1.1.1.2  mrg       static_assert(_Sum::__hi >= _P0::__hi, "Internal library error");
    246   1.1.1.2  mrg       // Matches the input data.
    247   1.1.1.2  mrg       static_assert(_Sum::__hi == __n1 && _Sum::__lo == __n0,
    248   1.1.1.2  mrg 		    "Internal library error");
    249   1.1.1.2  mrg       static_assert(__rem < __d, "Internal library error");
    250       1.1  mrg     };
    251       1.1  mrg 
    252  1.1.1.10  mrg   /// @endcond
    253  1.1.1.10  mrg 
    254       1.1  mrg   /**
    255       1.1  mrg    *  @brief Provides compile-time rational arithmetic.
    256       1.1  mrg    *
    257       1.1  mrg    *  This class template represents any finite rational number with a
    258       1.1  mrg    *  numerator and denominator representable by compile-time constants of
    259       1.1  mrg    *  type intmax_t. The ratio is simplified when instantiated.
    260       1.1  mrg    *
    261       1.1  mrg    *  For example:
    262       1.1  mrg    *  @code
    263       1.1  mrg    *    std::ratio<7,-21>::num == -1;
    264       1.1  mrg    *    std::ratio<7,-21>::den == 3;
    265       1.1  mrg    *  @endcode
    266   1.1.1.5  mrg    *
    267       1.1  mrg   */
    268       1.1  mrg   template<intmax_t _Num, intmax_t _Den = 1>
    269       1.1  mrg     struct ratio
    270       1.1  mrg     {
    271       1.1  mrg       static_assert(_Den != 0, "denominator cannot be zero");
    272       1.1  mrg       static_assert(_Num >= -__INTMAX_MAX__ && _Den >= -__INTMAX_MAX__,
    273       1.1  mrg 		    "out of range");
    274       1.1  mrg 
    275       1.1  mrg       // Note: sign(N) * abs(N) == N
    276   1.1.1.2  mrg       static constexpr intmax_t num =
    277       1.1  mrg         _Num * __static_sign<_Den>::value / __static_gcd<_Num, _Den>::value;
    278       1.1  mrg 
    279   1.1.1.2  mrg       static constexpr intmax_t den =
    280       1.1  mrg         __static_abs<_Den>::value / __static_gcd<_Num, _Den>::value;
    281   1.1.1.2  mrg 
    282   1.1.1.2  mrg       typedef ratio<num, den> type;
    283       1.1  mrg     };
    284       1.1  mrg 
    285  1.1.1.12  mrg #if ! __cpp_inline_variables
    286       1.1  mrg   template<intmax_t _Num, intmax_t _Den>
    287   1.1.1.2  mrg     constexpr intmax_t ratio<_Num, _Den>::num;
    288       1.1  mrg 
    289       1.1  mrg   template<intmax_t _Num, intmax_t _Den>
    290   1.1.1.2  mrg     constexpr intmax_t ratio<_Num, _Den>::den;
    291  1.1.1.12  mrg #endif
    292       1.1  mrg 
    293  1.1.1.10  mrg   /// @cond undocumented
    294  1.1.1.10  mrg 
    295  1.1.1.13  mrg   template<typename _Tp>
    296  1.1.1.13  mrg     struct __is_ratio
    297  1.1.1.13  mrg     : std::false_type
    298  1.1.1.13  mrg     { };
    299  1.1.1.13  mrg 
    300  1.1.1.13  mrg   template<intmax_t _Num, intmax_t _Den>
    301  1.1.1.13  mrg     struct __is_ratio<ratio<_Num, _Den>>
    302  1.1.1.13  mrg     : std::true_type
    303  1.1.1.13  mrg     { };
    304  1.1.1.13  mrg 
    305  1.1.1.13  mrg #if __cpp_variable_templates
    306  1.1.1.13  mrg   template<typename _Tp>
    307  1.1.1.13  mrg     constexpr bool __is_ratio_v = false;
    308  1.1.1.13  mrg   template<intmax_t _Num, intmax_t _Den>
    309  1.1.1.13  mrg     constexpr bool __is_ratio_v<ratio<_Num, _Den>> = true;
    310  1.1.1.13  mrg #endif
    311  1.1.1.13  mrg 
    312  1.1.1.13  mrg   template<typename _R1, typename _R2>
    313  1.1.1.13  mrg     constexpr bool
    314  1.1.1.13  mrg     __are_both_ratios() noexcept
    315  1.1.1.13  mrg     {
    316  1.1.1.13  mrg #if __cpp_variable_templates && __cpp_if_constexpr
    317  1.1.1.13  mrg       if constexpr (__is_ratio_v<_R1>)
    318  1.1.1.13  mrg 	if constexpr (__is_ratio_v<_R2>)
    319  1.1.1.13  mrg 	  return true;
    320  1.1.1.13  mrg       return false;
    321  1.1.1.13  mrg #else
    322  1.1.1.13  mrg       return __and_<__is_ratio<_R1>, __is_ratio<_R2>>::value;
    323  1.1.1.13  mrg #endif
    324  1.1.1.13  mrg     }
    325  1.1.1.13  mrg 
    326       1.1  mrg   template<typename _R1, typename _R2>
    327   1.1.1.2  mrg     struct __ratio_multiply
    328       1.1  mrg     {
    329  1.1.1.13  mrg       static_assert(std::__are_both_ratios<_R1, _R2>(),
    330  1.1.1.13  mrg 		    "both template arguments must be a std::ratio");
    331  1.1.1.13  mrg 
    332       1.1  mrg     private:
    333       1.1  mrg       static const intmax_t __gcd1 =
    334       1.1  mrg         __static_gcd<_R1::num, _R2::den>::value;
    335       1.1  mrg       static const intmax_t __gcd2 =
    336       1.1  mrg         __static_gcd<_R2::num, _R1::den>::value;
    337       1.1  mrg 
    338       1.1  mrg     public:
    339       1.1  mrg       typedef ratio<
    340       1.1  mrg         __safe_multiply<(_R1::num / __gcd1),
    341       1.1  mrg                         (_R2::num / __gcd2)>::value,
    342       1.1  mrg         __safe_multiply<(_R1::den / __gcd2),
    343       1.1  mrg                         (_R2::den / __gcd1)>::value> type;
    344   1.1.1.2  mrg 
    345   1.1.1.2  mrg       static constexpr intmax_t num = type::num;
    346   1.1.1.2  mrg       static constexpr intmax_t den = type::den;
    347       1.1  mrg     };
    348       1.1  mrg 
    349  1.1.1.12  mrg #if ! __cpp_inline_variables
    350       1.1  mrg   template<typename _R1, typename _R2>
    351   1.1.1.2  mrg     constexpr intmax_t __ratio_multiply<_R1, _R2>::num;
    352   1.1.1.2  mrg 
    353   1.1.1.2  mrg   template<typename _R1, typename _R2>
    354   1.1.1.2  mrg     constexpr intmax_t __ratio_multiply<_R1, _R2>::den;
    355  1.1.1.12  mrg #endif
    356   1.1.1.2  mrg 
    357  1.1.1.10  mrg   /// @endcond
    358  1.1.1.10  mrg 
    359   1.1.1.2  mrg   /// ratio_multiply
    360   1.1.1.2  mrg   template<typename _R1, typename _R2>
    361   1.1.1.2  mrg     using ratio_multiply = typename __ratio_multiply<_R1, _R2>::type;
    362   1.1.1.2  mrg 
    363  1.1.1.10  mrg   /// @cond undocumented
    364  1.1.1.10  mrg 
    365   1.1.1.2  mrg   template<typename _R1, typename _R2>
    366   1.1.1.2  mrg     struct __ratio_divide
    367       1.1  mrg     {
    368       1.1  mrg       static_assert(_R2::num != 0, "division by 0");
    369       1.1  mrg 
    370   1.1.1.2  mrg       typedef typename __ratio_multiply<
    371       1.1  mrg         _R1,
    372       1.1  mrg         ratio<_R2::den, _R2::num>>::type type;
    373   1.1.1.2  mrg 
    374   1.1.1.2  mrg       static constexpr intmax_t num = type::num;
    375   1.1.1.2  mrg       static constexpr intmax_t den = type::den;
    376       1.1  mrg     };
    377       1.1  mrg 
    378  1.1.1.12  mrg #if ! __cpp_inline_variables
    379   1.1.1.2  mrg   template<typename _R1, typename _R2>
    380   1.1.1.2  mrg     constexpr intmax_t __ratio_divide<_R1, _R2>::num;
    381   1.1.1.2  mrg 
    382   1.1.1.2  mrg   template<typename _R1, typename _R2>
    383   1.1.1.2  mrg     constexpr intmax_t __ratio_divide<_R1, _R2>::den;
    384  1.1.1.12  mrg #endif
    385   1.1.1.2  mrg 
    386  1.1.1.10  mrg   /// @endcond
    387  1.1.1.10  mrg 
    388   1.1.1.2  mrg   /// ratio_divide
    389   1.1.1.2  mrg   template<typename _R1, typename _R2>
    390   1.1.1.2  mrg     using ratio_divide = typename __ratio_divide<_R1, _R2>::type;
    391   1.1.1.2  mrg 
    392       1.1  mrg   /// ratio_equal
    393       1.1  mrg   template<typename _R1, typename _R2>
    394       1.1  mrg     struct ratio_equal
    395       1.1  mrg     : integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den>
    396  1.1.1.13  mrg     {
    397  1.1.1.13  mrg       static_assert(std::__are_both_ratios<_R1, _R2>(),
    398  1.1.1.13  mrg 		    "both template arguments must be a std::ratio");
    399  1.1.1.13  mrg     };
    400   1.1.1.5  mrg 
    401       1.1  mrg   /// ratio_not_equal
    402       1.1  mrg   template<typename _R1, typename _R2>
    403       1.1  mrg     struct ratio_not_equal
    404       1.1  mrg     : integral_constant<bool, !ratio_equal<_R1, _R2>::value>
    405       1.1  mrg     { };
    406   1.1.1.2  mrg 
    407  1.1.1.10  mrg   /// @cond undocumented
    408  1.1.1.10  mrg 
    409   1.1.1.2  mrg   // Both numbers are positive.
    410   1.1.1.2  mrg   template<typename _R1, typename _R2,
    411   1.1.1.2  mrg            typename _Left = __big_mul<_R1::num,_R2::den>,
    412   1.1.1.2  mrg            typename _Right = __big_mul<_R2::num,_R1::den> >
    413   1.1.1.2  mrg     struct __ratio_less_impl_1
    414   1.1.1.2  mrg     : integral_constant<bool, __big_less<_Left::__hi, _Left::__lo,
    415   1.1.1.2  mrg            _Right::__hi, _Right::__lo>::value>
    416   1.1.1.5  mrg     { };
    417   1.1.1.2  mrg 
    418   1.1.1.2  mrg   template<typename _R1, typename _R2,
    419   1.1.1.2  mrg 	   bool = (_R1::num == 0 || _R2::num == 0
    420   1.1.1.2  mrg 		   || (__static_sign<_R1::num>::value
    421   1.1.1.2  mrg 		       != __static_sign<_R2::num>::value)),
    422   1.1.1.2  mrg 	   bool = (__static_sign<_R1::num>::value == -1
    423   1.1.1.2  mrg 		   && __static_sign<_R2::num>::value == -1)>
    424   1.1.1.2  mrg     struct __ratio_less_impl
    425   1.1.1.2  mrg     : __ratio_less_impl_1<_R1, _R2>::type
    426   1.1.1.2  mrg     { };
    427   1.1.1.2  mrg 
    428       1.1  mrg   template<typename _R1, typename _R2>
    429   1.1.1.2  mrg     struct __ratio_less_impl<_R1, _R2, true, false>
    430   1.1.1.2  mrg     : integral_constant<bool, _R1::num < _R2::num>
    431       1.1  mrg     { };
    432       1.1  mrg 
    433       1.1  mrg   template<typename _R1, typename _R2>
    434   1.1.1.2  mrg     struct __ratio_less_impl<_R1, _R2, false, true>
    435   1.1.1.2  mrg     : __ratio_less_impl_1<ratio<-_R2::num, _R2::den>,
    436   1.1.1.2  mrg            ratio<-_R1::num, _R1::den> >::type
    437       1.1  mrg     { };
    438       1.1  mrg 
    439  1.1.1.10  mrg   /// @endcond
    440  1.1.1.10  mrg 
    441       1.1  mrg   /// ratio_less
    442       1.1  mrg   template<typename _R1, typename _R2>
    443       1.1  mrg     struct ratio_less
    444       1.1  mrg     : __ratio_less_impl<_R1, _R2>::type
    445  1.1.1.13  mrg     {
    446  1.1.1.13  mrg       static_assert(std::__are_both_ratios<_R1, _R2>(),
    447  1.1.1.13  mrg 		    "both template arguments must be a std::ratio");
    448  1.1.1.13  mrg     };
    449   1.1.1.5  mrg 
    450       1.1  mrg   /// ratio_less_equal
    451       1.1  mrg   template<typename _R1, typename _R2>
    452       1.1  mrg     struct ratio_less_equal
    453       1.1  mrg     : integral_constant<bool, !ratio_less<_R2, _R1>::value>
    454       1.1  mrg     { };
    455   1.1.1.5  mrg 
    456       1.1  mrg   /// ratio_greater
    457       1.1  mrg   template<typename _R1, typename _R2>
    458       1.1  mrg     struct ratio_greater
    459       1.1  mrg     : integral_constant<bool, ratio_less<_R2, _R1>::value>
    460       1.1  mrg     { };
    461       1.1  mrg 
    462       1.1  mrg   /// ratio_greater_equal
    463       1.1  mrg   template<typename _R1, typename _R2>
    464       1.1  mrg     struct ratio_greater_equal
    465       1.1  mrg     : integral_constant<bool, !ratio_less<_R1, _R2>::value>
    466       1.1  mrg     { };
    467       1.1  mrg 
    468   1.1.1.5  mrg #if __cplusplus > 201402L
    469   1.1.1.5  mrg   template <typename _R1, typename _R2>
    470   1.1.1.5  mrg     inline constexpr bool ratio_equal_v = ratio_equal<_R1, _R2>::value;
    471   1.1.1.5  mrg   template <typename _R1, typename _R2>
    472   1.1.1.5  mrg     inline constexpr bool ratio_not_equal_v = ratio_not_equal<_R1, _R2>::value;
    473   1.1.1.5  mrg   template <typename _R1, typename _R2>
    474   1.1.1.5  mrg     inline constexpr bool ratio_less_v = ratio_less<_R1, _R2>::value;
    475   1.1.1.5  mrg   template <typename _R1, typename _R2>
    476  1.1.1.13  mrg     inline constexpr bool ratio_less_equal_v
    477  1.1.1.13  mrg       = ratio_less_equal<_R1, _R2>::value;
    478   1.1.1.5  mrg   template <typename _R1, typename _R2>
    479   1.1.1.5  mrg     inline constexpr bool ratio_greater_v = ratio_greater<_R1, _R2>::value;
    480   1.1.1.5  mrg   template <typename _R1, typename _R2>
    481   1.1.1.5  mrg     inline constexpr bool ratio_greater_equal_v
    482  1.1.1.13  mrg       = ratio_greater_equal<_R1, _R2>::value;
    483   1.1.1.5  mrg #endif // C++17
    484   1.1.1.5  mrg 
    485  1.1.1.10  mrg   /// @cond undocumented
    486  1.1.1.10  mrg 
    487   1.1.1.2  mrg   template<typename _R1, typename _R2,
    488   1.1.1.2  mrg       bool = (_R1::num >= 0),
    489   1.1.1.2  mrg       bool = (_R2::num >= 0),
    490   1.1.1.2  mrg       bool = ratio_less<ratio<__static_abs<_R1::num>::value, _R1::den>,
    491   1.1.1.2  mrg         ratio<__static_abs<_R2::num>::value, _R2::den> >::value>
    492   1.1.1.2  mrg     struct __ratio_add_impl
    493   1.1.1.2  mrg     {
    494   1.1.1.2  mrg     private:
    495   1.1.1.2  mrg       typedef typename __ratio_add_impl<
    496   1.1.1.2  mrg         ratio<-_R1::num, _R1::den>,
    497   1.1.1.2  mrg         ratio<-_R2::num, _R2::den> >::type __t;
    498   1.1.1.2  mrg     public:
    499   1.1.1.2  mrg       typedef ratio<-__t::num, __t::den> type;
    500   1.1.1.2  mrg     };
    501   1.1.1.2  mrg 
    502   1.1.1.2  mrg   // True addition of nonnegative numbers.
    503   1.1.1.2  mrg   template<typename _R1, typename _R2, bool __b>
    504   1.1.1.2  mrg     struct __ratio_add_impl<_R1, _R2, true, true, __b>
    505   1.1.1.2  mrg     {
    506   1.1.1.2  mrg     private:
    507   1.1.1.2  mrg       static constexpr uintmax_t __g = __static_gcd<_R1::den, _R2::den>::value;
    508   1.1.1.2  mrg       static constexpr uintmax_t __d2 = _R2::den / __g;
    509   1.1.1.2  mrg       typedef __big_mul<_R1::den, __d2> __d;
    510   1.1.1.2  mrg       typedef __big_mul<_R1::num, _R2::den / __g> __x;
    511   1.1.1.2  mrg       typedef __big_mul<_R2::num, _R1::den / __g> __y;
    512   1.1.1.2  mrg       typedef __big_add<__x::__hi, __x::__lo, __y::__hi, __y::__lo> __n;
    513   1.1.1.2  mrg       static_assert(__n::__hi >= __x::__hi, "Internal library error");
    514   1.1.1.2  mrg       typedef __big_div<__n::__hi, __n::__lo, __g> __ng;
    515   1.1.1.2  mrg       static constexpr uintmax_t __g2 = __static_gcd<__ng::__rem, __g>::value;
    516   1.1.1.2  mrg       typedef __big_div<__n::__hi, __n::__lo, __g2> __n_final;
    517   1.1.1.2  mrg       static_assert(__n_final::__rem == 0, "Internal library error");
    518   1.1.1.2  mrg       static_assert(__n_final::__quot_hi == 0 &&
    519   1.1.1.2  mrg         __n_final::__quot_lo <= __INTMAX_MAX__, "overflow in addition");
    520   1.1.1.2  mrg       typedef __big_mul<_R1::den / __g2, __d2> __d_final;
    521   1.1.1.2  mrg       static_assert(__d_final::__hi == 0 &&
    522   1.1.1.2  mrg         __d_final::__lo <= __INTMAX_MAX__, "overflow in addition");
    523   1.1.1.2  mrg     public:
    524   1.1.1.2  mrg       typedef ratio<__n_final::__quot_lo, __d_final::__lo> type;
    525   1.1.1.2  mrg     };
    526   1.1.1.2  mrg 
    527   1.1.1.2  mrg   template<typename _R1, typename _R2>
    528   1.1.1.2  mrg     struct __ratio_add_impl<_R1, _R2, false, true, true>
    529   1.1.1.2  mrg     : __ratio_add_impl<_R2, _R1>
    530   1.1.1.2  mrg     { };
    531   1.1.1.2  mrg 
    532   1.1.1.2  mrg   // True subtraction of nonnegative numbers yielding a nonnegative result.
    533   1.1.1.2  mrg   template<typename _R1, typename _R2>
    534   1.1.1.2  mrg     struct __ratio_add_impl<_R1, _R2, true, false, false>
    535   1.1.1.2  mrg     {
    536   1.1.1.2  mrg     private:
    537   1.1.1.2  mrg       static constexpr uintmax_t __g = __static_gcd<_R1::den, _R2::den>::value;
    538   1.1.1.2  mrg       static constexpr uintmax_t __d2 = _R2::den / __g;
    539   1.1.1.2  mrg       typedef __big_mul<_R1::den, __d2> __d;
    540   1.1.1.2  mrg       typedef __big_mul<_R1::num, _R2::den / __g> __x;
    541   1.1.1.2  mrg       typedef __big_mul<-_R2::num, _R1::den / __g> __y;
    542   1.1.1.2  mrg       typedef __big_sub<__x::__hi, __x::__lo, __y::__hi, __y::__lo> __n;
    543   1.1.1.2  mrg       typedef __big_div<__n::__hi, __n::__lo, __g> __ng;
    544   1.1.1.2  mrg       static constexpr uintmax_t __g2 = __static_gcd<__ng::__rem, __g>::value;
    545   1.1.1.2  mrg       typedef __big_div<__n::__hi, __n::__lo, __g2> __n_final;
    546   1.1.1.2  mrg       static_assert(__n_final::__rem == 0, "Internal library error");
    547   1.1.1.2  mrg       static_assert(__n_final::__quot_hi == 0 &&
    548   1.1.1.2  mrg         __n_final::__quot_lo <= __INTMAX_MAX__, "overflow in addition");
    549   1.1.1.2  mrg       typedef __big_mul<_R1::den / __g2, __d2> __d_final;
    550   1.1.1.2  mrg       static_assert(__d_final::__hi == 0 &&
    551   1.1.1.2  mrg         __d_final::__lo <= __INTMAX_MAX__, "overflow in addition");
    552   1.1.1.2  mrg     public:
    553   1.1.1.2  mrg       typedef ratio<__n_final::__quot_lo, __d_final::__lo> type;
    554   1.1.1.2  mrg     };
    555   1.1.1.2  mrg 
    556   1.1.1.2  mrg   template<typename _R1, typename _R2>
    557   1.1.1.2  mrg     struct __ratio_add
    558   1.1.1.2  mrg     {
    559  1.1.1.13  mrg       static_assert(std::__are_both_ratios<_R1, _R2>(),
    560  1.1.1.13  mrg 		    "both template arguments must be a std::ratio");
    561  1.1.1.13  mrg 
    562   1.1.1.2  mrg       typedef typename __ratio_add_impl<_R1, _R2>::type type;
    563   1.1.1.2  mrg       static constexpr intmax_t num = type::num;
    564   1.1.1.2  mrg       static constexpr intmax_t den = type::den;
    565   1.1.1.2  mrg     };
    566   1.1.1.2  mrg 
    567  1.1.1.12  mrg #if ! __cpp_inline_variables
    568   1.1.1.2  mrg   template<typename _R1, typename _R2>
    569   1.1.1.2  mrg     constexpr intmax_t __ratio_add<_R1, _R2>::num;
    570   1.1.1.2  mrg 
    571   1.1.1.2  mrg   template<typename _R1, typename _R2>
    572   1.1.1.2  mrg     constexpr intmax_t __ratio_add<_R1, _R2>::den;
    573  1.1.1.12  mrg #endif
    574   1.1.1.2  mrg 
    575  1.1.1.10  mrg   /// @endcond
    576  1.1.1.10  mrg 
    577   1.1.1.2  mrg   /// ratio_add
    578   1.1.1.2  mrg   template<typename _R1, typename _R2>
    579   1.1.1.2  mrg     using ratio_add = typename __ratio_add<_R1, _R2>::type;
    580   1.1.1.2  mrg 
    581  1.1.1.10  mrg   /// @cond undocumented
    582  1.1.1.10  mrg 
    583   1.1.1.2  mrg   template<typename _R1, typename _R2>
    584   1.1.1.2  mrg     struct __ratio_subtract
    585   1.1.1.2  mrg     {
    586   1.1.1.2  mrg       typedef typename __ratio_add<
    587   1.1.1.2  mrg         _R1,
    588   1.1.1.2  mrg         ratio<-_R2::num, _R2::den>>::type type;
    589   1.1.1.2  mrg 
    590   1.1.1.2  mrg       static constexpr intmax_t num = type::num;
    591   1.1.1.2  mrg       static constexpr intmax_t den = type::den;
    592   1.1.1.2  mrg     };
    593   1.1.1.2  mrg 
    594  1.1.1.12  mrg #if ! __cpp_inline_variables
    595   1.1.1.2  mrg   template<typename _R1, typename _R2>
    596   1.1.1.2  mrg     constexpr intmax_t __ratio_subtract<_R1, _R2>::num;
    597   1.1.1.2  mrg 
    598   1.1.1.2  mrg   template<typename _R1, typename _R2>
    599   1.1.1.2  mrg     constexpr intmax_t __ratio_subtract<_R1, _R2>::den;
    600  1.1.1.12  mrg #endif
    601   1.1.1.2  mrg 
    602  1.1.1.10  mrg   /// @endcond
    603  1.1.1.10  mrg 
    604   1.1.1.2  mrg   /// ratio_subtract
    605   1.1.1.2  mrg   template<typename _R1, typename _R2>
    606   1.1.1.2  mrg     using ratio_subtract = typename __ratio_subtract<_R1, _R2>::type;
    607   1.1.1.2  mrg 
    608  1.1.1.14  mrg #if __INTMAX_WIDTH__ >= 96
    609  1.1.1.14  mrg # if __cpp_lib_ratio >= 202306L
    610  1.1.1.14  mrg #  if __INTMAX_WIDTH__ >= 128
    611  1.1.1.14  mrg   using quecto = ratio<                  1, 1000000000000000000000000000000>;
    612  1.1.1.14  mrg #  endif
    613  1.1.1.14  mrg   using ronto  = ratio<                     1, 1000000000000000000000000000>;
    614  1.1.1.14  mrg # endif
    615  1.1.1.14  mrg   using yocto  = ratio<                        1, 1000000000000000000000000>;
    616  1.1.1.14  mrg   using zepto  = ratio<                        1,    1000000000000000000000>;
    617  1.1.1.14  mrg #endif
    618  1.1.1.14  mrg   using atto   = ratio<                        1,       1000000000000000000>;
    619  1.1.1.14  mrg   using femto  = ratio<                        1,          1000000000000000>;
    620  1.1.1.14  mrg   using pico   = ratio<                        1,             1000000000000>;
    621  1.1.1.14  mrg   using nano   = ratio<                        1,                1000000000>;
    622  1.1.1.14  mrg   using micro  = ratio<                        1,                   1000000>;
    623  1.1.1.14  mrg   using milli  = ratio<                        1,                      1000>;
    624  1.1.1.14  mrg   using centi  = ratio<                        1,                       100>;
    625  1.1.1.14  mrg   using deci   = ratio<                        1,                        10>;
    626  1.1.1.14  mrg   using deca   = ratio<                       10,                         1>;
    627  1.1.1.14  mrg   using hecto  = ratio<                      100,                         1>;
    628  1.1.1.14  mrg   using kilo   = ratio<                     1000,                         1>;
    629  1.1.1.14  mrg   using mega   = ratio<                  1000000,                         1>;
    630  1.1.1.14  mrg   using giga   = ratio<               1000000000,                         1>;
    631  1.1.1.14  mrg   using tera   = ratio<            1000000000000,                         1>;
    632  1.1.1.14  mrg   using peta   = ratio<         1000000000000000,                         1>;
    633  1.1.1.14  mrg   using exa    = ratio<      1000000000000000000,                         1>;
    634  1.1.1.14  mrg #if __INTMAX_WIDTH__ >= 96
    635  1.1.1.14  mrg   using zetta  = ratio<   1000000000000000000000,                         1>;
    636  1.1.1.14  mrg   using yotta  = ratio<1000000000000000000000000,                         1>;
    637  1.1.1.14  mrg # if __cpp_lib_ratio >= 202306L
    638  1.1.1.14  mrg   using ronna  = ratio<1000000000000000000000000000,                      1>;
    639  1.1.1.14  mrg #  if __INTMAX_WIDTH__ >= 128
    640  1.1.1.14  mrg   using quetta = ratio<1000000000000000000000000000000,                   1>;
    641  1.1.1.14  mrg #  endif
    642  1.1.1.14  mrg # endif
    643  1.1.1.14  mrg #endif
    644       1.1  mrg 
    645  1.1.1.11  mrg   /// @} group ratio
    646   1.1.1.2  mrg _GLIBCXX_END_NAMESPACE_VERSION
    647   1.1.1.2  mrg } // namespace
    648       1.1  mrg 
    649   1.1.1.2  mrg #endif // C++11
    650       1.1  mrg 
    651       1.1  mrg #endif //_GLIBCXX_RATIO
    652