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