2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
19 template <intmax_t N, intmax_t D = 1>
23 static constexpr intmax_t num;
24 static constexpr intmax_t den;
25 typedef ratio<num, den> type;
29 template <class R1, class R2> using ratio_add = ...;
30 template <class R1, class R2> using ratio_subtract = ...;
31 template <class R1, class R2> using ratio_multiply = ...;
32 template <class R1, class R2> using ratio_divide = ...;
35 template <class R1, class R2> struct ratio_equal;
36 template <class R1, class R2> struct ratio_not_equal;
37 template <class R1, class R2> struct ratio_less;
38 template <class R1, class R2> struct ratio_less_equal;
39 template <class R1, class R2> struct ratio_greater;
40 template <class R1, class R2> struct ratio_greater_equal;
42 // convenience SI typedefs
43 using quecto = ratio <1, 1'000'000'000'000'000'000'000'000'000'000>; // Since C++26; not supported
44 using ronto = ratio <1, 1'000'000'000'000'000'000'000'000'000>; // Since C++26; not supported
45 typedef ratio<1, 1000000000000000000000000> yocto; // not supported
46 typedef ratio<1, 1000000000000000000000> zepto; // not supported
47 typedef ratio<1, 1000000000000000000> atto;
48 typedef ratio<1, 1000000000000000> femto;
49 typedef ratio<1, 1000000000000> pico;
50 typedef ratio<1, 1000000000> nano;
51 typedef ratio<1, 1000000> micro;
52 typedef ratio<1, 1000> milli;
53 typedef ratio<1, 100> centi;
54 typedef ratio<1, 10> deci;
55 typedef ratio< 10, 1> deca;
56 typedef ratio< 100, 1> hecto;
57 typedef ratio< 1000, 1> kilo;
58 typedef ratio< 1000000, 1> mega;
59 typedef ratio< 1000000000, 1> giga;
60 typedef ratio< 1000000000000, 1> tera;
61 typedef ratio< 1000000000000000, 1> peta;
62 typedef ratio< 1000000000000000000, 1> exa;
63 typedef ratio< 1000000000000000000000, 1> zetta; // not supported
64 typedef ratio<1000000000000000000000000, 1> yotta; // not supported
65 using ronna = ratio <1'000'000'000'000'000'000'000'000'000, 1>; // Since C++26; not supported
66 using quetta = ratio <1'000'000'000'000'000'000'000'000'000'000, 1>; // Since C++26; not supported
68 // 20.11.5, ratio comparison
69 template <class R1, class R2> inline constexpr bool ratio_equal_v
70 = ratio_equal<R1, R2>::value; // C++17
71 template <class R1, class R2> inline constexpr bool ratio_not_equal_v
72 = ratio_not_equal<R1, R2>::value; // C++17
73 template <class R1, class R2> inline constexpr bool ratio_less_v
74 = ratio_less<R1, R2>::value; // C++17
75 template <class R1, class R2> inline constexpr bool ratio_less_equal_v
76 = ratio_less_equal<R1, R2>::value; // C++17
77 template <class R1, class R2> inline constexpr bool ratio_greater_v
78 = ratio_greater<R1, R2>::value; // C++17
79 template <class R1, class R2> inline constexpr bool ratio_greater_equal_v
80 = ratio_greater_equal<R1, R2>::value; // C++17
85 #include <__type_traits/integral_constant.h>
90 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
91 # pragma GCC system_header
95 #include <__undef_macros>
97 _LIBCPP_BEGIN_NAMESPACE_STD
101 template <intmax_t _Xp, intmax_t _Yp>
102 inline const intmax_t __static_gcd = __static_gcd<_Yp, _Xp % _Yp>;
104 template <intmax_t _Xp>
105 inline const intmax_t __static_gcd<_Xp, 0> = _Xp;
108 inline const intmax_t __static_gcd<0, 0> = 1;
112 template <intmax_t _Xp, intmax_t _Yp>
113 inline const intmax_t __static_lcm = _Xp / __static_gcd<_Xp, _Yp> * _Yp;
115 template <intmax_t _Xp>
116 inline const intmax_t __static_abs = _Xp < 0 ? -_Xp : _Xp;
118 template <intmax_t _Xp>
119 inline const intmax_t __static_sign = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
121 template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp> >
124 template <intmax_t _Xp, intmax_t _Yp>
125 class __ll_add<_Xp, _Yp, 1> {
126 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
127 static const intmax_t max = -min;
129 static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
132 static const intmax_t value = _Xp + _Yp;
135 template <intmax_t _Xp, intmax_t _Yp>
136 class __ll_add<_Xp, _Yp, 0> {
138 static const intmax_t value = _Xp;
141 template <intmax_t _Xp, intmax_t _Yp>
142 class __ll_add<_Xp, _Yp, -1> {
143 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
144 static const intmax_t max = -min;
146 static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
149 static const intmax_t value = _Xp + _Yp;
152 template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp> >
155 template <intmax_t _Xp, intmax_t _Yp>
156 class __ll_sub<_Xp, _Yp, 1> {
157 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
158 static const intmax_t max = -min;
160 static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
163 static const intmax_t value = _Xp - _Yp;
166 template <intmax_t _Xp, intmax_t _Yp>
167 class __ll_sub<_Xp, _Yp, 0> {
169 static const intmax_t value = _Xp;
172 template <intmax_t _Xp, intmax_t _Yp>
173 class __ll_sub<_Xp, _Yp, -1> {
174 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
175 static const intmax_t max = -min;
177 static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");
180 static const intmax_t value = _Xp - _Yp;
183 template <intmax_t _Xp, intmax_t _Yp>
185 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
186 static const intmax_t min = nan + 1;
187 static const intmax_t max = -min;
188 static const intmax_t __a_x = __static_abs<_Xp>;
189 static const intmax_t __a_y = __static_abs<_Yp>;
191 static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
194 static const intmax_t value = _Xp * _Yp;
197 template <intmax_t _Yp>
198 class __ll_mul<0, _Yp> {
200 static const intmax_t value = 0;
203 template <intmax_t _Xp>
204 class __ll_mul<_Xp, 0> {
206 static const intmax_t value = 0;
210 class __ll_mul<0, 0> {
212 static const intmax_t value = 0;
215 // Not actually used but left here in case needed in future maintenance
216 template <intmax_t _Xp, intmax_t _Yp>
218 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
219 static const intmax_t min = nan + 1;
220 static const intmax_t max = -min;
222 static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
225 static const intmax_t value = _Xp / _Yp;
228 template <intmax_t _Num, intmax_t _Den = 1>
229 class _LIBCPP_TEMPLATE_VIS ratio {
230 static_assert(__static_abs<_Num> >= 0, "ratio numerator is out of range");
231 static_assert(_Den != 0, "ratio divide by 0");
232 static_assert(__static_abs<_Den> > 0, "ratio denominator is out of range");
233 static _LIBCPP_CONSTEXPR const intmax_t __na = __static_abs<_Num>;
234 static _LIBCPP_CONSTEXPR const intmax_t __da = __static_abs<_Den>;
235 static _LIBCPP_CONSTEXPR const intmax_t __s = __static_sign<_Num> * __static_sign<_Den>;
236 static _LIBCPP_CONSTEXPR const intmax_t __gcd = __static_gcd<__na, __da>;
239 static inline _LIBCPP_CONSTEXPR const intmax_t num = __s * __na / __gcd;
240 static inline _LIBCPP_CONSTEXPR const intmax_t den = __da / __gcd;
242 typedef ratio<num, den> type;
246 inline const bool __is_ratio_v = false;
248 template <intmax_t _Num, intmax_t _Den>
249 inline const bool __is_ratio_v<ratio<_Num, _Den> > = true;
251 typedef ratio<1LL, 1000000000000000000LL> atto;
252 typedef ratio<1LL, 1000000000000000LL> femto;
253 typedef ratio<1LL, 1000000000000LL> pico;
254 typedef ratio<1LL, 1000000000LL> nano;
255 typedef ratio<1LL, 1000000LL> micro;
256 typedef ratio<1LL, 1000LL> milli;
257 typedef ratio<1LL, 100LL> centi;
258 typedef ratio<1LL, 10LL> deci;
259 typedef ratio< 10LL, 1LL> deca;
260 typedef ratio< 100LL, 1LL> hecto;
261 typedef ratio< 1000LL, 1LL> kilo;
262 typedef ratio< 1000000LL, 1LL> mega;
263 typedef ratio< 1000000000LL, 1LL> giga;
264 typedef ratio< 1000000000000LL, 1LL> tera;
265 typedef ratio< 1000000000000000LL, 1LL> peta;
266 typedef ratio<1000000000000000000LL, 1LL> exa;
268 template <class _R1, class _R2>
269 struct __ratio_multiply {
271 static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>;
272 static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>;
274 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
275 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
278 typedef typename ratio< __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,
279 __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value >::type type;
282 #ifndef _LIBCPP_CXX03_LANG
284 template <class _R1, class _R2>
285 using ratio_multiply = typename __ratio_multiply<_R1, _R2>::type;
287 #else // _LIBCPP_CXX03_LANG
289 template <class _R1, class _R2>
290 struct _LIBCPP_TEMPLATE_VIS ratio_multiply : public __ratio_multiply<_R1, _R2>::type {};
292 #endif // _LIBCPP_CXX03_LANG
294 template <class _R1, class _R2>
295 struct __ratio_divide {
297 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>;
298 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>;
300 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
301 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
304 typedef typename ratio< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
305 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::type type;
308 #ifndef _LIBCPP_CXX03_LANG
310 template <class _R1, class _R2>
311 using ratio_divide = typename __ratio_divide<_R1, _R2>::type;
313 #else // _LIBCPP_CXX03_LANG
315 template <class _R1, class _R2>
316 struct _LIBCPP_TEMPLATE_VIS ratio_divide : public __ratio_divide<_R1, _R2>::type {};
318 #endif // _LIBCPP_CXX03_LANG
320 template <class _R1, class _R2>
323 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>;
324 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>;
326 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
327 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
330 typedef typename ratio_multiply<
331 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
332 ratio< __ll_add< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
333 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::value,
334 _R2::den > >::type type;
337 #ifndef _LIBCPP_CXX03_LANG
339 template <class _R1, class _R2>
340 using ratio_add = typename __ratio_add<_R1, _R2>::type;
342 #else // _LIBCPP_CXX03_LANG
344 template <class _R1, class _R2>
345 struct _LIBCPP_TEMPLATE_VIS ratio_add : public __ratio_add<_R1, _R2>::type {};
347 #endif // _LIBCPP_CXX03_LANG
349 template <class _R1, class _R2>
350 struct __ratio_subtract {
352 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>;
353 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>;
355 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
356 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
359 typedef typename ratio_multiply<
360 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
361 ratio< __ll_sub< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
362 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::value,
363 _R2::den > >::type type;
366 #ifndef _LIBCPP_CXX03_LANG
368 template <class _R1, class _R2>
369 using ratio_subtract = typename __ratio_subtract<_R1, _R2>::type;
371 #else // _LIBCPP_CXX03_LANG
373 template <class _R1, class _R2>
374 struct _LIBCPP_TEMPLATE_VIS ratio_subtract : public __ratio_subtract<_R1, _R2>::type {};
376 #endif // _LIBCPP_CXX03_LANG
380 template <class _R1, class _R2>
381 struct _LIBCPP_TEMPLATE_VIS ratio_equal : _BoolConstant<(_R1::num == _R2::num && _R1::den == _R2::den)> {
382 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
383 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
386 template <class _R1, class _R2>
387 struct _LIBCPP_TEMPLATE_VIS ratio_not_equal : _BoolConstant<!ratio_equal<_R1, _R2>::value> {
388 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
389 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
397 intmax_t _Q1 = _R1::num / _R1::den,
398 intmax_t _M1 = _R1::num % _R1::den,
399 intmax_t _Q2 = _R2::num / _R2::den,
400 intmax_t _M2 = _R2::num % _R2::den>
401 struct __ratio_less1 {
402 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
405 template <class _R1, class _R2, bool _Odd, intmax_t _Qp>
406 struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0> {
407 static const bool value = false;
410 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>
411 struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2> {
412 static const bool value = !_Odd;
415 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1>
416 struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0> {
417 static const bool value = _Odd;
420 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1, intmax_t _M2>
421 struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2> {
422 static const bool value = __ratio_less1<ratio<_R1::den, _M1>, ratio<_R2::den, _M2>, !_Odd>::value;
425 template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>, intmax_t _S2 = __static_sign<_R2::num> >
426 struct __ratio_less {
427 static const bool value = _S1 < _S2;
430 template <class _R1, class _R2>
431 struct __ratio_less<_R1, _R2, 1LL, 1LL> {
432 static const bool value = __ratio_less1<_R1, _R2>::value;
435 template <class _R1, class _R2>
436 struct __ratio_less<_R1, _R2, -1LL, -1LL> {
437 static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
440 template <class _R1, class _R2>
441 struct _LIBCPP_TEMPLATE_VIS ratio_less : _BoolConstant<__ratio_less<_R1, _R2>::value> {
442 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
443 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
446 template <class _R1, class _R2>
447 struct _LIBCPP_TEMPLATE_VIS ratio_less_equal : _BoolConstant<!ratio_less<_R2, _R1>::value> {
448 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
449 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
452 template <class _R1, class _R2>
453 struct _LIBCPP_TEMPLATE_VIS ratio_greater : _BoolConstant<ratio_less<_R2, _R1>::value> {
454 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
455 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
458 template <class _R1, class _R2>
459 struct _LIBCPP_TEMPLATE_VIS ratio_greater_equal : _BoolConstant<!ratio_less<_R1, _R2>::value> {
460 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
461 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
464 template <class _R1, class _R2>
465 using __ratio_gcd = ratio<__static_gcd<_R1::num, _R2::num>, __static_lcm<_R1::den, _R2::den> >;
467 #if _LIBCPP_STD_VER >= 17
468 template <class _R1, class _R2>
469 inline constexpr bool ratio_equal_v = ratio_equal<_R1, _R2>::value;
471 template <class _R1, class _R2>
472 inline constexpr bool ratio_not_equal_v = ratio_not_equal<_R1, _R2>::value;
474 template <class _R1, class _R2>
475 inline constexpr bool ratio_less_v = ratio_less<_R1, _R2>::value;
477 template <class _R1, class _R2>
478 inline constexpr bool ratio_less_equal_v = ratio_less_equal<_R1, _R2>::value;
480 template <class _R1, class _R2>
481 inline constexpr bool ratio_greater_v = ratio_greater<_R1, _R2>::value;
483 template <class _R1, class _R2>
484 inline constexpr bool ratio_greater_equal_v = ratio_greater_equal<_R1, _R2>::value;
487 _LIBCPP_END_NAMESPACE_STD
491 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
492 # include <type_traits>
495 #endif // _LIBCPP_RATIO