blob: 8970ba36934c1bd0821ee3256b9943f9bef7c184 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
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>
Mark de Weverce8f12c2021-12-22 18:14:14 +010084#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +000085
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000086#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -050087# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000088#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000089
Eric Fiselierf4433a32017-05-31 22:07:49 +000090_LIBCPP_PUSH_MACROS
91#include <__undef_macros>
92
93
Howard Hinnantc51e1022010-05-11 19:42:16 +000094_LIBCPP_BEGIN_NAMESPACE_STD
95
96// __static_gcd
97
98template <intmax_t _Xp, intmax_t _Yp>
99struct __static_gcd
100{
101 static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value;
102};
103
104template <intmax_t _Xp>
105struct __static_gcd<_Xp, 0>
106{
107 static const intmax_t value = _Xp;
108};
109
Howard Hinnantcbf51aa2011-11-01 23:13:37 +0000110template <>
111struct __static_gcd<0, 0>
112{
113 static const intmax_t value = 1;
114};
115
Howard Hinnantc51e1022010-05-11 19:42:16 +0000116// __static_lcm
117
118template <intmax_t _Xp, intmax_t _Yp>
119struct __static_lcm
120{
121 static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp;
122};
123
124template <intmax_t _Xp>
125struct __static_abs
126{
127 static const intmax_t value = _Xp < 0 ? -_Xp : _Xp;
128};
129
130template <intmax_t _Xp>
131struct __static_sign
132{
133 static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
134};
135
136template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
137class __ll_add;
138
139template <intmax_t _Xp, intmax_t _Yp>
140class __ll_add<_Xp, _Yp, 1>
141{
142 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
143 static const intmax_t max = -min;
144
145 static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
146public:
147 static const intmax_t value = _Xp + _Yp;
148};
149
150template <intmax_t _Xp, intmax_t _Yp>
151class __ll_add<_Xp, _Yp, 0>
152{
153public:
154 static const intmax_t value = _Xp;
155};
156
157template <intmax_t _Xp, intmax_t _Yp>
158class __ll_add<_Xp, _Yp, -1>
159{
160 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
161 static const intmax_t max = -min;
162
163 static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
164public:
165 static const intmax_t value = _Xp + _Yp;
166};
167
168template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
169class __ll_sub;
170
171template <intmax_t _Xp, intmax_t _Yp>
172class __ll_sub<_Xp, _Yp, 1>
173{
174 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
175 static const intmax_t max = -min;
176
177 static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
178public:
179 static const intmax_t value = _Xp - _Yp;
180};
181
182template <intmax_t _Xp, intmax_t _Yp>
183class __ll_sub<_Xp, _Yp, 0>
184{
185public:
186 static const intmax_t value = _Xp;
187};
188
189template <intmax_t _Xp, intmax_t _Yp>
190class __ll_sub<_Xp, _Yp, -1>
191{
192 static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
193 static const intmax_t max = -min;
194
195 static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");
196public:
197 static const intmax_t value = _Xp - _Yp;
198};
199
200template <intmax_t _Xp, intmax_t _Yp>
201class __ll_mul
202{
203 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
204 static const intmax_t min = nan + 1;
205 static const intmax_t max = -min;
206 static const intmax_t __a_x = __static_abs<_Xp>::value;
207 static const intmax_t __a_y = __static_abs<_Yp>::value;
208
209 static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
210public:
211 static const intmax_t value = _Xp * _Yp;
212};
213
214template <intmax_t _Yp>
215class __ll_mul<0, _Yp>
216{
217public:
218 static const intmax_t value = 0;
219};
220
221template <intmax_t _Xp>
222class __ll_mul<_Xp, 0>
223{
224public:
225 static const intmax_t value = 0;
226};
227
228template <>
229class __ll_mul<0, 0>
230{
231public:
232 static const intmax_t value = 0;
233};
234
235// Not actually used but left here in case needed in future maintenance
236template <intmax_t _Xp, intmax_t _Yp>
237class __ll_div
238{
239 static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
240 static const intmax_t min = nan + 1;
241 static const intmax_t max = -min;
242
243 static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
244public:
245 static const intmax_t value = _Xp / _Yp;
246};
247
248template <intmax_t _Num, intmax_t _Den = 1>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000249class _LIBCPP_TEMPLATE_VIS ratio
Howard Hinnantc51e1022010-05-11 19:42:16 +0000250{
251 static_assert(__static_abs<_Num>::value >= 0, "ratio numerator is out of range");
252 static_assert(_Den != 0, "ratio divide by 0");
253 static_assert(__static_abs<_Den>::value > 0, "ratio denominator is out of range");
Marshall Clow0735e4e2015-04-16 21:36:54 +0000254 static _LIBCPP_CONSTEXPR const intmax_t __na = __static_abs<_Num>::value;
255 static _LIBCPP_CONSTEXPR const intmax_t __da = __static_abs<_Den>::value;
256 static _LIBCPP_CONSTEXPR const intmax_t __s = __static_sign<_Num>::value * __static_sign<_Den>::value;
257 static _LIBCPP_CONSTEXPR const intmax_t __gcd = __static_gcd<__na, __da>::value;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000258public:
Marshall Clow0735e4e2015-04-16 21:36:54 +0000259 static _LIBCPP_CONSTEXPR const intmax_t num = __s * __na / __gcd;
260 static _LIBCPP_CONSTEXPR const intmax_t den = __da / __gcd;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261
262 typedef ratio<num, den> type;
263};
264
Eric Fiselierbd716a42015-05-20 03:15:01 +0000265template <intmax_t _Num, intmax_t _Den>
266_LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::num;
267
268template <intmax_t _Num, intmax_t _Den>
269_LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::den;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
Howard Hinnantf39463b2010-05-18 17:32:30 +0000271template <class _Tp> struct __is_ratio : false_type {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000272template <intmax_t _Num, intmax_t _Den> struct __is_ratio<ratio<_Num, _Den> > : true_type {};
273
274typedef ratio<1LL, 1000000000000000000LL> atto;
275typedef ratio<1LL, 1000000000000000LL> femto;
276typedef ratio<1LL, 1000000000000LL> pico;
277typedef ratio<1LL, 1000000000LL> nano;
278typedef ratio<1LL, 1000000LL> micro;
279typedef ratio<1LL, 1000LL> milli;
280typedef ratio<1LL, 100LL> centi;
281typedef ratio<1LL, 10LL> deci;
282typedef ratio< 10LL, 1LL> deca;
283typedef ratio< 100LL, 1LL> hecto;
284typedef ratio< 1000LL, 1LL> kilo;
285typedef ratio< 1000000LL, 1LL> mega;
286typedef ratio< 1000000000LL, 1LL> giga;
287typedef ratio< 1000000000000LL, 1LL> tera;
288typedef ratio< 1000000000000000LL, 1LL> peta;
289typedef ratio<1000000000000000000LL, 1LL> exa;
290
291template <class _R1, class _R2>
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000292struct __ratio_multiply
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293{
294private:
295 static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>::value;
296 static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>::value;
297public:
298 typedef typename ratio
299 <
300 __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,
301 __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value
302 >::type type;
303};
304
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000305#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000306
307template <class _R1, class _R2> using ratio_multiply
308 = typename __ratio_multiply<_R1, _R2>::type;
309
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000310#else // _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000311
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000313struct _LIBCPP_TEMPLATE_VIS ratio_multiply
Howard Hinnante179d8f2010-11-28 19:41:07 +0000314 : public __ratio_multiply<_R1, _R2>::type {};
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000315
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400316#endif // _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000317
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000318template <class _R1, class _R2>
319struct __ratio_divide
Howard Hinnantc51e1022010-05-11 19:42:16 +0000320{
321private:
322 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
323 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
324public:
325 typedef typename ratio
326 <
327 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
328 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
329 >::type type;
330};
331
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000332#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000333
334template <class _R1, class _R2> using ratio_divide
335 = typename __ratio_divide<_R1, _R2>::type;
336
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000337#else // _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000338
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000340struct _LIBCPP_TEMPLATE_VIS ratio_divide
Howard Hinnante179d8f2010-11-28 19:41:07 +0000341 : public __ratio_divide<_R1, _R2>::type {};
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000342
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400343#endif // _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000344
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000345template <class _R1, class _R2>
346struct __ratio_add
Howard Hinnantc51e1022010-05-11 19:42:16 +0000347{
348private:
349 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
350 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
351public:
352 typedef typename ratio_multiply
353 <
354 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
355 ratio
356 <
357 __ll_add
358 <
359 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
360 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
361 >::value,
362 _R2::den
363 >
364 >::type type;
365};
366
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000367#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000368
369template <class _R1, class _R2> using ratio_add
370 = typename __ratio_add<_R1, _R2>::type;
371
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000372#else // _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000373
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000375struct _LIBCPP_TEMPLATE_VIS ratio_add
Howard Hinnante179d8f2010-11-28 19:41:07 +0000376 : public __ratio_add<_R1, _R2>::type {};
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000377
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400378#endif // _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000379
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000380template <class _R1, class _R2>
381struct __ratio_subtract
Howard Hinnantc51e1022010-05-11 19:42:16 +0000382{
383private:
384 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
385 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
386public:
387 typedef typename ratio_multiply
388 <
389 ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
390 ratio
391 <
392 __ll_sub
393 <
394 __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
395 __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
396 >::value,
397 _R2::den
398 >
399 >::type type;
400};
401
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000402#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000403
404template <class _R1, class _R2> using ratio_subtract
405 = typename __ratio_subtract<_R1, _R2>::type;
406
Eric Fiselierc6f17cc2016-09-25 03:34:28 +0000407#else // _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000408
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000409template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000410struct _LIBCPP_TEMPLATE_VIS ratio_subtract
Howard Hinnante179d8f2010-11-28 19:41:07 +0000411 : public __ratio_subtract<_R1, _R2>::type {};
Howard Hinnantbc2b5ac2010-11-24 17:05:06 +0000412
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400413#endif // _LIBCPP_CXX03_LANG
Howard Hinnant0b8647d2011-05-31 16:55:36 +0000414
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415// ratio_equal
416
417template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000418struct _LIBCPP_TEMPLATE_VIS ratio_equal
Marshall Clow5460b362015-05-18 23:21:06 +0000419 : public _LIBCPP_BOOL_CONSTANT((_R1::num == _R2::num && _R1::den == _R2::den)) {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420
421template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000422struct _LIBCPP_TEMPLATE_VIS ratio_not_equal
Marshall Clow5460b362015-05-18 23:21:06 +0000423 : public _LIBCPP_BOOL_CONSTANT((!ratio_equal<_R1, _R2>::value)) {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424
425// ratio_less
426
427template <class _R1, class _R2, bool _Odd = false,
428 intmax_t _Q1 = _R1::num / _R1::den, intmax_t _M1 = _R1::num % _R1::den,
429 intmax_t _Q2 = _R2::num / _R2::den, intmax_t _M2 = _R2::num % _R2::den>
430struct __ratio_less1
431{
432 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
433};
434
Howard Hinnantc834c512011-11-29 18:15:50 +0000435template <class _R1, class _R2, bool _Odd, intmax_t _Qp>
436struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000437{
438 static const bool value = false;
439};
440
Howard Hinnantc834c512011-11-29 18:15:50 +0000441template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>
442struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000443{
444 static const bool value = !_Odd;
445};
446
Howard Hinnantc834c512011-11-29 18:15:50 +0000447template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1>
448struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000449{
450 static const bool value = _Odd;
451};
452
Howard Hinnantc834c512011-11-29 18:15:50 +0000453template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000454 intmax_t _M2>
Howard Hinnantc834c512011-11-29 18:15:50 +0000455struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000456{
457 static const bool value = __ratio_less1<ratio<_R1::den, _M1>,
458 ratio<_R2::den, _M2>, !_Odd>::value;
459};
460
461template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>::value,
462 intmax_t _S2 = __static_sign<_R2::num>::value>
463struct __ratio_less
464{
465 static const bool value = _S1 < _S2;
466};
467
468template <class _R1, class _R2>
469struct __ratio_less<_R1, _R2, 1LL, 1LL>
470{
471 static const bool value = __ratio_less1<_R1, _R2>::value;
472};
473
474template <class _R1, class _R2>
475struct __ratio_less<_R1, _R2, -1LL, -1LL>
476{
477 static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
478};
479
480template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000481struct _LIBCPP_TEMPLATE_VIS ratio_less
Marshall Clow5460b362015-05-18 23:21:06 +0000482 : public _LIBCPP_BOOL_CONSTANT((__ratio_less<_R1, _R2>::value)) {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000483
484template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000485struct _LIBCPP_TEMPLATE_VIS ratio_less_equal
Marshall Clow5460b362015-05-18 23:21:06 +0000486 : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R2, _R1>::value)) {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000487
488template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000489struct _LIBCPP_TEMPLATE_VIS ratio_greater
Marshall Clow5460b362015-05-18 23:21:06 +0000490 : public _LIBCPP_BOOL_CONSTANT((ratio_less<_R2, _R1>::value)) {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000491
492template <class _R1, class _R2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000493struct _LIBCPP_TEMPLATE_VIS ratio_greater_equal
Marshall Clow5460b362015-05-18 23:21:06 +0000494 : public _LIBCPP_BOOL_CONSTANT((!ratio_less<_R1, _R2>::value)) {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000495
496template <class _R1, class _R2>
497struct __ratio_gcd
498{
499 typedef ratio<__static_gcd<_R1::num, _R2::num>::value,
500 __static_lcm<_R1::den, _R2::den>::value> type;
501};
502
Louis Dionne7acc4fd2021-09-22 09:46:19 -0400503#if _LIBCPP_STD_VER > 14
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000504template <class _R1, class _R2>
Louis Dionne559be102021-09-22 09:35:32 -0400505inline constexpr bool ratio_equal_v = ratio_equal<_R1, _R2>::value;
Marshall Clowcec5d832015-11-30 05:04:48 +0000506
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000507template <class _R1, class _R2>
Louis Dionne559be102021-09-22 09:35:32 -0400508inline constexpr bool ratio_not_equal_v = ratio_not_equal<_R1, _R2>::value;
Marshall Clowcec5d832015-11-30 05:04:48 +0000509
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000510template <class _R1, class _R2>
Louis Dionne559be102021-09-22 09:35:32 -0400511inline constexpr bool ratio_less_v = ratio_less<_R1, _R2>::value;
Marshall Clowcec5d832015-11-30 05:04:48 +0000512
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000513template <class _R1, class _R2>
Louis Dionne559be102021-09-22 09:35:32 -0400514inline constexpr bool ratio_less_equal_v = ratio_less_equal<_R1, _R2>::value;
Marshall Clowcec5d832015-11-30 05:04:48 +0000515
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000516template <class _R1, class _R2>
Louis Dionne559be102021-09-22 09:35:32 -0400517inline constexpr bool ratio_greater_v = ratio_greater<_R1, _R2>::value;
Marshall Clowcec5d832015-11-30 05:04:48 +0000518
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000519template <class _R1, class _R2>
Louis Dionne559be102021-09-22 09:35:32 -0400520inline constexpr bool ratio_greater_equal_v = ratio_greater_equal<_R1, _R2>::value;
Marshall Clowcec5d832015-11-30 05:04:48 +0000521#endif
522
Howard Hinnantc51e1022010-05-11 19:42:16 +0000523_LIBCPP_END_NAMESPACE_STD
524
Eric Fiselierf4433a32017-05-31 22:07:49 +0000525_LIBCPP_POP_MACROS
526
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400527#endif // _LIBCPP_RATIO