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