blob: bf54cd099dd41b724b91f5be6cffd2728eb6c9fa [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:161// -*- C++ -*-
Louis Dionneeb8650a2021-11-17 21:25:012//===----------------------------------------------------------------------===//
Howard Hinnant3e519522010-05-11 19:42:163//
Chandler Carruth57b08b02019-01-19 10:56:404// 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 Hinnant3e519522010-05-11 19:42:167//
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 Clow60d5e0e2015-04-16 21:36:5423 static constexpr intmax_t num;
24 static constexpr intmax_t den;
Howard Hinnant0c6a0fe2010-11-24 17:05:0625 typedef ratio<num, den> type;
Howard Hinnant3e519522010-05-11 19:42:1626};
27
28// ratio arithmetic
Howard Hinnant0c6a0fe2010-11-24 17:05:0629template <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 Hinnant3e519522010-05-11 19:42:1633
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
Mark de Weverf805c792023-06-17 13:50:5043using quecto = ratio <1, 1'000'000'000'000'000'000'000'000'000'000>; // Since C++26; not supported
44using ronto = ratio <1, 1'000'000'000'000'000'000'000'000'000>; // Since C++26; not supported
Howard Hinnant3e519522010-05-11 19:42:1645typedef ratio<1, 1000000000000000000000000> yocto; // not supported
46typedef ratio<1, 1000000000000000000000> zepto; // not supported
47typedef ratio<1, 1000000000000000000> atto;
48typedef ratio<1, 1000000000000000> femto;
49typedef ratio<1, 1000000000000> pico;
50typedef ratio<1, 1000000000> nano;
51typedef ratio<1, 1000000> micro;
52typedef ratio<1, 1000> milli;
53typedef ratio<1, 100> centi;
54typedef ratio<1, 10> deci;
55typedef ratio< 10, 1> deca;
56typedef ratio< 100, 1> hecto;
57typedef ratio< 1000, 1> kilo;
58typedef ratio< 1000000, 1> mega;
59typedef ratio< 1000000000, 1> giga;
60typedef ratio< 1000000000000, 1> tera;
61typedef ratio< 1000000000000000, 1> peta;
62typedef ratio< 1000000000000000000, 1> exa;
63typedef ratio< 1000000000000000000000, 1> zetta; // not supported
64typedef ratio<1000000000000000000000000, 1> yotta; // not supported
Mark de Weverf805c792023-06-17 13:50:5065using ronna = ratio <1'000'000'000'000'000'000'000'000'000, 1>; // Since C++26; not supported
66using quetta = ratio <1'000'000'000'000'000'000'000'000'000'000, 1>; // Since C++26; not supported
Howard Hinnant3e519522010-05-11 19:42:1667
Marshall Clow825a9ed2015-11-30 05:04:4868 // 20.11.5, ratio comparison
Marshall Clow40a01d52018-01-02 17:17:0169 template <class R1, class R2> inline constexpr bool ratio_equal_v
Marshall Clow825a9ed2015-11-30 05:04:4870 = ratio_equal<R1, R2>::value; // C++17
Marshall Clow40a01d52018-01-02 17:17:0171 template <class R1, class R2> inline constexpr bool ratio_not_equal_v
Marshall Clow825a9ed2015-11-30 05:04:4872 = ratio_not_equal<R1, R2>::value; // C++17
Marshall Clow40a01d52018-01-02 17:17:0173 template <class R1, class R2> inline constexpr bool ratio_less_v
Marshall Clow825a9ed2015-11-30 05:04:4874 = ratio_less<R1, R2>::value; // C++17
Marshall Clow40a01d52018-01-02 17:17:0175 template <class R1, class R2> inline constexpr bool ratio_less_equal_v
Marshall Clow825a9ed2015-11-30 05:04:4876 = ratio_less_equal<R1, R2>::value; // C++17
Marshall Clow40a01d52018-01-02 17:17:0177 template <class R1, class R2> inline constexpr bool ratio_greater_v
Marshall Clow825a9ed2015-11-30 05:04:4878 = ratio_greater<R1, R2>::value; // C++17
Marshall Clow40a01d52018-01-02 17:17:0179 template <class R1, class R2> inline constexpr bool ratio_greater_equal_v
Marshall Clow825a9ed2015-11-30 05:04:4880 = ratio_greater_equal<R1, R2>::value; // C++17
Howard Hinnant3e519522010-05-11 19:42:1681}
82*/
83
Nikolas Klauserb9a26582024-12-21 12:01:4884#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
85# include <__cxx03/ratio>
86#else
Nikolas Klauserc166a9c2024-12-10 15:02:1287# include <__config>
88# include <__type_traits/integral_constant.h>
89# include <climits>
90# include <cstdint>
91# include <version>
Howard Hinnant3e519522010-05-11 19:42:1692
Nikolas Klauserc166a9c2024-12-10 15:02:1293# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
94# pragma GCC system_header
95# endif
Howard Hinnant3e519522010-05-11 19:42:1696
Eric Fiseliera016efb2017-05-31 22:07:4997_LIBCPP_PUSH_MACROS
Nikolas Klauserc166a9c2024-12-10 15:02:1298# include <__undef_macros>
Eric Fiseliera016efb2017-05-31 22:07:4999
Howard Hinnant3e519522010-05-11 19:42:16100_LIBCPP_BEGIN_NAMESPACE_STD
101
102// __static_gcd
103
104template <intmax_t _Xp, intmax_t _Yp>
Nikolas Klausere3c958a2024-11-12 14:15:18105inline const intmax_t __static_gcd = __static_gcd<_Yp, _Xp % _Yp>;
Howard Hinnant3e519522010-05-11 19:42:16106
107template <intmax_t _Xp>
Nikolas Klausere3c958a2024-11-12 14:15:18108inline const intmax_t __static_gcd<_Xp, 0> = _Xp;
Howard Hinnant3e519522010-05-11 19:42:16109
Howard Hinnant05e48582011-11-01 23:13:37110template <>
Nikolas Klausere3c958a2024-11-12 14:15:18111inline const intmax_t __static_gcd<0, 0> = 1;
Howard Hinnant05e48582011-11-01 23:13:37112
Howard Hinnant3e519522010-05-11 19:42:16113// __static_lcm
114
115template <intmax_t _Xp, intmax_t _Yp>
Nikolas Klausere3c958a2024-11-12 14:15:18116inline const intmax_t __static_lcm = _Xp / __static_gcd<_Xp, _Yp> * _Yp;
Howard Hinnant3e519522010-05-11 19:42:16117
118template <intmax_t _Xp>
Nikolas Klausere3c958a2024-11-12 14:15:18119inline const intmax_t __static_abs = _Xp < 0 ? -_Xp : _Xp;
Howard Hinnant3e519522010-05-11 19:42:16120
121template <intmax_t _Xp>
Nikolas Klausere3c958a2024-11-12 14:15:18122inline const intmax_t __static_sign = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
Howard Hinnant3e519522010-05-11 19:42:16123
Nikolas Klausere3c958a2024-11-12 14:15:18124template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp> >
Howard Hinnant3e519522010-05-11 19:42:16125class __ll_add;
126
127template <intmax_t _Xp, intmax_t _Yp>
Louis Dionne9783f282023-12-18 19:01:33128class __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;
Howard Hinnant3e519522010-05-11 19:42:16131
Louis Dionne9783f282023-12-18 19:01:33132 static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
133
Howard Hinnant3e519522010-05-11 19:42:16134public:
Louis Dionne9783f282023-12-18 19:01:33135 static const intmax_t value = _Xp + _Yp;
Howard Hinnant3e519522010-05-11 19:42:16136};
137
138template <intmax_t _Xp, intmax_t _Yp>
Louis Dionne9783f282023-12-18 19:01:33139class __ll_add<_Xp, _Yp, 0> {
Howard Hinnant3e519522010-05-11 19:42:16140public:
Louis Dionne9783f282023-12-18 19:01:33141 static const intmax_t value = _Xp;
Howard Hinnant3e519522010-05-11 19:42:16142};
143
144template <intmax_t _Xp, intmax_t _Yp>
Louis Dionne9783f282023-12-18 19:01:33145class __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;
Howard Hinnant3e519522010-05-11 19:42:16148
Louis Dionne9783f282023-12-18 19:01:33149 static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
150
Howard Hinnant3e519522010-05-11 19:42:16151public:
Louis Dionne9783f282023-12-18 19:01:33152 static const intmax_t value = _Xp + _Yp;
Howard Hinnant3e519522010-05-11 19:42:16153};
154
Nikolas Klausere3c958a2024-11-12 14:15:18155template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp> >
Howard Hinnant3e519522010-05-11 19:42:16156class __ll_sub;
157
158template <intmax_t _Xp, intmax_t _Yp>
Louis Dionne9783f282023-12-18 19:01:33159class __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;
Howard Hinnant3e519522010-05-11 19:42:16162
Louis Dionne9783f282023-12-18 19:01:33163 static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
164
Howard Hinnant3e519522010-05-11 19:42:16165public:
Louis Dionne9783f282023-12-18 19:01:33166 static const intmax_t value = _Xp - _Yp;
Howard Hinnant3e519522010-05-11 19:42:16167};
168
169template <intmax_t _Xp, intmax_t _Yp>
Louis Dionne9783f282023-12-18 19:01:33170class __ll_sub<_Xp, _Yp, 0> {
Howard Hinnant3e519522010-05-11 19:42:16171public:
Louis Dionne9783f282023-12-18 19:01:33172 static const intmax_t value = _Xp;
Howard Hinnant3e519522010-05-11 19:42:16173};
174
175template <intmax_t _Xp, intmax_t _Yp>
Louis Dionne9783f282023-12-18 19:01:33176class __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;
Howard Hinnant3e519522010-05-11 19:42:16179
Louis Dionne9783f282023-12-18 19:01:33180 static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");
181
Howard Hinnant3e519522010-05-11 19:42:16182public:
Louis Dionne9783f282023-12-18 19:01:33183 static const intmax_t value = _Xp - _Yp;
Howard Hinnant3e519522010-05-11 19:42:16184};
185
186template <intmax_t _Xp, intmax_t _Yp>
Louis Dionne9783f282023-12-18 19:01:33187class __ll_mul {
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;
Nikolas Klausere3c958a2024-11-12 14:15:18191 static const intmax_t __a_x = __static_abs<_Xp>;
192 static const intmax_t __a_y = __static_abs<_Yp>;
Howard Hinnant3e519522010-05-11 19:42:16193
Louis Dionne9783f282023-12-18 19:01:33194 static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
195
Howard Hinnant3e519522010-05-11 19:42:16196public:
Louis Dionne9783f282023-12-18 19:01:33197 static const intmax_t value = _Xp * _Yp;
Howard Hinnant3e519522010-05-11 19:42:16198};
199
200template <intmax_t _Yp>
Louis Dionne9783f282023-12-18 19:01:33201class __ll_mul<0, _Yp> {
Howard Hinnant3e519522010-05-11 19:42:16202public:
Louis Dionne9783f282023-12-18 19:01:33203 static const intmax_t value = 0;
Howard Hinnant3e519522010-05-11 19:42:16204};
205
206template <intmax_t _Xp>
Louis Dionne9783f282023-12-18 19:01:33207class __ll_mul<_Xp, 0> {
Howard Hinnant3e519522010-05-11 19:42:16208public:
Louis Dionne9783f282023-12-18 19:01:33209 static const intmax_t value = 0;
Howard Hinnant3e519522010-05-11 19:42:16210};
211
212template <>
Louis Dionne9783f282023-12-18 19:01:33213class __ll_mul<0, 0> {
Howard Hinnant3e519522010-05-11 19:42:16214public:
Louis Dionne9783f282023-12-18 19:01:33215 static const intmax_t value = 0;
Howard Hinnant3e519522010-05-11 19:42:16216};
217
218// Not actually used but left here in case needed in future maintenance
219template <intmax_t _Xp, intmax_t _Yp>
Louis Dionne9783f282023-12-18 19:01:33220class __ll_div {
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;
Howard Hinnant3e519522010-05-11 19:42:16224
Louis Dionne9783f282023-12-18 19:01:33225 static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
226
Howard Hinnant3e519522010-05-11 19:42:16227public:
Louis Dionne9783f282023-12-18 19:01:33228 static const intmax_t value = _Xp / _Yp;
Howard Hinnant3e519522010-05-11 19:42:16229};
230
231template <intmax_t _Num, intmax_t _Den = 1>
Nikolas Klauseraf9c04f2025-04-09 21:47:57232class ratio {
Nikolas Klausere3c958a2024-11-12 14:15:18233 static_assert(__static_abs<_Num> >= 0, "ratio numerator is out of range");
Louis Dionne9783f282023-12-18 19:01:33234 static_assert(_Den != 0, "ratio divide by 0");
Nikolas Klausere3c958a2024-11-12 14:15:18235 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>;
Howard Hinnant3e519522010-05-11 19:42:16240
Louis Dionne9783f282023-12-18 19:01:33241public:
Nikolas Klauserb69ddbc2024-11-13 10:57:16242 static inline _LIBCPP_CONSTEXPR const intmax_t num = __s * __na / __gcd;
243 static inline _LIBCPP_CONSTEXPR const intmax_t den = __da / __gcd;
Louis Dionne9783f282023-12-18 19:01:33244
245 typedef ratio<num, den> type;
Howard Hinnant3e519522010-05-11 19:42:16246};
247
Louis Dionne9783f282023-12-18 19:01:33248template <class _Tp>
Nikolas Klausere3c958a2024-11-12 14:15:18249inline const bool __is_ratio_v = false;
250
Louis Dionne9783f282023-12-18 19:01:33251template <intmax_t _Num, intmax_t _Den>
Nikolas Klausere3c958a2024-11-12 14:15:18252inline const bool __is_ratio_v<ratio<_Num, _Den> > = true;
Howard Hinnant3e519522010-05-11 19:42:16253
254typedef ratio<1LL, 1000000000000000000LL> atto;
Louis Dionne9783f282023-12-18 19:01:33255typedef ratio<1LL, 1000000000000000LL> femto;
256typedef ratio<1LL, 1000000000000LL> pico;
257typedef ratio<1LL, 1000000000LL> nano;
258typedef ratio<1LL, 1000000LL> micro;
259typedef ratio<1LL, 1000LL> milli;
260typedef ratio<1LL, 100LL> centi;
261typedef ratio<1LL, 10LL> deci;
262typedef ratio< 10LL, 1LL> deca;
263typedef ratio< 100LL, 1LL> hecto;
264typedef ratio< 1000LL, 1LL> kilo;
265typedef ratio< 1000000LL, 1LL> mega;
266typedef ratio< 1000000000LL, 1LL> giga;
267typedef ratio< 1000000000000LL, 1LL> tera;
268typedef ratio< 1000000000000000LL, 1LL> peta;
Howard Hinnant3e519522010-05-11 19:42:16269typedef ratio<1000000000000000000LL, 1LL> exa;
270
271template <class _R1, class _R2>
Louis Dionne9783f282023-12-18 19:01:33272struct __ratio_multiply {
Howard Hinnant3e519522010-05-11 19:42:16273private:
Nikolas Klausere3c958a2024-11-12 14:15:18274 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>;
Louis Dionne9783f282023-12-18 19:01:33276
Nikolas Klausere3c958a2024-11-12 14:15:18277 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");
Mark de Weverfe0d2772024-02-11 12:53:59279
Howard Hinnant3e519522010-05-11 19:42:16280public:
Louis Dionne9783f282023-12-18 19:01:33281 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;
Howard Hinnant3e519522010-05-11 19:42:16283};
284
Nikolas Klauserc166a9c2024-12-10 15:02:12285# ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3c67082011-05-31 16:55:36286
Louis Dionne9783f282023-12-18 19:01:33287template <class _R1, class _R2>
288using ratio_multiply = typename __ratio_multiply<_R1, _R2>::type;
Howard Hinnante3c67082011-05-31 16:55:36289
Nikolas Klauserc166a9c2024-12-10 15:02:12290# else // _LIBCPP_CXX03_LANG
Howard Hinnante3c67082011-05-31 16:55:36291
Howard Hinnant3e519522010-05-11 19:42:16292template <class _R1, class _R2>
Nikolas Klauseraf9c04f2025-04-09 21:47:57293struct ratio_multiply : public __ratio_multiply<_R1, _R2>::type {};
Howard Hinnant0c6a0fe2010-11-24 17:05:06294
Nikolas Klauserc166a9c2024-12-10 15:02:12295# endif // _LIBCPP_CXX03_LANG
Howard Hinnante3c67082011-05-31 16:55:36296
Howard Hinnant0c6a0fe2010-11-24 17:05:06297template <class _R1, class _R2>
Louis Dionne9783f282023-12-18 19:01:33298struct __ratio_divide {
Howard Hinnant3e519522010-05-11 19:42:16299private:
Nikolas Klausere3c958a2024-11-12 14:15:18300 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>;
Louis Dionne9783f282023-12-18 19:01:33302
Nikolas Klausere3c958a2024-11-12 14:15:18303 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");
Mark de Weverfe0d2772024-02-11 12:53:59305
Howard Hinnant3e519522010-05-11 19:42:16306public:
Louis Dionne9783f282023-12-18 19:01:33307 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;
Howard Hinnant3e519522010-05-11 19:42:16309};
310
Nikolas Klauserc166a9c2024-12-10 15:02:12311# ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3c67082011-05-31 16:55:36312
Louis Dionne9783f282023-12-18 19:01:33313template <class _R1, class _R2>
314using ratio_divide = typename __ratio_divide<_R1, _R2>::type;
Howard Hinnante3c67082011-05-31 16:55:36315
Nikolas Klauserc166a9c2024-12-10 15:02:12316# else // _LIBCPP_CXX03_LANG
Howard Hinnante3c67082011-05-31 16:55:36317
Howard Hinnant3e519522010-05-11 19:42:16318template <class _R1, class _R2>
Nikolas Klauseraf9c04f2025-04-09 21:47:57319struct ratio_divide : public __ratio_divide<_R1, _R2>::type {};
Howard Hinnant0c6a0fe2010-11-24 17:05:06320
Nikolas Klauserc166a9c2024-12-10 15:02:12321# endif // _LIBCPP_CXX03_LANG
Howard Hinnante3c67082011-05-31 16:55:36322
Howard Hinnant0c6a0fe2010-11-24 17:05:06323template <class _R1, class _R2>
Louis Dionne9783f282023-12-18 19:01:33324struct __ratio_add {
Howard Hinnant3e519522010-05-11 19:42:16325private:
Nikolas Klausere3c958a2024-11-12 14:15:18326 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>;
Louis Dionne9783f282023-12-18 19:01:33328
Nikolas Klausere3c958a2024-11-12 14:15:18329 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");
Mark de Weverfe0d2772024-02-11 12:53:59331
Howard Hinnant3e519522010-05-11 19:42:16332public:
Louis Dionne9783f282023-12-18 19:01:33333 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;
Howard Hinnant3e519522010-05-11 19:42:16338};
339
Nikolas Klauserc166a9c2024-12-10 15:02:12340# ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3c67082011-05-31 16:55:36341
Louis Dionne9783f282023-12-18 19:01:33342template <class _R1, class _R2>
343using ratio_add = typename __ratio_add<_R1, _R2>::type;
Howard Hinnante3c67082011-05-31 16:55:36344
Nikolas Klauserc166a9c2024-12-10 15:02:12345# else // _LIBCPP_CXX03_LANG
Howard Hinnante3c67082011-05-31 16:55:36346
Howard Hinnant3e519522010-05-11 19:42:16347template <class _R1, class _R2>
Nikolas Klauseraf9c04f2025-04-09 21:47:57348struct ratio_add : public __ratio_add<_R1, _R2>::type {};
Howard Hinnant0c6a0fe2010-11-24 17:05:06349
Nikolas Klauserc166a9c2024-12-10 15:02:12350# endif // _LIBCPP_CXX03_LANG
Howard Hinnante3c67082011-05-31 16:55:36351
Howard Hinnant0c6a0fe2010-11-24 17:05:06352template <class _R1, class _R2>
Louis Dionne9783f282023-12-18 19:01:33353struct __ratio_subtract {
Howard Hinnant3e519522010-05-11 19:42:16354private:
Nikolas Klausere3c958a2024-11-12 14:15:18355 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>;
Louis Dionne9783f282023-12-18 19:01:33357
Nikolas Klausere3c958a2024-11-12 14:15:18358 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");
Mark de Weverfe0d2772024-02-11 12:53:59360
Howard Hinnant3e519522010-05-11 19:42:16361public:
Louis Dionne9783f282023-12-18 19:01:33362 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;
Howard Hinnant3e519522010-05-11 19:42:16367};
368
Nikolas Klauserc166a9c2024-12-10 15:02:12369# ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3c67082011-05-31 16:55:36370
Louis Dionne9783f282023-12-18 19:01:33371template <class _R1, class _R2>
372using ratio_subtract = typename __ratio_subtract<_R1, _R2>::type;
Howard Hinnante3c67082011-05-31 16:55:36373
Nikolas Klauserc166a9c2024-12-10 15:02:12374# else // _LIBCPP_CXX03_LANG
Howard Hinnante3c67082011-05-31 16:55:36375
Howard Hinnant0c6a0fe2010-11-24 17:05:06376template <class _R1, class _R2>
Nikolas Klauseraf9c04f2025-04-09 21:47:57377struct ratio_subtract : public __ratio_subtract<_R1, _R2>::type {};
Howard Hinnant0c6a0fe2010-11-24 17:05:06378
Nikolas Klauserc166a9c2024-12-10 15:02:12379# endif // _LIBCPP_CXX03_LANG
Howard Hinnante3c67082011-05-31 16:55:36380
Howard Hinnant3e519522010-05-11 19:42:16381// ratio_equal
382
383template <class _R1, class _R2>
Nikolas Klauseraf9c04f2025-04-09 21:47:57384struct ratio_equal : _BoolConstant<(_R1::num == _R2::num && _R1::den == _R2::den)> {
Nikolas Klausere3c958a2024-11-12 14:15:18385 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");
Mark de Weverfe0d2772024-02-11 12:53:59387};
Howard Hinnant3e519522010-05-11 19:42:16388
389template <class _R1, class _R2>
Nikolas Klauseraf9c04f2025-04-09 21:47:57390struct ratio_not_equal : _BoolConstant<!ratio_equal<_R1, _R2>::value> {
Nikolas Klausere3c958a2024-11-12 14:15:18391 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");
Mark de Weverfe0d2772024-02-11 12:53:59393};
Howard Hinnant3e519522010-05-11 19:42:16394
395// ratio_less
396
Louis Dionne9783f282023-12-18 19:01:33397template <class _R1,
398 class _R2,
399 bool _Odd = false,
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>
404struct __ratio_less1 {
405 static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
Howard Hinnant3e519522010-05-11 19:42:16406};
407
Howard Hinnantc003db12011-11-29 18:15:50408template <class _R1, class _R2, bool _Odd, intmax_t _Qp>
Louis Dionne9783f282023-12-18 19:01:33409struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0> {
410 static const bool value = false;
Howard Hinnant3e519522010-05-11 19:42:16411};
412
Howard Hinnantc003db12011-11-29 18:15:50413template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>
Louis Dionne9783f282023-12-18 19:01:33414struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2> {
415 static const bool value = !_Odd;
Howard Hinnant3e519522010-05-11 19:42:16416};
417
Howard Hinnantc003db12011-11-29 18:15:50418template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1>
Louis Dionne9783f282023-12-18 19:01:33419struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0> {
420 static const bool value = _Odd;
Howard Hinnant3e519522010-05-11 19:42:16421};
422
Louis Dionne9783f282023-12-18 19:01:33423template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1, intmax_t _M2>
424struct __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;
Howard Hinnant3e519522010-05-11 19:42:16426};
427
Nikolas Klausere3c958a2024-11-12 14:15:18428template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>, intmax_t _S2 = __static_sign<_R2::num> >
Louis Dionne9783f282023-12-18 19:01:33429struct __ratio_less {
430 static const bool value = _S1 < _S2;
Howard Hinnant3e519522010-05-11 19:42:16431};
432
433template <class _R1, class _R2>
Louis Dionne9783f282023-12-18 19:01:33434struct __ratio_less<_R1, _R2, 1LL, 1LL> {
435 static const bool value = __ratio_less1<_R1, _R2>::value;
Howard Hinnant3e519522010-05-11 19:42:16436};
437
438template <class _R1, class _R2>
Louis Dionne9783f282023-12-18 19:01:33439struct __ratio_less<_R1, _R2, -1LL, -1LL> {
440 static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
Howard Hinnant3e519522010-05-11 19:42:16441};
442
443template <class _R1, class _R2>
Nikolas Klauseraf9c04f2025-04-09 21:47:57444struct ratio_less : _BoolConstant<__ratio_less<_R1, _R2>::value> {
Nikolas Klausere3c958a2024-11-12 14:15:18445 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");
Mark de Weverfe0d2772024-02-11 12:53:59447};
Howard Hinnant3e519522010-05-11 19:42:16448
449template <class _R1, class _R2>
Nikolas Klauseraf9c04f2025-04-09 21:47:57450struct ratio_less_equal : _BoolConstant<!ratio_less<_R2, _R1>::value> {
Nikolas Klausere3c958a2024-11-12 14:15:18451 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");
Mark de Weverfe0d2772024-02-11 12:53:59453};
Howard Hinnant3e519522010-05-11 19:42:16454
455template <class _R1, class _R2>
Nikolas Klauseraf9c04f2025-04-09 21:47:57456struct ratio_greater : _BoolConstant<ratio_less<_R2, _R1>::value> {
Nikolas Klausere3c958a2024-11-12 14:15:18457 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");
Mark de Weverfe0d2772024-02-11 12:53:59459};
Howard Hinnant3e519522010-05-11 19:42:16460
461template <class _R1, class _R2>
Nikolas Klauseraf9c04f2025-04-09 21:47:57462struct ratio_greater_equal : _BoolConstant<!ratio_less<_R1, _R2>::value> {
Nikolas Klausere3c958a2024-11-12 14:15:18463 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");
Mark de Weverfe0d2772024-02-11 12:53:59465};
Howard Hinnant3e519522010-05-11 19:42:16466
467template <class _R1, class _R2>
Nikolas Klauserf6958522025-01-08 16:12:59468using __ratio_gcd _LIBCPP_NODEBUG = ratio<__static_gcd<_R1::num, _R2::num>, __static_lcm<_R1::den, _R2::den> >;
Howard Hinnant3e519522010-05-11 19:42:16469
Nikolas Klauserc166a9c2024-12-10 15:02:12470# if _LIBCPP_STD_VER >= 17
Marshall Clow40a01d52018-01-02 17:17:01471template <class _R1, class _R2>
Louis Dionnecb793e12021-09-22 13:35:32472inline constexpr bool ratio_equal_v = ratio_equal<_R1, _R2>::value;
Marshall Clow825a9ed2015-11-30 05:04:48473
Marshall Clow40a01d52018-01-02 17:17:01474template <class _R1, class _R2>
Louis Dionnecb793e12021-09-22 13:35:32475inline constexpr bool ratio_not_equal_v = ratio_not_equal<_R1, _R2>::value;
Marshall Clow825a9ed2015-11-30 05:04:48476
Marshall Clow40a01d52018-01-02 17:17:01477template <class _R1, class _R2>
Louis Dionnecb793e12021-09-22 13:35:32478inline constexpr bool ratio_less_v = ratio_less<_R1, _R2>::value;
Marshall Clow825a9ed2015-11-30 05:04:48479
Marshall Clow40a01d52018-01-02 17:17:01480template <class _R1, class _R2>
Louis Dionnecb793e12021-09-22 13:35:32481inline constexpr bool ratio_less_equal_v = ratio_less_equal<_R1, _R2>::value;
Marshall Clow825a9ed2015-11-30 05:04:48482
Marshall Clow40a01d52018-01-02 17:17:01483template <class _R1, class _R2>
Louis Dionnecb793e12021-09-22 13:35:32484inline constexpr bool ratio_greater_v = ratio_greater<_R1, _R2>::value;
Marshall Clow825a9ed2015-11-30 05:04:48485
Marshall Clow40a01d52018-01-02 17:17:01486template <class _R1, class _R2>
Louis Dionnecb793e12021-09-22 13:35:32487inline constexpr bool ratio_greater_equal_v = ratio_greater_equal<_R1, _R2>::value;
Nikolas Klauserc166a9c2024-12-10 15:02:12488# endif
Marshall Clow825a9ed2015-11-30 05:04:48489
Howard Hinnant3e519522010-05-11 19:42:16490_LIBCPP_END_NAMESPACE_STD
491
Eric Fiseliera016efb2017-05-31 22:07:49492_LIBCPP_POP_MACROS
493
Nikolas Klauserc166a9c2024-12-10 15:02:12494# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
495# include <type_traits>
496# endif
Nikolas Klauserb9a26582024-12-21 12:01:48497#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
Nikolas Klauserdc21ce42023-01-15 18:53:49498
Louis Dionne4cd6ca12021-04-20 16:03:32499#endif // _LIBCPP_RATIO