2 //===---------------------------- ratio -----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
20 template <intmax_t N, intmax_t D = 1>
24 static constexpr intmax_t num;
25 static constexpr intmax_t den;
26 typedef ratio<num, den> type;
30 template <class R1, class R2> using ratio_add = ...;
31 template <class R1, class R2> using ratio_subtract = ...;
32 template <class R1, class R2> using ratio_multiply = ...;
33 template <class R1, class R2> using ratio_divide = ...;
36 template <class R1, class R2> struct ratio_equal;
37 template <class R1, class R2> struct ratio_not_equal;
38 template <class R1, class R2> struct ratio_less;
39 template <class R1, class R2> struct ratio_less_equal;
40 template <class R1, class R2> struct ratio_greater;
41 template <class R1, class R2> struct ratio_greater_equal;
43 // convenience SI typedefs
44 typedef ratio<1, 1000000000000000000000000> yocto; // not supported
45 typedef ratio<1, 1000000000000000000000> zepto; // not supported
46 typedef ratio<1, 1000000000000000000> atto;
47 typedef ratio<1, 1000000000000000> femto;
48 typedef ratio<1, 1000000000000> pico;
49 typedef ratio<1, 1000000000> nano;
50 typedef ratio<1, 1000000> micro;
51 typedef ratio<1, 1000> milli;
52 typedef ratio<1, 100> centi;
53 typedef ratio<1, 10> deci;
54 typedef ratio< 10, 1> deca;
55 typedef ratio< 100, 1> hecto;
56 typedef ratio< 1000, 1> kilo;
57 typedef ratio< 1000000, 1> mega;
58 typedef ratio< 1000000000, 1> giga;
59 typedef ratio< 1000000000000, 1> tera;
60 typedef ratio< 1000000000000000, 1> peta;
61 typedef ratio< 1000000000000000000, 1> exa;
62 typedef ratio< 1000000000000000000000, 1> zetta; // not supported
63 typedef ratio<1000000000000000000000000, 1> yotta; // not supported
65 // 20.11.5, ratio comparison
66 template <class R1, class R2> constexpr bool ratio_equal_v
67 = ratio_equal<R1, R2>::value; // C++17
68 template <class R1, class R2> constexpr bool ratio_not_equal_v
69 = ratio_not_equal<R1, R2>::value; // C++17
70 template <class R1, class R2> constexpr bool ratio_less_v
71 = ratio_less<R1, R2>::value; // C++17
72 template <class R1, class R2> constexpr bool ratio_less_equal_v
73 = ratio_less_equal<R1, R2>::value; // C++17
74 template <class R1, class R2> constexpr bool ratio_greater_v
75 = ratio_greater<R1, R2>::value; // C++17
76 template <class R1, class R2> constexpr bool ratio_greater_equal_v
77 = ratio_greater_equal<R1, R2>::value; // C++17
84 #include <type_traits>
86 #include <__undef_min_max>
88 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
89 #pragma GCC system_header
92 _LIBCPP_BEGIN_NAMESPACE_STD
96 template <intmax_t _Xp, intmax_t _Yp>
99 static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value;
102 template <intmax_t _Xp>
103 struct __static_gcd<_Xp, 0>
105 static const intmax_t value = _Xp;
109 struct __static_gcd<0, 0>
111 static const intmax_t value = 1;
116 template <intmax_t _Xp, intmax_t _Yp>
119 static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp;
122 template <intmax_t _Xp>
125 static const intmax_t value = _Xp < 0 ? -_Xp : _Xp;
128 template <intmax_t _Xp>
131 static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
134 template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
137 template <intmax_t _Xp, intmax_t _Yp>
138 class __ll_add<_Xp, _Yp, 1>
140 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
141 static const intmax_t max = -min;
143 static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
145 static const intmax_t value = _Xp + _Yp;
148 template <intmax_t _Xp, intmax_t _Yp>
149 class __ll_add<_Xp, _Yp, 0>
152 static const intmax_t value = _Xp;
155 template <intmax_t _Xp, intmax_t _Yp>
156 class __ll_add<_Xp, _Yp, -1>
158 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
159 static const intmax_t max = -min;
161 static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
163 static const intmax_t value = _Xp + _Yp;
166 template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
169 template <intmax_t _Xp, intmax_t _Yp>
170 class __ll_sub<_Xp, _Yp, 1>
172 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
173 static const intmax_t max = -min;
175 static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
177 static const intmax_t value = _Xp - _Yp;
180 template <intmax_t _Xp, intmax_t _Yp>
181 class __ll_sub<_Xp, _Yp, 0>
184 static const intmax_t value = _Xp;
187 template <intmax_t _Xp, intmax_t _Yp>
188 class __ll_sub<_Xp, _Yp, -1>
190 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
191 static const intmax_t max = -min;
193 static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");
195 static const intmax_t value = _Xp - _Yp;
198 template <intmax_t _Xp, intmax_t _Yp>
201 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
202 static const intmax_t min = nan + 1;
203 static const intmax_t max = -min;
204 static const intmax_t __a_x = __static_abs<_Xp>::value;
205 static const intmax_t __a_y = __static_abs<_Yp>::value;
207 static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
209 static const intmax_t value = _Xp * _Yp;
212 template <intmax_t _Yp>
213 class __ll_mul<0, _Yp>
216 static const intmax_t value = 0;
219 template <intmax_t _Xp>
220 class __ll_mul<_Xp, 0>
223 static const intmax_t value = 0;
230 static const intmax_t value = 0;
233 // Not actually used but left here in case needed in future maintenance
234 template <intmax_t _Xp, intmax_t _Yp>
237 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
238 static const intmax_t min = nan + 1;
239 static const intmax_t max = -min;
241 static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
243 static const intmax_t value = _Xp / _Yp;
246 template <intmax_t _Num, intmax_t _Den = 1>
247 class _LIBCPP_TEMPLATE_VIS ratio
249 static_assert(__static_abs<_Num>::value >= 0, "ratio numerator is out of range");
250 static_assert(_Den != 0, "ratio divide by 0");
251 static_assert(__static_abs<_Den>::value > 0, "ratio denominator is out of range");
252 static _LIBCPP_CONSTEXPR const intmax_t __na = __static_abs<_Num>::value;
253 static _LIBCPP_CONSTEXPR const intmax_t __da = __static_abs<_Den>::value;
254 static _LIBCPP_CONSTEXPR const intmax_t __s = __static_sign<_Num>::value * __static_sign<_Den>::value;
255 static _LIBCPP_CONSTEXPR const intmax_t __gcd = __static_gcd<__na, __da>::value;
257 static _LIBCPP_CONSTEXPR const intmax_t num = __s * __na / __gcd;
258 static _LIBCPP_CONSTEXPR const intmax_t den = __da / __gcd;
260 typedef ratio<num, den> type;
263 template <intmax_t _Num, intmax_t _Den>
264 _LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::num;
266 template <intmax_t _Num, intmax_t _Den>
267 _LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::den;
269 template <class _Tp> struct __is_ratio : false_type {};
270 template <intmax_t _Num, intmax_t _Den> struct __is_ratio<ratio<_Num, _Den> > : true_type {};
272 typedef ratio<1LL, 1000000000000000000LL> atto;
273 typedef ratio<1LL, 1000000000000000LL> femto;
274 typedef ratio<1LL, 1000000000000LL> pico;
275 typedef ratio<1LL, 1000000000LL> nano;
276 typedef ratio<1LL, 1000000LL> micro;
277 typedef ratio<1LL, 1000LL> milli;
278 typedef ratio<1LL, 100LL> centi;
279 typedef ratio<1LL, 10LL> deci;
280 typedef ratio< 10LL, 1LL> deca;
281 typedef ratio< 100LL, 1LL> hecto;
282 typedef ratio< 1000LL, 1LL> kilo;
283 typedef ratio< 1000000LL, 1LL> mega;
284 typedef ratio< 1000000000LL, 1LL> giga;
285 typedef ratio< 1000000000000LL, 1LL> tera;
286 typedef ratio< 1000000000000000LL, 1LL> peta;
287 typedef ratio<1000000000000000000LL, 1LL> exa;
289 template <class _R1, class _R2>
290 struct __ratio_multiply
293 static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>::value;
294 static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>::value;
296 typedef typename ratio
298 __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,
299 __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value
303 #ifndef _LIBCPP_CXX03_LANG
305 template <class _R1, class _R2> using ratio_multiply
306 = typename __ratio_multiply<_R1, _R2>::type;
308 #else // _LIBCPP_CXX03_LANG
310 template <class _R1, class _R2>
311 struct _LIBCPP_TEMPLATE_VIS ratio_multiply
312 : public __ratio_multiply<_R1, _R2>::type {};
314 #endif // _LIBCPP_CXX03_LANG
316 template <class _R1, class _R2>
317 struct __ratio_divide
320 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
321 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
323 typedef typename ratio
325 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
326 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
330 #ifndef _LIBCPP_CXX03_LANG
332 template <class _R1, class _R2> using ratio_divide
333 = typename __ratio_divide<_R1, _R2>::type;
335 #else // _LIBCPP_CXX03_LANG
337 template <class _R1, class _R2>
338 struct _LIBCPP_TEMPLATE_VIS ratio_divide
339 : public __ratio_divide<_R1, _R2>::type {};
341 #endif // _LIBCPP_CXX03_LANG
343 template <class _R1, class _R2>
347 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
348 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
350 typedef typename ratio_multiply
352 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
357 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
358 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
365 #ifndef _LIBCPP_CXX03_LANG
367 template <class _R1, class _R2> using ratio_add
368 = typename __ratio_add<_R1, _R2>::type;
370 #else // _LIBCPP_CXX03_LANG
372 template <class _R1, class _R2>
373 struct _LIBCPP_TEMPLATE_VIS ratio_add
374 : public __ratio_add<_R1, _R2>::type {};
376 #endif // _LIBCPP_CXX03_LANG
378 template <class _R1, class _R2>
379 struct __ratio_subtract
382 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
383 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
385 typedef typename ratio_multiply
387 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
392 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
393 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
400 #ifndef _LIBCPP_CXX03_LANG
402 template <class _R1, class _R2> using ratio_subtract
403 = typename __ratio_subtract<_R1, _R2>::type;
405 #else // _LIBCPP_CXX03_LANG
407 template <class _R1, class _R2>
408 struct _LIBCPP_TEMPLATE_VIS ratio_subtract
409 : public __ratio_subtract<_R1, _R2>::type {};
411 #endif // _LIBCPP_CXX03_LANG
415 template <class _R1, class _R2>
416 struct _LIBCPP_TEMPLATE_VIS ratio_equal
417 : public _LIBCPP_BOOL_CONSTANT((_R1::num == _R2::num && _R1::den == _R2::den)) {};
419 template <class _R1, class _R2>
420 struct _LIBCPP_TEMPLATE_VIS ratio_not_equal
421 : public _LIBCPP_BOOL_CONSTANT((!ratio_equal<_R1, _R2>::value)) {};
425 template <class _R1, class _R2, bool _Odd = false,
426 intmax_t _Q1 = _R1::num / _R1::den, intmax_t _M1 = _R1::num % _R1::den,
427 intmax_t _Q2 = _R2::num / _R2::den, intmax_t _M2 = _R2::num % _R2::den>
430 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
433 template <class _R1, class _R2, bool _Odd, intmax_t _Qp>
434 struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0>
436 static const bool value = false;
439 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>
440 struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2>
442 static const bool value = !_Odd;
445 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1>
446 struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0>
448 static const bool value = _Odd;
451 template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1,
453 struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2>
455 static const bool value = __ratio_less1<ratio<_R1::den, _M1>,
456 ratio<_R2::den, _M2>, !_Odd>::value;
459 template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>::value,
460 intmax_t _S2 = __static_sign<_R2::num>::value>
463 static const bool value = _S1 < _S2;
466 template <class _R1, class _R2>
467 struct __ratio_less<_R1, _R2, 1LL, 1LL>
469 static const bool value = __ratio_less1<_R1, _R2>::value;
472 template <class _R1, class _R2>
473 struct __ratio_less<_R1, _R2, -1LL, -1LL>
475 static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
478 template <class _R1, class _R2>
479 struct _LIBCPP_TEMPLATE_VIS ratio_less
480 : public _LIBCPP_BOOL_CONSTANT((__ratio_less<_R1, _R2>::value)) {};
482 template <class _R1, class _R2>
483 struct _LIBCPP_TEMPLATE_VIS ratio_less_equal
484 : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R2, _R1>::value)) {};
486 template <class _R1, class _R2>
487 struct _LIBCPP_TEMPLATE_VIS ratio_greater
488 : public _LIBCPP_BOOL_CONSTANT((ratio_less<_R2, _R1>::value)) {};
490 template <class _R1, class _R2>
491 struct _LIBCPP_TEMPLATE_VIS ratio_greater_equal
492 : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R1, _R2>::value)) {};
494 template <class _R1, class _R2>
497 typedef ratio<__static_gcd<_R1::num, _R2::num>::value,
498 __static_lcm<_R1::den, _R2::den>::value> type;
501 #if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES)
502 template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_equal_v
503 = ratio_equal<_R1, _R2>::value;
505 template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_not_equal_v
506 = ratio_not_equal<_R1, _R2>::value;
508 template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_less_v
509 = ratio_less<_R1, _R2>::value;
511 template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_less_equal_v
512 = ratio_less_equal<_R1, _R2>::value;
514 template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_greater_v
515 = ratio_greater<_R1, _R2>::value;
517 template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_greater_equal_v
518 = ratio_greater_equal<_R1, _R2>::value;
521 _LIBCPP_END_NAMESPACE_STD
523 #endif // _LIBCPP_RATIO