blob: 622b7a05ef80f2287aeaa9861763ac5f708704fd [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- ratio -----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_RATIO
11#define _LIBCPP_RATIO
12
13/*
14 ratio synopsis
15
16namespace std
17{
18
19template <intmax_t N, intmax_t D = 1>
20class ratio
21{
22public:
Marshall Clow0735e4e2015-04-16 21:36:54 +000023 static constexpr intmax_t num;
24 static constexpr intmax_t den;
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +000025 typedef ratio<num, den> type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000026};
27
28// ratio arithmetic
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +000029template <class R1, class R2> using ratio_add = ...;
30template <class R1, class R2> using ratio_subtract = ...;
31template <class R1, class R2> using ratio_multiply = ...;
32template <class R1, class R2> using ratio_divide = ...;
Howard Hinnantc51e1022010-05-11 19:42:16 +000033
34// ratio comparison
35template <class R1, class R2> struct ratio_equal;
36template <class R1, class R2> struct ratio_not_equal;
37template <class R1, class R2> struct ratio_less;
38template <class R1, class R2> struct ratio_less_equal;
39template <class R1, class R2> struct ratio_greater;
40template <class R1, class R2> struct ratio_greater_equal;
41
42// convenience SI typedefs
43typedef ratio<1, 1000000000000000000000000> yocto; // not supported
44typedef ratio<1, 1000000000000000000000> zepto; // not supported
45typedef ratio<1, 1000000000000000000> atto;
46typedef ratio<1, 1000000000000000> femto;
47typedef ratio<1, 1000000000000> pico;
48typedef ratio<1, 1000000000> nano;
49typedef ratio<1, 1000000> micro;
50typedef ratio<1, 1000> milli;
51typedef ratio<1, 100> centi;
52typedef ratio<1, 10> deci;
53typedef ratio< 10, 1> deca;
54typedef ratio< 100, 1> hecto;
55typedef ratio< 1000, 1> kilo;
56typedef ratio< 1000000, 1> mega;
57typedef ratio< 1000000000, 1> giga;
58typedef ratio< 1000000000000, 1> tera;
59typedef ratio< 1000000000000000, 1> peta;
60typedef ratio< 1000000000000000000, 1> exa;
61typedef ratio< 1000000000000000000000, 1> zetta; // not supported
62typedef ratio<1000000000000000000000000, 1> yotta; // not supported
63
Marshall Clowcec5d832015-11-30 05:04:48 +000064 // 20.11.5, ratio comparison
Marshall Clowf1bf62f2018-01-02 17:17:01 +000065 template <class R1, class R2> inline constexpr bool ratio_equal_v
Marshall Clowcec5d832015-11-30 05:04:48 +000066 = ratio_equal<R1, R2>::value; // C++17
Marshall Clowf1bf62f2018-01-02 17:17:01 +000067 template <class R1, class R2> inline constexpr bool ratio_not_equal_v
Marshall Clowcec5d832015-11-30 05:04:48 +000068 = ratio_not_equal<R1, R2>::value; // C++17
Marshall Clowf1bf62f2018-01-02 17:17:01 +000069 template <class R1, class R2> inline constexpr bool ratio_less_v
Marshall Clowcec5d832015-11-30 05:04:48 +000070 = ratio_less<R1, R2>::value; // C++17
Marshall Clowf1bf62f2018-01-02 17:17:01 +000071 template <class R1, class R2> inline constexpr bool ratio_less_equal_v
Marshall Clowcec5d832015-11-30 05:04:48 +000072 = ratio_less_equal<R1, R2>::value; // C++17
Marshall Clowf1bf62f2018-01-02 17:17:01 +000073 template <class R1, class R2> inline constexpr bool ratio_greater_v
Marshall Clowcec5d832015-11-30 05:04:48 +000074 = ratio_greater<R1, R2>::value; // C++17
Marshall Clowf1bf62f2018-01-02 17:17:01 +000075 template <class R1, class R2> inline constexpr bool ratio_greater_equal_v
Marshall Clowcec5d832015-11-30 05:04:48 +000076 = ratio_greater_equal<R1, R2>::value; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000077}
78*/
79
80#include <__config>
Howard Hinnantc51e1022010-05-11 19:42:16 +000081#include <climits>
Arthur O'Dwyeref181602021-05-19 11:57:04 -040082#include <cstdint>
Howard Hinnantc51e1022010-05-11 19:42:16 +000083#include <type_traits>
84
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000085#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000086#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000087#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
Eric Fiselierf4433a32017-05-31 22:07:49 +000089_LIBCPP_PUSH_MACROS
90#include <__undef_macros>
91
92
Howard Hinnantc51e1022010-05-11 19:42:16 +000093_LIBCPP_BEGIN_NAMESPACE_STD
94
95// __static_gcd
96
97template <intmax_t _Xp, intmax_t _Yp>
98struct __static_gcd
99{
100 static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value;
101};
102
103template <intmax_t _Xp>
104struct __static_gcd<_Xp, 0>
105{
106 static const intmax_t value = _Xp;
107};
108
Howard Hinnantcbf51aa2011-11-01 23:13:37 +0000109template <>
110struct __static_gcd<0, 0>
111{
112 static const intmax_t value = 1;
113};
114
Howard Hinnantc51e1022010-05-11 19:42:16 +0000115// __static_lcm
116
117template <intmax_t _Xp, intmax_t _Yp>
118struct __static_lcm
119{
120 static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp;
121};
122
123template <intmax_t _Xp>
124struct __static_abs
125{
126 static const intmax_t value = _Xp < 0 ? -_Xp : _Xp;
127};
128
129template <intmax_t _Xp>
130struct __static_sign
131{
132 static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
133};
134
135template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
136class __ll_add;
137
138template <intmax_t _Xp, intmax_t _Yp>
139class __ll_add<_Xp, _Yp, 1>
140{
141 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
142 static const intmax_t max = -min;
143
144 static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
145public:
146 static const intmax_t value = _Xp + _Yp;
147};
148
149template <intmax_t _Xp, intmax_t _Yp>
150class __ll_add<_Xp, _Yp, 0>
151{
152public:
153 static const intmax_t value = _Xp;
154};
155
156template <intmax_t _Xp, intmax_t _Yp>
157class __ll_add<_Xp, _Yp, -1>
158{
159 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
160 static const intmax_t max = -min;
161
162 static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
163public:
164 static const intmax_t value = _Xp + _Yp;
165};
166
167template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
168class __ll_sub;
169
170template <intmax_t _Xp, intmax_t _Yp>
171class __ll_sub<_Xp, _Yp, 1>
172{
173 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
174 static const intmax_t max = -min;
175
176 static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
177public:
178 static const intmax_t value = _Xp - _Yp;
179};
180
181template <intmax_t _Xp, intmax_t _Yp>
182class __ll_sub<_Xp, _Yp, 0>
183{
184public:
185 static const intmax_t value = _Xp;
186};
187
188template <intmax_t _Xp, intmax_t _Yp>
189class __ll_sub<_Xp, _Yp, -1>
190{
191 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
192 static const intmax_t max = -min;
193
194 static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");
195public:
196 static const intmax_t value = _Xp - _Yp;
197};
198
199template <intmax_t _Xp, intmax_t _Yp>
200class __ll_mul
201{
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;
207
208 static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
209public:
210 static const intmax_t value = _Xp * _Yp;
211};
212
213template <intmax_t _Yp>
214class __ll_mul<0, _Yp>
215{
216public:
217 static const intmax_t value = 0;
218};
219
220template <intmax_t _Xp>
221class __ll_mul<_Xp, 0>
222{
223public:
224 static const intmax_t value = 0;
225};
226
227template <>
228class __ll_mul<0, 0>
229{
230public:
231 static const intmax_t value = 0;
232};
233
234// Not actually used but left here in case needed in future maintenance
235template <intmax_t _Xp, intmax_t _Yp>
236class __ll_div
237{
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;
241
242 static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
243public:
244 static const intmax_t value = _Xp / _Yp;
245};
246
247template <intmax_t _Num, intmax_t _Den = 1>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000248class _LIBCPP_TEMPLATE_VIS ratio
Howard Hinnantc51e1022010-05-11 19:42:16 +0000249{
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");
Marshall Clow0735e4e2015-04-16 21:36:54 +0000253 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;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257public:
Marshall Clow0735e4e2015-04-16 21:36:54 +0000258 static _LIBCPP_CONSTEXPR const intmax_t num = __s * __na / __gcd;
259 static _LIBCPP_CONSTEXPR const intmax_t den = __da / __gcd;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000260
261 typedef ratio<num, den> type;
262};
263
Eric Fiselierbd716a42015-05-20 03:15:01 +0000264template <intmax_t _Num, intmax_t _Den>
265_LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::num;
266
267template <intmax_t _Num, intmax_t _Den>
268_LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::den;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269
Howard Hinnantf39463b2010-05-18 17:32:30 +0000270template <class _Tp> struct __is_ratio : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000271template <intmax_t _Num, intmax_t _Den> struct __is_ratio<ratio<_Num, _Den> > : true_type {};
272
273typedef ratio<1LL, 1000000000000000000LL> atto;
274typedef ratio<1LL, 1000000000000000LL> femto;
275typedef ratio<1LL, 1000000000000LL> pico;
276typedef ratio<1LL, 1000000000LL> nano;
277typedef ratio<1LL, 1000000LL> micro;
278typedef ratio<1LL, 1000LL> milli;
279typedef ratio<1LL, 100LL> centi;
280typedef ratio<1LL, 10LL> deci;
281typedef ratio< 10LL, 1LL> deca;
282typedef ratio< 100LL, 1LL> hecto;
283typedef ratio< 1000LL, 1LL> kilo;
284typedef ratio< 1000000LL, 1LL> mega;
285typedef ratio< 1000000000LL, 1LL> giga;
286typedef ratio< 1000000000000LL, 1LL> tera;
287typedef ratio< 1000000000000000LL, 1LL> peta;
288typedef ratio<1000000000000000000LL, 1LL> exa;
289
290template <class _R1, class _R2>
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000291struct __ratio_multiply
Howard Hinnantc51e1022010-05-11 19:42:16 +0000292{
293private:
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;
296public:
297 typedef typename ratio
298 <
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
301 >::type type;
302};
303
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000304#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000305
306template <class _R1, class _R2> using ratio_multiply
307 = typename __ratio_multiply<_R1, _R2>::type;
308
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000309#else // _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000310
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000312struct _LIBCPP_TEMPLATE_VIS ratio_multiply
Howard Hinnante179d8f2010-11-28 19:41:07 +0000313 : public __ratio_multiply<_R1, _R2>::type {};
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000314
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400315#endif // _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000316
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000317template <class _R1, class _R2>
318struct __ratio_divide
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319{
320private:
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;
323public:
324 typedef typename ratio
325 <
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
328 >::type type;
329};
330
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000331#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000332
333template <class _R1, class _R2> using ratio_divide
334 = typename __ratio_divide<_R1, _R2>::type;
335
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000336#else // _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000337
Howard Hinnantc51e1022010-05-11 19:42:16 +0000338template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000339struct _LIBCPP_TEMPLATE_VIS ratio_divide
Howard Hinnante179d8f2010-11-28 19:41:07 +0000340 : public __ratio_divide<_R1, _R2>::type {};
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000341
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400342#endif // _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000343
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000344template <class _R1, class _R2>
345struct __ratio_add
Howard Hinnantc51e1022010-05-11 19:42:16 +0000346{
347private:
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;
350public:
351 typedef typename ratio_multiply
352 <
353 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
354 ratio
355 <
356 __ll_add
357 <
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
360 >::value,
361 _R2::den
362 >
363 >::type type;
364};
365
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000366#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000367
368template <class _R1, class _R2> using ratio_add
369 = typename __ratio_add<_R1, _R2>::type;
370
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000371#else // _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000372
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000374struct _LIBCPP_TEMPLATE_VIS ratio_add
Howard Hinnante179d8f2010-11-28 19:41:07 +0000375 : public __ratio_add<_R1, _R2>::type {};
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000376
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400377#endif // _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000378
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000379template <class _R1, class _R2>
380struct __ratio_subtract
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381{
382private:
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;
385public:
386 typedef typename ratio_multiply
387 <
388 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
389 ratio
390 <
391 __ll_sub
392 <
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
395 >::value,
396 _R2::den
397 >
398 >::type type;
399};
400
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000401#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000402
403template <class _R1, class _R2> using ratio_subtract
404 = typename __ratio_subtract<_R1, _R2>::type;
405
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000406#else // _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000407
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000408template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000409struct _LIBCPP_TEMPLATE_VIS ratio_subtract
Howard Hinnante179d8f2010-11-28 19:41:07 +0000410 : public __ratio_subtract<_R1, _R2>::type {};
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000411
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400412#endif // _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000413
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414// ratio_equal
415
416template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000417struct _LIBCPP_TEMPLATE_VIS ratio_equal
Marshall Clow5460b362015-05-18 23:21:06 +0000418 : public _LIBCPP_BOOL_CONSTANT((_R1::num == _R2::num && _R1::den == _R2::den)) {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
420template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000421struct _LIBCPP_TEMPLATE_VIS ratio_not_equal
Marshall Clow5460b362015-05-18 23:21:06 +0000422 : public _LIBCPP_BOOL_CONSTANT((!ratio_equal<_R1, _R2>::value)) {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423
424// ratio_less
425
426template <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>
429struct __ratio_less1
430{
431 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
432};
433
Howard Hinnantc834c512011-11-29 18:15:50 +0000434template <class _R1, class _R2, bool _Odd, intmax_t _Qp>
435struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436{
437 static const bool value = false;
438};
439
Howard Hinnantc834c512011-11-29 18:15:50 +0000440template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>
441struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000442{
443 static const bool value = !_Odd;
444};
445
Howard Hinnantc834c512011-11-29 18:15:50 +0000446template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1>
447struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000448{
449 static const bool value = _Odd;
450};
451
Howard Hinnantc834c512011-11-29 18:15:50 +0000452template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000453 intmax_t _M2>
Howard Hinnantc834c512011-11-29 18:15:50 +0000454struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000455{
456 static const bool value = __ratio_less1<ratio<_R1::den, _M1>,
457 ratio<_R2::den, _M2>, !_Odd>::value;
458};
459
460template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>::value,
461 intmax_t _S2 = __static_sign<_R2::num>::value>
462struct __ratio_less
463{
464 static const bool value = _S1 < _S2;
465};
466
467template <class _R1, class _R2>
468struct __ratio_less<_R1, _R2, 1LL, 1LL>
469{
470 static const bool value = __ratio_less1<_R1, _R2>::value;
471};
472
473template <class _R1, class _R2>
474struct __ratio_less<_R1, _R2, -1LL, -1LL>
475{
476 static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
477};
478
479template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000480struct _LIBCPP_TEMPLATE_VIS ratio_less
Marshall Clow5460b362015-05-18 23:21:06 +0000481 : public _LIBCPP_BOOL_CONSTANT((__ratio_less<_R1, _R2>::value)) {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000482
483template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000484struct _LIBCPP_TEMPLATE_VIS ratio_less_equal
Marshall Clow5460b362015-05-18 23:21:06 +0000485 : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R2, _R1>::value)) {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000486
487template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000488struct _LIBCPP_TEMPLATE_VIS ratio_greater
Marshall Clow5460b362015-05-18 23:21:06 +0000489 : public _LIBCPP_BOOL_CONSTANT((ratio_less<_R2, _R1>::value)) {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000490
491template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000492struct _LIBCPP_TEMPLATE_VIS ratio_greater_equal
Marshall Clow5460b362015-05-18 23:21:06 +0000493 : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R1, _R2>::value)) {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000494
495template <class _R1, class _R2>
496struct __ratio_gcd
497{
498 typedef ratio<__static_gcd<_R1::num, _R2::num>::value,
499 __static_lcm<_R1::den, _R2::den>::value> type;
500};
501
Marshall Clowcec5d832015-11-30 05:04:48 +0000502#if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES)
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000503template <class _R1, class _R2>
Louis Dionne559be102021-09-22 09:35:32 -0400504inline constexpr bool ratio_equal_v = ratio_equal<_R1, _R2>::value;
Marshall Clowcec5d832015-11-30 05:04:48 +0000505
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000506template <class _R1, class _R2>
Louis Dionne559be102021-09-22 09:35:32 -0400507inline constexpr bool ratio_not_equal_v = ratio_not_equal<_R1, _R2>::value;
Marshall Clowcec5d832015-11-30 05:04:48 +0000508
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000509template <class _R1, class _R2>
Louis Dionne559be102021-09-22 09:35:32 -0400510inline constexpr bool ratio_less_v = ratio_less<_R1, _R2>::value;
Marshall Clowcec5d832015-11-30 05:04:48 +0000511
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000512template <class _R1, class _R2>
Louis Dionne559be102021-09-22 09:35:32 -0400513inline constexpr bool ratio_less_equal_v = ratio_less_equal<_R1, _R2>::value;
Marshall Clowcec5d832015-11-30 05:04:48 +0000514
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000515template <class _R1, class _R2>
Louis Dionne559be102021-09-22 09:35:32 -0400516inline constexpr bool ratio_greater_v = ratio_greater<_R1, _R2>::value;
Marshall Clowcec5d832015-11-30 05:04:48 +0000517
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000518template <class _R1, class _R2>
Louis Dionne559be102021-09-22 09:35:32 -0400519inline constexpr bool ratio_greater_equal_v = ratio_greater_equal<_R1, _R2>::value;
Marshall Clowcec5d832015-11-30 05:04:48 +0000520#endif
521
Howard Hinnantc51e1022010-05-11 19:42:16 +0000522_LIBCPP_END_NAMESPACE_STD
523
Eric Fiselierf4433a32017-05-31 22:07:49 +0000524_LIBCPP_POP_MACROS
525
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400526#endif // _LIBCPP_RATIO