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
84 #include <__cxx03/__config>
85 #include <__cxx03/__type_traits/integral_constant.h>
86 #include <__cxx03/climits>
87 #include <__cxx03/cstdint>
88 #include <__cxx03/version>
90 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
91 # pragma GCC system_header
95 #include <__cxx03/__undef_macros>
97 _LIBCPP_BEGIN_NAMESPACE_STD
101 template <intmax_t _Xp, intmax_t _Yp>
102 struct __static_gcd {
103 static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value;
106 template <intmax_t _Xp>
107 struct __static_gcd<_Xp, 0> {
108 static const intmax_t value = _Xp;
112 struct __static_gcd<0, 0> {
113 static const intmax_t value = 1;
118 template <intmax_t _Xp, intmax_t _Yp>
119 struct __static_lcm {
120 static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp;
123 template <intmax_t _Xp>
124 struct __static_abs {
125 static const intmax_t value = _Xp < 0 ? -_Xp : _Xp;
128 template <intmax_t _Xp>
129 struct __static_sign {
130 static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
133 template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
136 template <intmax_t _Xp, intmax_t _Yp>
137 class __ll_add<_Xp, _Yp, 1> {
138 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
139 static const intmax_t max = -min;
141 static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
144 static const intmax_t value = _Xp + _Yp;
147 template <intmax_t _Xp, intmax_t _Yp>
148 class __ll_add<_Xp, _Yp, 0> {
150 static const intmax_t value = _Xp;
153 template <intmax_t _Xp, intmax_t _Yp>
154 class __ll_add<_Xp, _Yp, -1> {
155 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
156 static const intmax_t max = -min;
158 static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
161 static const intmax_t value = _Xp + _Yp;
164 template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
167 template <intmax_t _Xp, intmax_t _Yp>
168 class __ll_sub<_Xp, _Yp, 1> {
169 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
170 static const intmax_t max = -min;
172 static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
175 static const intmax_t value = _Xp - _Yp;
178 template <intmax_t _Xp, intmax_t _Yp>
179 class __ll_sub<_Xp, _Yp, 0> {
181 static const intmax_t value = _Xp;
184 template <intmax_t _Xp, intmax_t _Yp>
185 class __ll_sub<_Xp, _Yp, -1> {
186 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
187 static const intmax_t max = -min;
189 static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");
192 static const intmax_t value = _Xp - _Yp;
195 template <intmax_t _Xp, intmax_t _Yp>
197 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
198 static const intmax_t min = nan + 1;
199 static const intmax_t max = -min;
200 static const intmax_t __a_x = __static_abs<_Xp>::value;
201 static const intmax_t __a_y = __static_abs<_Yp>::value;
203 static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
206 static const intmax_t value = _Xp * _Yp;
209 template <intmax_t _Yp>
210 class __ll_mul<0, _Yp> {
212 static const intmax_t value = 0;
215 template <intmax_t _Xp>
216 class __ll_mul<_Xp, 0> {
218 static const intmax_t value = 0;
222 class __ll_mul<0, 0> {
224 static const intmax_t value = 0;
227 // Not actually used but left here in case needed in future maintenance
228 template <intmax_t _Xp, intmax_t _Yp>
230 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
231 static const intmax_t min = nan + 1;
232 static const intmax_t max = -min;
234 static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
237 static const intmax_t value = _Xp / _Yp;
240 template <intmax_t _Num, intmax_t _Den = 1>
241 class _LIBCPP_TEMPLATE_VIS ratio {
242 static_assert(__static_abs<_Num>::value >= 0, "ratio numerator is out of range");
243 static_assert(_Den != 0, "ratio divide by 0");
244 static_assert(__static_abs<_Den>::value > 0, "ratio denominator is out of range");
245 static _LIBCPP_CONSTEXPR const intmax_t __na = __static_abs<_Num>::value;
246 static _LIBCPP_CONSTEXPR const intmax_t __da = __static_abs<_Den>::value;
247 static _LIBCPP_CONSTEXPR const intmax_t __s = __static_sign<_Num>::value * __static_sign<_Den>::value;
248 static _LIBCPP_CONSTEXPR const intmax_t __gcd = __static_gcd<__na, __da>::value;
251 static _LIBCPP_CONSTEXPR const intmax_t num = __s * __na / __gcd;
252 static _LIBCPP_CONSTEXPR const intmax_t den = __da / __gcd;
254 typedef ratio<num, den> type;
257 template <intmax_t _Num, intmax_t _Den>
258 _LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::num;
260 template <intmax_t _Num, intmax_t _Den>
261 _LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::den;
264 struct __is_ratio : false_type {};
265 template <intmax_t _Num, intmax_t _Den>
266 struct __is_ratio<ratio<_Num, _Den> > : true_type {};
268 typedef ratio<1LL, 1000000000000000000LL> atto;
269 typedef ratio<1LL, 1000000000000000LL> femto;
270 typedef ratio<1LL, 1000000000000LL> pico;
271 typedef ratio<1LL, 1000000000LL> nano;
272 typedef ratio<1LL, 1000000LL> micro;
273 typedef ratio<1LL, 1000LL> milli;
274 typedef ratio<1LL, 100LL> centi;
275 typedef ratio<1LL, 10LL> deci;
276 typedef ratio< 10LL, 1LL> deca;
277 typedef ratio< 100LL, 1LL> hecto;
278 typedef ratio< 1000LL, 1LL> kilo;
279 typedef ratio< 1000000LL, 1LL> mega;
280 typedef ratio< 1000000000LL, 1LL> giga;
281 typedef ratio< 1000000000000LL, 1LL> tera;
282 typedef ratio< 1000000000000000LL, 1LL> peta;
283 typedef ratio<1000000000000000000LL, 1LL> exa;
285 template <class _R1, class _R2>
286 struct __ratio_multiply {
288 static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>::value;
289 static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>::value;
291 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
292 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
295 typedef typename ratio< __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,
296 __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value >::type type;
299 #ifndef _LIBCPP_CXX03_LANG
301 template <class _R1, class _R2>
302 using ratio_multiply = typename __ratio_multiply<_R1, _R2>::type;
304 #else // _LIBCPP_CXX03_LANG
306 template <class _R1, class _R2>
307 struct _LIBCPP_TEMPLATE_VIS ratio_multiply : public __ratio_multiply<_R1, _R2>::type {};
309 #endif // _LIBCPP_CXX03_LANG
311 template <class _R1, class _R2>
312 struct __ratio_divide {
314 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
315 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
317 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
318 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
321 typedef typename ratio< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
322 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::type type;
325 #ifndef _LIBCPP_CXX03_LANG
327 template <class _R1, class _R2>
328 using ratio_divide = typename __ratio_divide<_R1, _R2>::type;
330 #else // _LIBCPP_CXX03_LANG
332 template <class _R1, class _R2>
333 struct _LIBCPP_TEMPLATE_VIS ratio_divide : public __ratio_divide<_R1, _R2>::type {};
335 #endif // _LIBCPP_CXX03_LANG
337 template <class _R1, class _R2>
340 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
341 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
343 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
344 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
347 typedef typename ratio_multiply<
348 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
349 ratio< __ll_add< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
350 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::value,
351 _R2::den > >::type type;
354 #ifndef _LIBCPP_CXX03_LANG
356 template <class _R1, class _R2>
357 using ratio_add = typename __ratio_add<_R1, _R2>::type;
359 #else // _LIBCPP_CXX03_LANG
361 template <class _R1, class _R2>
362 struct _LIBCPP_TEMPLATE_VIS ratio_add : public __ratio_add<_R1, _R2>::type {};
364 #endif // _LIBCPP_CXX03_LANG
366 template <class _R1, class _R2>
367 struct __ratio_subtract {
369 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
370 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
372 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
373 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
376 typedef typename ratio_multiply<
377 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
378 ratio< __ll_sub< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
379 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::value,
380 _R2::den > >::type type;
383 #ifndef _LIBCPP_CXX03_LANG
385 template <class _R1, class _R2>
386 using ratio_subtract = typename __ratio_subtract<_R1, _R2>::type;
388 #else // _LIBCPP_CXX03_LANG
390 template <class _R1, class _R2>
391 struct _LIBCPP_TEMPLATE_VIS ratio_subtract : public __ratio_subtract<_R1, _R2>::type {};
393 #endif // _LIBCPP_CXX03_LANG
397 template <class _R1, class _R2>
398 struct _LIBCPP_TEMPLATE_VIS ratio_equal : _BoolConstant<(_R1::num == _R2::num && _R1::den == _R2::den)> {
399 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
400 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
403 template <class _R1, class _R2>
404 struct _LIBCPP_TEMPLATE_VIS ratio_not_equal : _BoolConstant<!ratio_equal<_R1, _R2>::value> {
405 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
406 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
414 intmax_t _Q1 = _R1::num / _R1::den,
415 intmax_t _M1 = _R1::num % _R1::den,
416 intmax_t _Q2 = _R2::num / _R2::den,
417 intmax_t _M2 = _R2::num % _R2::den>
418 struct __ratio_less1 {
419 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
422 template <class _R1, class _R2, bool _Odd, intmax_t _Qp>
423 struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0> {
424 static const bool value = false;
427 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>
428 struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2> {
429 static const bool value = !_Odd;
432 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1>
433 struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0> {
434 static const bool value = _Odd;
437 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1, intmax_t _M2>
438 struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2> {
439 static const bool value = __ratio_less1<ratio<_R1::den, _M1>, ratio<_R2::den, _M2>, !_Odd>::value;
444 intmax_t _S1 = __static_sign<_R1::num>::value,
445 intmax_t _S2 = __static_sign<_R2::num>::value>
446 struct __ratio_less {
447 static const bool value = _S1 < _S2;
450 template <class _R1, class _R2>
451 struct __ratio_less<_R1, _R2, 1LL, 1LL> {
452 static const bool value = __ratio_less1<_R1, _R2>::value;
455 template <class _R1, class _R2>
456 struct __ratio_less<_R1, _R2, -1LL, -1LL> {
457 static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
460 template <class _R1, class _R2>
461 struct _LIBCPP_TEMPLATE_VIS ratio_less : _BoolConstant<__ratio_less<_R1, _R2>::value> {
462 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
463 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
466 template <class _R1, class _R2>
467 struct _LIBCPP_TEMPLATE_VIS ratio_less_equal : _BoolConstant<!ratio_less<_R2, _R1>::value> {
468 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
469 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
472 template <class _R1, class _R2>
473 struct _LIBCPP_TEMPLATE_VIS ratio_greater : _BoolConstant<ratio_less<_R2, _R1>::value> {
474 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
475 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
478 template <class _R1, class _R2>
479 struct _LIBCPP_TEMPLATE_VIS ratio_greater_equal : _BoolConstant<!ratio_less<_R1, _R2>::value> {
480 static_assert(__is_ratio<_R1>::value, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
481 static_assert(__is_ratio<_R2>::value, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
484 template <class _R1, class _R2>
486 typedef ratio<__static_gcd<_R1::num, _R2::num>::value, __static_lcm<_R1::den, _R2::den>::value> type;
489 #if _LIBCPP_STD_VER >= 17
490 template <class _R1, class _R2>
491 inline constexpr bool ratio_equal_v = ratio_equal<_R1, _R2>::value;
493 template <class _R1, class _R2>
494 inline constexpr bool ratio_not_equal_v = ratio_not_equal<_R1, _R2>::value;
496 template <class _R1, class _R2>
497 inline constexpr bool ratio_less_v = ratio_less<_R1, _R2>::value;
499 template <class _R1, class _R2>
500 inline constexpr bool ratio_less_equal_v = ratio_less_equal<_R1, _R2>::value;
502 template <class _R1, class _R2>
503 inline constexpr bool ratio_greater_v = ratio_greater<_R1, _R2>::value;
505 template <class _R1, class _R2>
506 inline constexpr bool ratio_greater_equal_v = ratio_greater_equal<_R1, _R2>::value;
509 _LIBCPP_END_NAMESPACE_STD
513 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
514 # include <__cxx03/type_traits>
517 #endif // _LIBCPP_RATIO