2 //===---------------------------- ratio -----------------------------------===//
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 typedef ratio<1, 1000000000000000000000000> yocto; // not supported
44 typedef ratio<1, 1000000000000000000000> zepto; // not supported
45 typedef ratio<1, 1000000000000000000> atto;
46 typedef ratio<1, 1000000000000000> femto;
47 typedef ratio<1, 1000000000000> pico;
48 typedef ratio<1, 1000000000> nano;
49 typedef ratio<1, 1000000> micro;
50 typedef ratio<1, 1000> milli;
51 typedef ratio<1, 100> centi;
52 typedef ratio<1, 10> deci;
53 typedef ratio< 10, 1> deca;
54 typedef ratio< 100, 1> hecto;
55 typedef ratio< 1000, 1> kilo;
56 typedef ratio< 1000000, 1> mega;
57 typedef ratio< 1000000000, 1> giga;
58 typedef ratio< 1000000000000, 1> tera;
59 typedef ratio< 1000000000000000, 1> peta;
60 typedef ratio< 1000000000000000000, 1> exa;
61 typedef ratio< 1000000000000000000000, 1> zetta; // not supported
62 typedef ratio<1000000000000000000000000, 1> yotta; // not supported
64 // 20.11.5, ratio comparison
65 template <class R1, class R2> inline constexpr bool ratio_equal_v
66 = ratio_equal<R1, R2>::value; // C++17
67 template <class R1, class R2> inline constexpr bool ratio_not_equal_v
68 = ratio_not_equal<R1, R2>::value; // C++17
69 template <class R1, class R2> inline constexpr bool ratio_less_v
70 = ratio_less<R1, R2>::value; // C++17
71 template <class R1, class R2> inline constexpr bool ratio_less_equal_v
72 = ratio_less_equal<R1, R2>::value; // C++17
73 template <class R1, class R2> inline constexpr bool ratio_greater_v
74 = ratio_greater<R1, R2>::value; // C++17
75 template <class R1, class R2> inline constexpr bool ratio_greater_equal_v
76 = ratio_greater_equal<R1, R2>::value; // C++17
83 #include <type_traits>
85 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
86 #pragma GCC system_header
90 #include <__undef_macros>
93 _LIBCPP_BEGIN_NAMESPACE_STD
97 template <intmax_t _Xp, intmax_t _Yp>
100 static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value;
103 template <intmax_t _Xp>
104 struct __static_gcd<_Xp, 0>
106 static const intmax_t value = _Xp;
110 struct __static_gcd<0, 0>
112 static const intmax_t value = 1;
117 template <intmax_t _Xp, intmax_t _Yp>
120 static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp;
123 template <intmax_t _Xp>
126 static const intmax_t value = _Xp < 0 ? -_Xp : _Xp;
129 template <intmax_t _Xp>
132 static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
135 template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
138 template <intmax_t _Xp, intmax_t _Yp>
139 class __ll_add<_Xp, _Yp, 1>
141 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
142 static const intmax_t max = -min;
144 static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
146 static const intmax_t value = _Xp + _Yp;
149 template <intmax_t _Xp, intmax_t _Yp>
150 class __ll_add<_Xp, _Yp, 0>
153 static const intmax_t value = _Xp;
156 template <intmax_t _Xp, intmax_t _Yp>
157 class __ll_add<_Xp, _Yp, -1>
159 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
160 static const intmax_t max = -min;
162 static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
164 static const intmax_t value = _Xp + _Yp;
167 template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
170 template <intmax_t _Xp, intmax_t _Yp>
171 class __ll_sub<_Xp, _Yp, 1>
173 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
174 static const intmax_t max = -min;
176 static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
178 static const intmax_t value = _Xp - _Yp;
181 template <intmax_t _Xp, intmax_t _Yp>
182 class __ll_sub<_Xp, _Yp, 0>
185 static const intmax_t value = _Xp;
188 template <intmax_t _Xp, intmax_t _Yp>
189 class __ll_sub<_Xp, _Yp, -1>
191 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
192 static const intmax_t max = -min;
194 static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");
196 static const intmax_t value = _Xp - _Yp;
199 template <intmax_t _Xp, intmax_t _Yp>
202 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
203 static const intmax_t min = nan + 1;
204 static const intmax_t max = -min;
205 static const intmax_t __a_x = __static_abs<_Xp>::value;
206 static const intmax_t __a_y = __static_abs<_Yp>::value;
208 static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
210 static const intmax_t value = _Xp * _Yp;
213 template <intmax_t _Yp>
214 class __ll_mul<0, _Yp>
217 static const intmax_t value = 0;
220 template <intmax_t _Xp>
221 class __ll_mul<_Xp, 0>
224 static const intmax_t value = 0;
231 static const intmax_t value = 0;
234 // Not actually used but left here in case needed in future maintenance
235 template <intmax_t _Xp, intmax_t _Yp>
238 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
239 static const intmax_t min = nan + 1;
240 static const intmax_t max = -min;
242 static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
244 static const intmax_t value = _Xp / _Yp;
247 template <intmax_t _Num, intmax_t _Den = 1>
248 class _LIBCPP_TEMPLATE_VIS ratio
250 static_assert(__static_abs<_Num>::value >= 0, "ratio numerator is out of range");
251 static_assert(_Den != 0, "ratio divide by 0");
252 static_assert(__static_abs<_Den>::value > 0, "ratio denominator is out of range");
253 static _LIBCPP_CONSTEXPR const intmax_t __na = __static_abs<_Num>::value;
254 static _LIBCPP_CONSTEXPR const intmax_t __da = __static_abs<_Den>::value;
255 static _LIBCPP_CONSTEXPR const intmax_t __s = __static_sign<_Num>::value * __static_sign<_Den>::value;
256 static _LIBCPP_CONSTEXPR const intmax_t __gcd = __static_gcd<__na, __da>::value;
258 static _LIBCPP_CONSTEXPR const intmax_t num = __s * __na / __gcd;
259 static _LIBCPP_CONSTEXPR const intmax_t den = __da / __gcd;
261 typedef ratio<num, den> type;
264 template <intmax_t _Num, intmax_t _Den>
265 _LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::num;
267 template <intmax_t _Num, intmax_t _Den>
268 _LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::den;
270 template <class _Tp> struct __is_ratio : false_type {};
271 template <intmax_t _Num, intmax_t _Den> struct __is_ratio<ratio<_Num, _Den> > : true_type {};
273 typedef ratio<1LL, 1000000000000000000LL> atto;
274 typedef ratio<1LL, 1000000000000000LL> femto;
275 typedef ratio<1LL, 1000000000000LL> pico;
276 typedef ratio<1LL, 1000000000LL> nano;
277 typedef ratio<1LL, 1000000LL> micro;
278 typedef ratio<1LL, 1000LL> milli;
279 typedef ratio<1LL, 100LL> centi;
280 typedef ratio<1LL, 10LL> deci;
281 typedef ratio< 10LL, 1LL> deca;
282 typedef ratio< 100LL, 1LL> hecto;
283 typedef ratio< 1000LL, 1LL> kilo;
284 typedef ratio< 1000000LL, 1LL> mega;
285 typedef ratio< 1000000000LL, 1LL> giga;
286 typedef ratio< 1000000000000LL, 1LL> tera;
287 typedef ratio< 1000000000000000LL, 1LL> peta;
288 typedef ratio<1000000000000000000LL, 1LL> exa;
290 template <class _R1, class _R2>
291 struct __ratio_multiply
294 static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>::value;
295 static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>::value;
297 typedef typename ratio
299 __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,
300 __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value
304 #ifndef _LIBCPP_CXX03_LANG
306 template <class _R1, class _R2> using ratio_multiply
307 = typename __ratio_multiply<_R1, _R2>::type;
309 #else // _LIBCPP_CXX03_LANG
311 template <class _R1, class _R2>
312 struct _LIBCPP_TEMPLATE_VIS ratio_multiply
313 : public __ratio_multiply<_R1, _R2>::type {};
315 #endif // _LIBCPP_CXX03_LANG
317 template <class _R1, class _R2>
318 struct __ratio_divide
321 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
322 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
324 typedef typename ratio
326 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
327 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
331 #ifndef _LIBCPP_CXX03_LANG
333 template <class _R1, class _R2> using ratio_divide
334 = typename __ratio_divide<_R1, _R2>::type;
336 #else // _LIBCPP_CXX03_LANG
338 template <class _R1, class _R2>
339 struct _LIBCPP_TEMPLATE_VIS ratio_divide
340 : public __ratio_divide<_R1, _R2>::type {};
342 #endif // _LIBCPP_CXX03_LANG
344 template <class _R1, class _R2>
348 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
349 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
351 typedef typename ratio_multiply
353 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
358 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
359 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
366 #ifndef _LIBCPP_CXX03_LANG
368 template <class _R1, class _R2> using ratio_add
369 = typename __ratio_add<_R1, _R2>::type;
371 #else // _LIBCPP_CXX03_LANG
373 template <class _R1, class _R2>
374 struct _LIBCPP_TEMPLATE_VIS ratio_add
375 : public __ratio_add<_R1, _R2>::type {};
377 #endif // _LIBCPP_CXX03_LANG
379 template <class _R1, class _R2>
380 struct __ratio_subtract
383 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
384 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
386 typedef typename ratio_multiply
388 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
393 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
394 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
401 #ifndef _LIBCPP_CXX03_LANG
403 template <class _R1, class _R2> using ratio_subtract
404 = typename __ratio_subtract<_R1, _R2>::type;
406 #else // _LIBCPP_CXX03_LANG
408 template <class _R1, class _R2>
409 struct _LIBCPP_TEMPLATE_VIS ratio_subtract
410 : public __ratio_subtract<_R1, _R2>::type {};
412 #endif // _LIBCPP_CXX03_LANG
416 template <class _R1, class _R2>
417 struct _LIBCPP_TEMPLATE_VIS ratio_equal
418 : public _LIBCPP_BOOL_CONSTANT((_R1::num == _R2::num && _R1::den == _R2::den)) {};
420 template <class _R1, class _R2>
421 struct _LIBCPP_TEMPLATE_VIS ratio_not_equal
422 : public _LIBCPP_BOOL_CONSTANT((!ratio_equal<_R1, _R2>::value)) {};
426 template <class _R1, class _R2, bool _Odd = false,
427 intmax_t _Q1 = _R1::num / _R1::den, intmax_t _M1 = _R1::num % _R1::den,
428 intmax_t _Q2 = _R2::num / _R2::den, intmax_t _M2 = _R2::num % _R2::den>
431 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
434 template <class _R1, class _R2, bool _Odd, intmax_t _Qp>
435 struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0>
437 static const bool value = false;
440 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>
441 struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2>
443 static const bool value = !_Odd;
446 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1>
447 struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0>
449 static const bool value = _Odd;
452 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1,
454 struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2>
456 static const bool value = __ratio_less1<ratio<_R1::den, _M1>,
457 ratio<_R2::den, _M2>, !_Odd>::value;
460 template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>::value,
461 intmax_t _S2 = __static_sign<_R2::num>::value>
464 static const bool value = _S1 < _S2;
467 template <class _R1, class _R2>
468 struct __ratio_less<_R1, _R2, 1LL, 1LL>
470 static const bool value = __ratio_less1<_R1, _R2>::value;
473 template <class _R1, class _R2>
474 struct __ratio_less<_R1, _R2, -1LL, -1LL>
476 static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
479 template <class _R1, class _R2>
480 struct _LIBCPP_TEMPLATE_VIS ratio_less
481 : public _LIBCPP_BOOL_CONSTANT((__ratio_less<_R1, _R2>::value)) {};
483 template <class _R1, class _R2>
484 struct _LIBCPP_TEMPLATE_VIS ratio_less_equal
485 : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R2, _R1>::value)) {};
487 template <class _R1, class _R2>
488 struct _LIBCPP_TEMPLATE_VIS ratio_greater
489 : public _LIBCPP_BOOL_CONSTANT((ratio_less<_R2, _R1>::value)) {};
491 template <class _R1, class _R2>
492 struct _LIBCPP_TEMPLATE_VIS ratio_greater_equal
493 : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R1, _R2>::value)) {};
495 template <class _R1, class _R2>
498 typedef ratio<__static_gcd<_R1::num, _R2::num>::value,
499 __static_lcm<_R1::den, _R2::den>::value> type;
502 #if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES)
503 template <class _R1, class _R2>
504 _LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_equal_v
505 = ratio_equal<_R1, _R2>::value;
507 template <class _R1, class _R2>
508 _LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_not_equal_v
509 = ratio_not_equal<_R1, _R2>::value;
511 template <class _R1, class _R2>
512 _LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_less_v
513 = ratio_less<_R1, _R2>::value;
515 template <class _R1, class _R2>
516 _LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_less_equal_v
517 = ratio_less_equal<_R1, _R2>::value;
519 template <class _R1, class _R2>
520 _LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_greater_v
521 = ratio_greater<_R1, _R2>::value;
523 template <class _R1, class _R2>
524 _LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_greater_equal_v
525 = ratio_greater_equal<_R1, _R2>::value;
528 _LIBCPP_END_NAMESPACE_STD
532 #endif // _LIBCPP_RATIO