31#define _GLIBCXX_RATIO 1
33#pragma GCC system_header
35#if __cplusplus < 201103L
42#define __glibcxx_want_ratio
45namespace std _GLIBCXX_VISIBILITY(default)
47_GLIBCXX_BEGIN_NAMESPACE_VERSION
59 template<
intmax_t _Pn>
61 : integral_constant<intmax_t, (_Pn < 0) ? -1 : 1>
64 template<intmax_t _Pn>
66 : integral_constant<intmax_t, _Pn * __static_sign<_Pn>::value>
69 template<intmax_t _Pn, intmax_t _Qn>
71 : __static_gcd<_Qn, (_Pn % _Qn)>
74 template<intmax_t _Pn>
75 struct __static_gcd<_Pn, 0>
76 : integral_constant<intmax_t, __static_abs<_Pn>::value>
79 template<intmax_t _Qn>
80 struct __static_gcd<0, _Qn>
81 : integral_constant<intmax_t, __static_abs<_Qn>::value>
90 template<intmax_t _Pn, intmax_t _Qn>
91 struct __safe_multiply
94 static const uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
96 static const uintmax_t __a0 = __static_abs<_Pn>::value % __c;
97 static const uintmax_t __a1 = __static_abs<_Pn>::value / __c;
98 static const uintmax_t __b0 = __static_abs<_Qn>::value % __c;
99 static const uintmax_t __b1 = __static_abs<_Qn>::value / __c;
101 static_assert(__a1 == 0 || __b1 == 0,
102 "overflow in multiplication");
103 static_assert(__a0 * __b1 + __b0 * __a1 < (__c >> 1),
104 "overflow in multiplication");
105 static_assert(__b0 * __a0 <= __INTMAX_MAX__,
106 "overflow in multiplication");
107 static_assert((__a0 * __b1 + __b0 * __a1) * __c
108 <= __INTMAX_MAX__ - __b0 * __a0,
109 "overflow in multiplication");
112 static const intmax_t value = _Pn * _Qn;
117 template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2>
119 : integral_constant<bool, (__hi1 < __hi2
120 || (__hi1 == __hi2 && __lo1 < __lo2))>
123 template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2>
126 static constexpr uintmax_t __lo = __lo1 + __lo2;
127 static constexpr uintmax_t __hi = (__hi1 + __hi2 +
128 (__lo1 + __lo2 < __lo1));
132 template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2>
135 static_assert(!__big_less<__hi1, __lo1, __hi2, __lo2>::value,
136 "Internal library error");
137 static constexpr uintmax_t __lo = __lo1 - __lo2;
138 static constexpr uintmax_t __hi = (__hi1 - __hi2 -
143 template<uintmax_t __x, uintmax_t __y>
147 static constexpr uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
148 static constexpr uintmax_t __x0 = __x % __c;
149 static constexpr uintmax_t __x1 = __x / __c;
150 static constexpr uintmax_t __y0 = __y % __c;
151 static constexpr uintmax_t __y1 = __y / __c;
152 static constexpr uintmax_t __x0y0 = __x0 * __y0;
153 static constexpr uintmax_t __x0y1 = __x0 * __y1;
154 static constexpr uintmax_t __x1y0 = __x1 * __y0;
155 static constexpr uintmax_t __x1y1 = __x1 * __y1;
156 static constexpr uintmax_t __mix = __x0y1 + __x1y0;
157 static constexpr uintmax_t __mix_lo = __mix * __c;
158 static constexpr uintmax_t __mix_hi
159 = __mix / __c + ((__mix < __x0y1) ? __c : 0);
160 typedef __big_add<__mix_hi, __mix_lo, __x1y1, __x0y0> _Res;
162 static constexpr uintmax_t __hi = _Res::__hi;
163 static constexpr uintmax_t __lo = _Res::__lo;
168 template<uintmax_t __n1, uintmax_t __n0, uintmax_t __d>
169 struct __big_div_impl
172 static_assert(__d >= (uintmax_t(1) << (sizeof(intmax_t) * 8 - 1)),
173 "Internal library error");
174 static_assert(__n1 < __d, "Internal library error");
175 static constexpr uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
176 static constexpr uintmax_t __d1 = __d / __c;
177 static constexpr uintmax_t __d0 = __d % __c;
179 static constexpr uintmax_t __q1x = __n1 / __d1;
180 static constexpr uintmax_t __r1x = __n1 % __d1;
181 static constexpr uintmax_t __m = __q1x * __d0;
182 static constexpr uintmax_t __r1y = __r1x * __c + __n0 / __c;
183 static constexpr uintmax_t __r1z = __r1y + __d;
184 static constexpr uintmax_t __r1
185 = ((__r1y < __m) ? ((__r1z >= __d) && (__r1z < __m))
186 ? (__r1z + __d) : __r1z : __r1y) - __m;
187 static constexpr uintmax_t __q1
188 = __q1x - ((__r1y < __m)
189 ? ((__r1z >= __d) && (__r1z < __m)) ? 2 : 1 : 0);
190 static constexpr uintmax_t __q0x = __r1 / __d1;
191 static constexpr uintmax_t __r0x = __r1 % __d1;
192 static constexpr uintmax_t __n = __q0x * __d0;
193 static constexpr uintmax_t __r0y = __r0x * __c + __n0 % __c;
194 static constexpr uintmax_t __r0z = __r0y + __d;
195 static constexpr uintmax_t __r0
196 = ((__r0y < __n) ? ((__r0z >= __d) && (__r0z < __n))
197 ? (__r0z + __d) : __r0z : __r0y) - __n;
198 static constexpr uintmax_t __q0
199 = __q0x - ((__r0y < __n) ? ((__r0z >= __d)
200 && (__r0z < __n)) ? 2 : 1 : 0);
203 static constexpr uintmax_t __quot = __q1 * __c + __q0;
204 static constexpr uintmax_t __rem = __r0;
207 typedef __big_mul<__quot, __d> _Prod;
208 typedef __big_add<_Prod::__hi, _Prod::__lo, 0, __rem> _Sum;
209 static_assert(_Sum::__hi == __n1 && _Sum::__lo == __n0,
210 "Internal library error");
213 template<uintmax_t __n1, uintmax_t __n0, uintmax_t __d>
217 static_assert(__d != 0, "Internal library error");
218 static_assert(sizeof (uintmax_t) == sizeof (unsigned long long),
219 "This library calls __builtin_clzll on uintmax_t, which "
220 "is unsafe on your platform. Please complain to "
221 "http://gcc.gnu.org/bugzilla/");
222 static constexpr int __shift = __builtin_clzll(__d);
223 static constexpr int __coshift_ = sizeof(uintmax_t) * 8 - __shift;
224 static constexpr int __coshift = (__shift != 0) ? __coshift_ : 0;
225 static constexpr uintmax_t __c1 = uintmax_t(1) << __shift;
226 static constexpr uintmax_t __c2 = uintmax_t(1) << __coshift;
227 static constexpr uintmax_t __new_d = __d * __c1;
228 static constexpr uintmax_t __new_n0 = __n0 * __c1;
229 static constexpr uintmax_t __n1_shifted = (__n1 % __d) * __c1;
230 static constexpr uintmax_t __n0_top = (__shift != 0) ? (__n0 / __c2) : 0;
231 static constexpr uintmax_t __new_n1 = __n1_shifted + __n0_top;
232 typedef __big_div_impl<__new_n1, __new_n0, __new_d> _Res;
235 static constexpr uintmax_t __quot_hi = __n1 / __d;
236 static constexpr uintmax_t __quot_lo = _Res::__quot;
237 static constexpr uintmax_t __rem = _Res::__rem / __c1;
240 typedef __big_mul<__quot_lo, __d> _P0;
241 typedef __big_mul<__quot_hi, __d> _P1;
242 typedef __big_add<_P0::__hi, _P0::__lo, _P1::__lo, __rem> _Sum;
244 static_assert(_P1::__hi == 0, "Internal library error");
245 static_assert(_Sum::__hi >= _P0::__hi, "Internal library error");
247 static_assert(_Sum::__hi == __n1 && _Sum::__lo == __n0,
248 "Internal library error");
249 static_assert(__rem < __d, "Internal library error");
268 template<intmax_t _Num, intmax_t _Den = 1>
271 static_assert(_Den != 0, "denominator cannot be zero");
272 static_assert(_Num >= -__INTMAX_MAX__ && _Den >= -__INTMAX_MAX__,
276 static constexpr intmax_t num =
277 _Num * __static_sign<_Den>::value / __static_gcd<_Num, _Den>::value;
279 static constexpr intmax_t den =
280 __static_abs<_Den>::value / __static_gcd<_Num, _Den>::value;
282 typedef ratio<num, den> type;
285#if ! __cpp_inline_variables
286 template<intmax_t _Num, intmax_t _Den>
287 constexpr intmax_t ratio<_Num, _Den>::num;
289 template<intmax_t _Num, intmax_t _Den>
290 constexpr intmax_t ratio<_Num, _Den>::den;
295 template<typename _Tp>
300 template<intmax_t _Num, intmax_t _Den>
301 struct __is_ratio<ratio<_Num, _Den>>
305#if __cpp_variable_templates
306 template<typename _Tp>
307 constexpr bool __is_ratio_v = false;
308 template<intmax_t _Num, intmax_t _Den>
309 constexpr bool __is_ratio_v<ratio<_Num, _Den>> = true;
312 template<typename _R1, typename _R2>
314 __are_both_ratios() noexcept
316#if __cpp_variable_templates && __cpp_if_constexpr
317 if constexpr (__is_ratio_v<_R1>)
318 if constexpr (__is_ratio_v<_R2>)
322 return __and_<__is_ratio<_R1>, __is_ratio<_R2>>::value;
326 template<typename _R1, typename _R2>
327 struct __ratio_multiply
329 static_assert(std::__are_both_ratios<_R1, _R2>(),
330 "both template arguments must be a std::ratio");
333 static const intmax_t __gcd1 =
334 __static_gcd<_R1::num, _R2::den>::value;
335 static const intmax_t __gcd2 =
336 __static_gcd<_R2::num, _R1::den>::value;
340 __safe_multiply<(_R1::num / __gcd1),
341 (_R2::num / __gcd2)>::value,
342 __safe_multiply<(_R1::den / __gcd2),
343 (_R2::den / __gcd1)>::value> type;
345 static constexpr intmax_t num = type::num;
346 static constexpr intmax_t den = type::den;
349#if ! __cpp_inline_variables
350 template<typename _R1, typename _R2>
351 constexpr intmax_t __ratio_multiply<_R1, _R2>::num;
353 template<typename _R1, typename _R2>
354 constexpr intmax_t __ratio_multiply<_R1, _R2>::den;
360 template<typename _R1, typename _R2>
361 using ratio_multiply = typename __ratio_multiply<_R1, _R2>::type;
365 template<typename _R1, typename _R2>
366 struct __ratio_divide
368 static_assert(_R2::num != 0, "division by 0");
370 typedef typename __ratio_multiply<
372 ratio<_R2::den, _R2::num>>::type type;
374 static constexpr intmax_t num = type::num;
375 static constexpr intmax_t den = type::den;
378#if ! __cpp_inline_variables
379 template<typename _R1, typename _R2>
380 constexpr intmax_t __ratio_divide<_R1, _R2>::num;
382 template<typename _R1, typename _R2>
383 constexpr intmax_t __ratio_divide<_R1, _R2>::den;
389 template<typename _R1, typename _R2>
390 using ratio_divide = typename __ratio_divide<_R1, _R2>::type;
393 template<typename _R1, typename _R2>
395 : integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den>
397 static_assert(std::__are_both_ratios<_R1, _R2>(),
398 "both template arguments must be a std::ratio");
402 template<typename _R1, typename _R2>
403 struct ratio_not_equal
404 : integral_constant<bool, !ratio_equal<_R1, _R2>::value>
410 template<typename _R1, typename _R2,
411 typename _Left = __big_mul<_R1::num,_R2::den>,
412 typename _Right = __big_mul<_R2::num,_R1::den> >
413 struct __ratio_less_impl_1
414 : integral_constant<bool, __big_less<_Left::__hi, _Left::__lo,
415 _Right::__hi, _Right::__lo>::value>
418 template<typename _R1, typename _R2,
419 bool = (_R1::num == 0 || _R2::num == 0
420 || (__static_sign<_R1::num>::value
421 != __static_sign<_R2::num>::value)),
422 bool = (__static_sign<_R1::num>::value == -1
423 && __static_sign<_R2::num>::value == -1)>
424 struct __ratio_less_impl
425 : __ratio_less_impl_1<_R1, _R2>::type
428 template<typename _R1, typename _R2>
429 struct __ratio_less_impl<_R1, _R2, true, false>
430 : integral_constant<bool, _R1::num < _R2::num>
433 template<typename _R1, typename _R2>
434 struct __ratio_less_impl<_R1, _R2, false, true>
435 : __ratio_less_impl_1<ratio<-_R2::num, _R2::den>,
436 ratio<-_R1::num, _R1::den> >::type
442 template<typename _R1, typename _R2>
444 : __ratio_less_impl<_R1, _R2>::type
446 static_assert(std::__are_both_ratios<_R1, _R2>(),
447 "both template arguments must be a std::ratio");
451 template<typename _R1, typename _R2>
452 struct ratio_less_equal
453 : integral_constant<bool, !ratio_less<_R2, _R1>::value>
457 template<typename _R1, typename _R2>
459 : integral_constant<bool, ratio_less<_R2, _R1>::value>
463 template<typename _R1, typename _R2>
464 struct ratio_greater_equal
465 : integral_constant<bool, !ratio_less<_R1, _R2>::value>
468#if __cplusplus > 201402L
469 template <typename _R1, typename _R2>
470 inline constexpr bool ratio_equal_v = ratio_equal<_R1, _R2>::value;
471 template <typename _R1, typename _R2>
472 inline constexpr bool ratio_not_equal_v = ratio_not_equal<_R1, _R2>::value;
473 template <typename _R1, typename _R2>
474 inline constexpr bool ratio_less_v = ratio_less<_R1, _R2>::value;
475 template <typename _R1, typename _R2>
476 inline constexpr bool ratio_less_equal_v
477 = ratio_less_equal<_R1, _R2>::value;
478 template <typename _R1, typename _R2>
479 inline constexpr bool ratio_greater_v = ratio_greater<_R1, _R2>::value;
480 template <typename _R1, typename _R2>
481 inline constexpr bool ratio_greater_equal_v
482 = ratio_greater_equal<_R1, _R2>::value;
487 template<typename _R1, typename _R2,
488 bool = (_R1::num >= 0),
489 bool = (_R2::num >= 0),
490 bool = ratio_less<ratio<__static_abs<_R1::num>::value, _R1::den>,
491 ratio<__static_abs<_R2::num>::value, _R2::den> >::value>
492 struct __ratio_add_impl
495 typedef typename __ratio_add_impl<
496 ratio<-_R1::num, _R1::den>,
497 ratio<-_R2::num, _R2::den> >::type __t;
499 typedef ratio<-__t::num, __t::den> type;
503 template<typename _R1, typename _R2, bool __b>
504 struct __ratio_add_impl<_R1, _R2, true, true, __b>
507 static constexpr uintmax_t __g = __static_gcd<_R1::den, _R2::den>::value;
508 static constexpr uintmax_t __d2 = _R2::den / __g;
509 typedef __big_mul<_R1::den, __d2> __d;
510 typedef __big_mul<_R1::num, _R2::den / __g> __x;
511 typedef __big_mul<_R2::num, _R1::den / __g> __y;
512 typedef __big_add<__x::__hi, __x::__lo, __y::__hi, __y::__lo> __n;
513 static_assert(__n::__hi >= __x::__hi, "Internal library error");
514 typedef __big_div<__n::__hi, __n::__lo, __g> __ng;
515 static constexpr uintmax_t __g2 = __static_gcd<__ng::__rem, __g>::value;
516 typedef __big_div<__n::__hi, __n::__lo, __g2> __n_final;
517 static_assert(__n_final::__rem == 0, "Internal library error");
518 static_assert(__n_final::__quot_hi == 0 &&
519 __n_final::__quot_lo <= __INTMAX_MAX__, "overflow in addition");
520 typedef __big_mul<_R1::den / __g2, __d2> __d_final;
521 static_assert(__d_final::__hi == 0 &&
522 __d_final::__lo <= __INTMAX_MAX__, "overflow in addition");
524 typedef ratio<__n_final::__quot_lo, __d_final::__lo> type;
527 template<typename _R1, typename _R2>
528 struct __ratio_add_impl<_R1, _R2, false, true, true>
529 : __ratio_add_impl<_R2, _R1>
533 template<typename _R1, typename _R2>
534 struct __ratio_add_impl<_R1, _R2, true, false, false>
537 static constexpr uintmax_t __g = __static_gcd<_R1::den, _R2::den>::value;
538 static constexpr uintmax_t __d2 = _R2::den / __g;
539 typedef __big_mul<_R1::den, __d2> __d;
540 typedef __big_mul<_R1::num, _R2::den / __g> __x;
541 typedef __big_mul<-_R2::num, _R1::den / __g> __y;
542 typedef __big_sub<__x::__hi, __x::__lo, __y::__hi, __y::__lo> __n;
543 typedef __big_div<__n::__hi, __n::__lo, __g> __ng;
544 static constexpr uintmax_t __g2 = __static_gcd<__ng::__rem, __g>::value;
545 typedef __big_div<__n::__hi, __n::__lo, __g2> __n_final;
546 static_assert(__n_final::__rem == 0, "Internal library error");
547 static_assert(__n_final::__quot_hi == 0 &&
548 __n_final::__quot_lo <= __INTMAX_MAX__, "overflow in addition");
549 typedef __big_mul<_R1::den / __g2, __d2> __d_final;
550 static_assert(__d_final::__hi == 0 &&
551 __d_final::__lo <= __INTMAX_MAX__, "overflow in addition");
553 typedef ratio<__n_final::__quot_lo, __d_final::__lo> type;
556 template<typename _R1, typename _R2>
559 static_assert(std::__are_both_ratios<_R1, _R2>(),
560 "both template arguments must be a std::ratio");
562 typedef typename __ratio_add_impl<_R1, _R2>::type type;
563 static constexpr intmax_t num = type::num;
564 static constexpr intmax_t den = type::den;
567#if ! __cpp_inline_variables
568 template<typename _R1, typename _R2>
569 constexpr intmax_t __ratio_add<_R1, _R2>::num;
571 template<typename _R1, typename _R2>
572 constexpr intmax_t __ratio_add<_R1, _R2>::den;
578 template<typename _R1, typename _R2>
579 using ratio_add = typename __ratio_add<_R1, _R2>::type;
583 template<typename _R1, typename _R2>
584 struct __ratio_subtract
586 typedef typename __ratio_add<
588 ratio<-_R2::num, _R2::den>>::type type;
590 static constexpr intmax_t num = type::num;
591 static constexpr intmax_t den = type::den;
594#if ! __cpp_inline_variables
595 template<typename _R1, typename _R2>
596 constexpr intmax_t __ratio_subtract<_R1, _R2>::num;
598 template<typename _R1, typename _R2>
599 constexpr intmax_t __ratio_subtract<_R1, _R2>::den;
605 template<typename _R1, typename _R2>
606 using ratio_subtract = typename __ratio_subtract<_R1, _R2>::type;
608#if __INTMAX_WIDTH__ >= 96
609# if __cpp_lib_ratio >= 202306L
610# if __INTMAX_WIDTH__ >= 128
611 using quecto = ratio< 1, 1000000000000000000000000000000>;
613 using ronto = ratio< 1, 1000000000000000000000000000>;
615 using yocto = ratio< 1, 1000000000000000000000000>;
616 using zepto = ratio< 1, 1000000000000000000000>;
618 using atto = ratio< 1, 1000000000000000000>;
619 using femto = ratio< 1, 1000000000000000>;
620 using pico = ratio< 1, 1000000000000>;
621 using nano = ratio< 1, 1000000000>;
622 using micro = ratio< 1, 1000000>;
623 using milli = ratio< 1, 1000>;
624 using centi = ratio< 1, 100>;
625 using deci = ratio< 1, 10>;
626 using deca = ratio< 10, 1>;
627 using hecto = ratio< 100, 1>;
628 using kilo = ratio< 1000, 1>;
629 using mega = ratio< 1000000, 1>;
630 using giga = ratio< 1000000000, 1>;
631 using tera = ratio< 1000000000000, 1>;
632 using peta = ratio< 1000000000000000, 1>;
633 using exa = ratio< 1000000000000000000, 1>;
634#if __INTMAX_WIDTH__ >= 96
635 using zetta = ratio< 1000000000000000000000, 1>;
636 using yotta = ratio<1000000000000000000000000, 1>;
637# if __cpp_lib_ratio >= 202306L
638 using ronna = ratio<1000000000000000000000000000, 1>;
639# if __INTMAX_WIDTH__ >= 128
640 using quetta = ratio<1000000000000000000000000000000, 1>;
646_GLIBCXX_END_NAMESPACE_VERSION
ISO C++ entities toplevel namespace is std.