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 #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
85 # include <__cxx03/ratio>
88 # include <__type_traits/integral_constant.h>
93 # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
94 # pragma GCC system_header
98 # include <__undef_macros>
100 _LIBCPP_BEGIN_NAMESPACE_STD
104 template <intmax_t _Xp, intmax_t _Yp>
105 inline const intmax_t __static_gcd = __static_gcd<_Yp, _Xp % _Yp>;
107 template <intmax_t _Xp>
108 inline const intmax_t __static_gcd<_Xp, 0> = _Xp;
111 inline const intmax_t __static_gcd<0, 0> = 1;
115 template <intmax_t _Xp, intmax_t _Yp>
116 inline const intmax_t __static_lcm = _Xp / __static_gcd<_Xp, _Yp> * _Yp;
118 template <intmax_t _Xp>
119 inline const intmax_t __static_abs = _Xp < 0 ? -_Xp : _Xp;
121 template <intmax_t _Xp>
122 inline const intmax_t __static_sign = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
124 template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp> >
127 template <intmax_t _Xp, intmax_t _Yp>
128 class __ll_add<_Xp, _Yp, 1> {
129 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
130 static const intmax_t max = -min;
132 static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
135 static const intmax_t value = _Xp + _Yp;
138 template <intmax_t _Xp, intmax_t _Yp>
139 class __ll_add<_Xp, _Yp, 0> {
141 static const intmax_t value = _Xp;
144 template <intmax_t _Xp, intmax_t _Yp>
145 class __ll_add<_Xp, _Yp, -1> {
146 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
147 static const intmax_t max = -min;
149 static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
152 static const intmax_t value = _Xp + _Yp;
155 template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp> >
158 template <intmax_t _Xp, intmax_t _Yp>
159 class __ll_sub<_Xp, _Yp, 1> {
160 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
161 static const intmax_t max = -min;
163 static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
166 static const intmax_t value = _Xp - _Yp;
169 template <intmax_t _Xp, intmax_t _Yp>
170 class __ll_sub<_Xp, _Yp, 0> {
172 static const intmax_t value = _Xp;
175 template <intmax_t _Xp, intmax_t _Yp>
176 class __ll_sub<_Xp, _Yp, -1> {
177 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
178 static const intmax_t max = -min;
180 static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");
183 static const intmax_t value = _Xp - _Yp;
186 template <intmax_t _Xp, intmax_t _Yp>
188 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
189 static const intmax_t min = nan + 1;
190 static const intmax_t max = -min;
191 static const intmax_t __a_x = __static_abs<_Xp>;
192 static const intmax_t __a_y = __static_abs<_Yp>;
194 static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
197 static const intmax_t value = _Xp * _Yp;
200 template <intmax_t _Yp>
201 class __ll_mul<0, _Yp> {
203 static const intmax_t value = 0;
206 template <intmax_t _Xp>
207 class __ll_mul<_Xp, 0> {
209 static const intmax_t value = 0;
213 class __ll_mul<0, 0> {
215 static const intmax_t value = 0;
218 // Not actually used but left here in case needed in future maintenance
219 template <intmax_t _Xp, intmax_t _Yp>
221 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
222 static const intmax_t min = nan + 1;
223 static const intmax_t max = -min;
225 static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
228 static const intmax_t value = _Xp / _Yp;
231 template <intmax_t _Num, intmax_t _Den = 1>
232 class _LIBCPP_TEMPLATE_VIS ratio {
233 static_assert(__static_abs<_Num> >= 0, "ratio numerator is out of range");
234 static_assert(_Den != 0, "ratio divide by 0");
235 static_assert(__static_abs<_Den> > 0, "ratio denominator is out of range");
236 static _LIBCPP_CONSTEXPR const intmax_t __na = __static_abs<_Num>;
237 static _LIBCPP_CONSTEXPR const intmax_t __da = __static_abs<_Den>;
238 static _LIBCPP_CONSTEXPR const intmax_t __s = __static_sign<_Num> * __static_sign<_Den>;
239 static _LIBCPP_CONSTEXPR const intmax_t __gcd = __static_gcd<__na, __da>;
242 static inline _LIBCPP_CONSTEXPR const intmax_t num = __s * __na / __gcd;
243 static inline _LIBCPP_CONSTEXPR const intmax_t den = __da / __gcd;
245 typedef ratio<num, den> type;
249 inline const bool __is_ratio_v = false;
251 template <intmax_t _Num, intmax_t _Den>
252 inline const bool __is_ratio_v<ratio<_Num, _Den> > = true;
254 typedef ratio<1LL, 1000000000000000000LL> atto;
255 typedef ratio<1LL, 1000000000000000LL> femto;
256 typedef ratio<1LL, 1000000000000LL> pico;
257 typedef ratio<1LL, 1000000000LL> nano;
258 typedef ratio<1LL, 1000000LL> micro;
259 typedef ratio<1LL, 1000LL> milli;
260 typedef ratio<1LL, 100LL> centi;
261 typedef ratio<1LL, 10LL> deci;
262 typedef ratio< 10LL, 1LL> deca;
263 typedef ratio< 100LL, 1LL> hecto;
264 typedef ratio< 1000LL, 1LL> kilo;
265 typedef ratio< 1000000LL, 1LL> mega;
266 typedef ratio< 1000000000LL, 1LL> giga;
267 typedef ratio< 1000000000000LL, 1LL> tera;
268 typedef ratio< 1000000000000000LL, 1LL> peta;
269 typedef ratio<1000000000000000000LL, 1LL> exa;
271 template <class _R1, class _R2>
272 struct __ratio_multiply {
274 static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>;
275 static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>;
277 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
278 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
281 typedef typename ratio< __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,
282 __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value >::type type;
285 # ifndef _LIBCPP_CXX03_LANG
287 template <class _R1, class _R2>
288 using ratio_multiply = typename __ratio_multiply<_R1, _R2>::type;
290 # else // _LIBCPP_CXX03_LANG
292 template <class _R1, class _R2>
293 struct _LIBCPP_TEMPLATE_VIS ratio_multiply : public __ratio_multiply<_R1, _R2>::type {};
295 # endif // _LIBCPP_CXX03_LANG
297 template <class _R1, class _R2>
298 struct __ratio_divide {
300 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>;
301 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>;
303 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
304 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
307 typedef typename ratio< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
308 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::type type;
311 # ifndef _LIBCPP_CXX03_LANG
313 template <class _R1, class _R2>
314 using ratio_divide = typename __ratio_divide<_R1, _R2>::type;
316 # else // _LIBCPP_CXX03_LANG
318 template <class _R1, class _R2>
319 struct _LIBCPP_TEMPLATE_VIS ratio_divide : public __ratio_divide<_R1, _R2>::type {};
321 # endif // _LIBCPP_CXX03_LANG
323 template <class _R1, class _R2>
326 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>;
327 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>;
329 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
330 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
333 typedef typename ratio_multiply<
334 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
335 ratio< __ll_add< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
336 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::value,
337 _R2::den > >::type type;
340 # ifndef _LIBCPP_CXX03_LANG
342 template <class _R1, class _R2>
343 using ratio_add = typename __ratio_add<_R1, _R2>::type;
345 # else // _LIBCPP_CXX03_LANG
347 template <class _R1, class _R2>
348 struct _LIBCPP_TEMPLATE_VIS ratio_add : public __ratio_add<_R1, _R2>::type {};
350 # endif // _LIBCPP_CXX03_LANG
352 template <class _R1, class _R2>
353 struct __ratio_subtract {
355 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>;
356 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>;
358 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
359 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
362 typedef typename ratio_multiply<
363 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
364 ratio< __ll_sub< __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
365 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value >::value,
366 _R2::den > >::type type;
369 # ifndef _LIBCPP_CXX03_LANG
371 template <class _R1, class _R2>
372 using ratio_subtract = typename __ratio_subtract<_R1, _R2>::type;
374 # else // _LIBCPP_CXX03_LANG
376 template <class _R1, class _R2>
377 struct _LIBCPP_TEMPLATE_VIS ratio_subtract : public __ratio_subtract<_R1, _R2>::type {};
379 # endif // _LIBCPP_CXX03_LANG
383 template <class _R1, class _R2>
384 struct _LIBCPP_TEMPLATE_VIS ratio_equal : _BoolConstant<(_R1::num == _R2::num && _R1::den == _R2::den)> {
385 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
386 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
389 template <class _R1, class _R2>
390 struct _LIBCPP_TEMPLATE_VIS ratio_not_equal : _BoolConstant<!ratio_equal<_R1, _R2>::value> {
391 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
392 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
400 intmax_t _Q1 = _R1::num / _R1::den,
401 intmax_t _M1 = _R1::num % _R1::den,
402 intmax_t _Q2 = _R2::num / _R2::den,
403 intmax_t _M2 = _R2::num % _R2::den>
404 struct __ratio_less1 {
405 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
408 template <class _R1, class _R2, bool _Odd, intmax_t _Qp>
409 struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0> {
410 static const bool value = false;
413 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>
414 struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2> {
415 static const bool value = !_Odd;
418 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1>
419 struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0> {
420 static const bool value = _Odd;
423 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1, intmax_t _M2>
424 struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2> {
425 static const bool value = __ratio_less1<ratio<_R1::den, _M1>, ratio<_R2::den, _M2>, !_Odd>::value;
428 template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>, intmax_t _S2 = __static_sign<_R2::num> >
429 struct __ratio_less {
430 static const bool value = _S1 < _S2;
433 template <class _R1, class _R2>
434 struct __ratio_less<_R1, _R2, 1LL, 1LL> {
435 static const bool value = __ratio_less1<_R1, _R2>::value;
438 template <class _R1, class _R2>
439 struct __ratio_less<_R1, _R2, -1LL, -1LL> {
440 static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
443 template <class _R1, class _R2>
444 struct _LIBCPP_TEMPLATE_VIS ratio_less : _BoolConstant<__ratio_less<_R1, _R2>::value> {
445 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
446 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
449 template <class _R1, class _R2>
450 struct _LIBCPP_TEMPLATE_VIS ratio_less_equal : _BoolConstant<!ratio_less<_R2, _R1>::value> {
451 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
452 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
455 template <class _R1, class _R2>
456 struct _LIBCPP_TEMPLATE_VIS ratio_greater : _BoolConstant<ratio_less<_R2, _R1>::value> {
457 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
458 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
461 template <class _R1, class _R2>
462 struct _LIBCPP_TEMPLATE_VIS ratio_greater_equal : _BoolConstant<!ratio_less<_R1, _R2>::value> {
463 static_assert(__is_ratio_v<_R1>, "[ratio.general]/2 requires R1 to be a specialisation of the ratio template");
464 static_assert(__is_ratio_v<_R2>, "[ratio.general]/2 requires R2 to be a specialisation of the ratio template");
467 template <class _R1, class _R2>
468 using __ratio_gcd = ratio<__static_gcd<_R1::num, _R2::num>, __static_lcm<_R1::den, _R2::den> >;
470 # if _LIBCPP_STD_VER >= 17
471 template <class _R1, class _R2>
472 inline constexpr bool ratio_equal_v = ratio_equal<_R1, _R2>::value;
474 template <class _R1, class _R2>
475 inline constexpr bool ratio_not_equal_v = ratio_not_equal<_R1, _R2>::value;
477 template <class _R1, class _R2>
478 inline constexpr bool ratio_less_v = ratio_less<_R1, _R2>::value;
480 template <class _R1, class _R2>
481 inline constexpr bool ratio_less_equal_v = ratio_less_equal<_R1, _R2>::value;
483 template <class _R1, class _R2>
484 inline constexpr bool ratio_greater_v = ratio_greater<_R1, _R2>::value;
486 template <class _R1, class _R2>
487 inline constexpr bool ratio_greater_equal_v = ratio_greater_equal<_R1, _R2>::value;
490 _LIBCPP_END_NAMESPACE_STD
494 # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
495 # include <type_traits>
497 #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
499 #endif // _LIBCPP_RATIO