ratio 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  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
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_RATIO
  10. #define _LIBCPP_RATIO
  11. /*
  12. ratio synopsis
  13. namespace std
  14. {
  15. template <intmax_t N, intmax_t D = 1>
  16. class ratio
  17. {
  18. public:
  19. static constexpr intmax_t num;
  20. static constexpr intmax_t den;
  21. typedef ratio<num, den> type;
  22. };
  23. // ratio arithmetic
  24. template <class R1, class R2> using ratio_add = ...;
  25. template <class R1, class R2> using ratio_subtract = ...;
  26. template <class R1, class R2> using ratio_multiply = ...;
  27. template <class R1, class R2> using ratio_divide = ...;
  28. // ratio comparison
  29. template <class R1, class R2> struct ratio_equal;
  30. template <class R1, class R2> struct ratio_not_equal;
  31. template <class R1, class R2> struct ratio_less;
  32. template <class R1, class R2> struct ratio_less_equal;
  33. template <class R1, class R2> struct ratio_greater;
  34. template <class R1, class R2> struct ratio_greater_equal;
  35. // convenience SI typedefs
  36. using quecto = ratio <1, 1'000'000'000'000'000'000'000'000'000'000>; // Since C++26; not supported
  37. using ronto = ratio <1, 1'000'000'000'000'000'000'000'000'000>; // Since C++26; not supported
  38. typedef ratio<1, 1000000000000000000000000> yocto; // not supported
  39. typedef ratio<1, 1000000000000000000000> zepto; // not supported
  40. typedef ratio<1, 1000000000000000000> atto;
  41. typedef ratio<1, 1000000000000000> femto;
  42. typedef ratio<1, 1000000000000> pico;
  43. typedef ratio<1, 1000000000> nano;
  44. typedef ratio<1, 1000000> micro;
  45. typedef ratio<1, 1000> milli;
  46. typedef ratio<1, 100> centi;
  47. typedef ratio<1, 10> deci;
  48. typedef ratio< 10, 1> deca;
  49. typedef ratio< 100, 1> hecto;
  50. typedef ratio< 1000, 1> kilo;
  51. typedef ratio< 1000000, 1> mega;
  52. typedef ratio< 1000000000, 1> giga;
  53. typedef ratio< 1000000000000, 1> tera;
  54. typedef ratio< 1000000000000000, 1> peta;
  55. typedef ratio< 1000000000000000000, 1> exa;
  56. typedef ratio< 1000000000000000000000, 1> zetta; // not supported
  57. typedef ratio<1000000000000000000000000, 1> yotta; // not supported
  58. using ronna = ratio <1'000'000'000'000'000'000'000'000'000, 1>; // Since C++26; not supported
  59. using quetta = ratio <1'000'000'000'000'000'000'000'000'000'000, 1>; // Since C++26; not supported
  60. // 20.11.5, ratio comparison
  61. template <class R1, class R2> inline constexpr bool ratio_equal_v
  62. = ratio_equal<R1, R2>::value; // C++17
  63. template <class R1, class R2> inline constexpr bool ratio_not_equal_v
  64. = ratio_not_equal<R1, R2>::value; // C++17
  65. template <class R1, class R2> inline constexpr bool ratio_less_v
  66. = ratio_less<R1, R2>::value; // C++17
  67. template <class R1, class R2> inline constexpr bool ratio_less_equal_v
  68. = ratio_less_equal<R1, R2>::value; // C++17
  69. template <class R1, class R2> inline constexpr bool ratio_greater_v
  70. = ratio_greater<R1, R2>::value; // C++17
  71. template <class R1, class R2> inline constexpr bool ratio_greater_equal_v
  72. = ratio_greater_equal<R1, R2>::value; // C++17
  73. }
  74. */
  75. #include <__assert> // all public C++ headers provide the assertion handler
  76. #include <__config>
  77. #include <__type_traits/integral_constant.h>
  78. #include <climits>
  79. #include <cstdint>
  80. #include <version>
  81. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  82. # pragma GCC system_header
  83. #endif
  84. _LIBCPP_PUSH_MACROS
  85. #include <__undef_macros>
  86. _LIBCPP_BEGIN_NAMESPACE_STD
  87. // __static_gcd
  88. template <intmax_t _Xp, intmax_t _Yp>
  89. struct __static_gcd
  90. {
  91. static const intmax_t value = __static_gcd<_Yp, _Xp % _Yp>::value;
  92. };
  93. template <intmax_t _Xp>
  94. struct __static_gcd<_Xp, 0>
  95. {
  96. static const intmax_t value = _Xp;
  97. };
  98. template <>
  99. struct __static_gcd<0, 0>
  100. {
  101. static const intmax_t value = 1;
  102. };
  103. // __static_lcm
  104. template <intmax_t _Xp, intmax_t _Yp>
  105. struct __static_lcm
  106. {
  107. static const intmax_t value = _Xp / __static_gcd<_Xp, _Yp>::value * _Yp;
  108. };
  109. template <intmax_t _Xp>
  110. struct __static_abs
  111. {
  112. static const intmax_t value = _Xp < 0 ? -_Xp : _Xp;
  113. };
  114. template <intmax_t _Xp>
  115. struct __static_sign
  116. {
  117. static const intmax_t value = _Xp == 0 ? 0 : (_Xp < 0 ? -1 : 1);
  118. };
  119. template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
  120. class __ll_add;
  121. template <intmax_t _Xp, intmax_t _Yp>
  122. class __ll_add<_Xp, _Yp, 1>
  123. {
  124. static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
  125. static const intmax_t max = -min;
  126. static_assert(_Xp <= max - _Yp, "overflow in __ll_add");
  127. public:
  128. static const intmax_t value = _Xp + _Yp;
  129. };
  130. template <intmax_t _Xp, intmax_t _Yp>
  131. class __ll_add<_Xp, _Yp, 0>
  132. {
  133. public:
  134. static const intmax_t value = _Xp;
  135. };
  136. template <intmax_t _Xp, intmax_t _Yp>
  137. class __ll_add<_Xp, _Yp, -1>
  138. {
  139. static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
  140. static const intmax_t max = -min;
  141. static_assert(min - _Yp <= _Xp, "overflow in __ll_add");
  142. public:
  143. static const intmax_t value = _Xp + _Yp;
  144. };
  145. template <intmax_t _Xp, intmax_t _Yp, intmax_t = __static_sign<_Yp>::value>
  146. class __ll_sub;
  147. template <intmax_t _Xp, intmax_t _Yp>
  148. class __ll_sub<_Xp, _Yp, 1>
  149. {
  150. static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
  151. static const intmax_t max = -min;
  152. static_assert(min + _Yp <= _Xp, "overflow in __ll_sub");
  153. public:
  154. static const intmax_t value = _Xp - _Yp;
  155. };
  156. template <intmax_t _Xp, intmax_t _Yp>
  157. class __ll_sub<_Xp, _Yp, 0>
  158. {
  159. public:
  160. static const intmax_t value = _Xp;
  161. };
  162. template <intmax_t _Xp, intmax_t _Yp>
  163. class __ll_sub<_Xp, _Yp, -1>
  164. {
  165. static const intmax_t min = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1;
  166. static const intmax_t max = -min;
  167. static_assert(_Xp <= max + _Yp, "overflow in __ll_sub");
  168. public:
  169. static const intmax_t value = _Xp - _Yp;
  170. };
  171. template <intmax_t _Xp, intmax_t _Yp>
  172. class __ll_mul
  173. {
  174. static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
  175. static const intmax_t min = nan + 1;
  176. static const intmax_t max = -min;
  177. static const intmax_t __a_x = __static_abs<_Xp>::value;
  178. static const intmax_t __a_y = __static_abs<_Yp>::value;
  179. static_assert(_Xp != nan && _Yp != nan && __a_x <= max / __a_y, "overflow in __ll_mul");
  180. public:
  181. static const intmax_t value = _Xp * _Yp;
  182. };
  183. template <intmax_t _Yp>
  184. class __ll_mul<0, _Yp>
  185. {
  186. public:
  187. static const intmax_t value = 0;
  188. };
  189. template <intmax_t _Xp>
  190. class __ll_mul<_Xp, 0>
  191. {
  192. public:
  193. static const intmax_t value = 0;
  194. };
  195. template <>
  196. class __ll_mul<0, 0>
  197. {
  198. public:
  199. static const intmax_t value = 0;
  200. };
  201. // Not actually used but left here in case needed in future maintenance
  202. template <intmax_t _Xp, intmax_t _Yp>
  203. class __ll_div
  204. {
  205. static const intmax_t nan = (1LL << (sizeof(intmax_t) * CHAR_BIT - 1));
  206. static const intmax_t min = nan + 1;
  207. static const intmax_t max = -min;
  208. static_assert(_Xp != nan && _Yp != nan && _Yp != 0, "overflow in __ll_div");
  209. public:
  210. static const intmax_t value = _Xp / _Yp;
  211. };
  212. template <intmax_t _Num, intmax_t _Den = 1>
  213. class _LIBCPP_TEMPLATE_VIS ratio
  214. {
  215. static_assert(__static_abs<_Num>::value >= 0, "ratio numerator is out of range");
  216. static_assert(_Den != 0, "ratio divide by 0");
  217. static_assert(__static_abs<_Den>::value > 0, "ratio denominator is out of range");
  218. static _LIBCPP_CONSTEXPR const intmax_t __na = __static_abs<_Num>::value;
  219. static _LIBCPP_CONSTEXPR const intmax_t __da = __static_abs<_Den>::value;
  220. static _LIBCPP_CONSTEXPR const intmax_t __s = __static_sign<_Num>::value * __static_sign<_Den>::value;
  221. static _LIBCPP_CONSTEXPR const intmax_t __gcd = __static_gcd<__na, __da>::value;
  222. public:
  223. static _LIBCPP_CONSTEXPR const intmax_t num = __s * __na / __gcd;
  224. static _LIBCPP_CONSTEXPR const intmax_t den = __da / __gcd;
  225. typedef ratio<num, den> type;
  226. };
  227. template <intmax_t _Num, intmax_t _Den>
  228. _LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::num;
  229. template <intmax_t _Num, intmax_t _Den>
  230. _LIBCPP_CONSTEXPR const intmax_t ratio<_Num, _Den>::den;
  231. template <class _Tp> struct __is_ratio : false_type {};
  232. template <intmax_t _Num, intmax_t _Den> struct __is_ratio<ratio<_Num, _Den> > : true_type {};
  233. typedef ratio<1LL, 1000000000000000000LL> atto;
  234. typedef ratio<1LL, 1000000000000000LL> femto;
  235. typedef ratio<1LL, 1000000000000LL> pico;
  236. typedef ratio<1LL, 1000000000LL> nano;
  237. typedef ratio<1LL, 1000000LL> micro;
  238. typedef ratio<1LL, 1000LL> milli;
  239. typedef ratio<1LL, 100LL> centi;
  240. typedef ratio<1LL, 10LL> deci;
  241. typedef ratio< 10LL, 1LL> deca;
  242. typedef ratio< 100LL, 1LL> hecto;
  243. typedef ratio< 1000LL, 1LL> kilo;
  244. typedef ratio< 1000000LL, 1LL> mega;
  245. typedef ratio< 1000000000LL, 1LL> giga;
  246. typedef ratio< 1000000000000LL, 1LL> tera;
  247. typedef ratio< 1000000000000000LL, 1LL> peta;
  248. typedef ratio<1000000000000000000LL, 1LL> exa;
  249. template <class _R1, class _R2>
  250. struct __ratio_multiply
  251. {
  252. private:
  253. static const intmax_t __gcd_n1_d2 = __static_gcd<_R1::num, _R2::den>::value;
  254. static const intmax_t __gcd_d1_n2 = __static_gcd<_R1::den, _R2::num>::value;
  255. public:
  256. typedef typename ratio
  257. <
  258. __ll_mul<_R1::num / __gcd_n1_d2, _R2::num / __gcd_d1_n2>::value,
  259. __ll_mul<_R2::den / __gcd_n1_d2, _R1::den / __gcd_d1_n2>::value
  260. >::type type;
  261. };
  262. #ifndef _LIBCPP_CXX03_LANG
  263. template <class _R1, class _R2> using ratio_multiply
  264. = typename __ratio_multiply<_R1, _R2>::type;
  265. #else // _LIBCPP_CXX03_LANG
  266. template <class _R1, class _R2>
  267. struct _LIBCPP_TEMPLATE_VIS ratio_multiply
  268. : public __ratio_multiply<_R1, _R2>::type {};
  269. #endif // _LIBCPP_CXX03_LANG
  270. template <class _R1, class _R2>
  271. struct __ratio_divide
  272. {
  273. private:
  274. static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
  275. static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
  276. public:
  277. typedef typename ratio
  278. <
  279. __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
  280. __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
  281. >::type type;
  282. };
  283. #ifndef _LIBCPP_CXX03_LANG
  284. template <class _R1, class _R2> using ratio_divide
  285. = typename __ratio_divide<_R1, _R2>::type;
  286. #else // _LIBCPP_CXX03_LANG
  287. template <class _R1, class _R2>
  288. struct _LIBCPP_TEMPLATE_VIS ratio_divide
  289. : public __ratio_divide<_R1, _R2>::type {};
  290. #endif // _LIBCPP_CXX03_LANG
  291. template <class _R1, class _R2>
  292. struct __ratio_add
  293. {
  294. private:
  295. static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
  296. static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
  297. public:
  298. typedef typename ratio_multiply
  299. <
  300. ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
  301. ratio
  302. <
  303. __ll_add
  304. <
  305. __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
  306. __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
  307. >::value,
  308. _R2::den
  309. >
  310. >::type type;
  311. };
  312. #ifndef _LIBCPP_CXX03_LANG
  313. template <class _R1, class _R2> using ratio_add
  314. = typename __ratio_add<_R1, _R2>::type;
  315. #else // _LIBCPP_CXX03_LANG
  316. template <class _R1, class _R2>
  317. struct _LIBCPP_TEMPLATE_VIS ratio_add
  318. : public __ratio_add<_R1, _R2>::type {};
  319. #endif // _LIBCPP_CXX03_LANG
  320. template <class _R1, class _R2>
  321. struct __ratio_subtract
  322. {
  323. private:
  324. static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
  325. static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
  326. public:
  327. typedef typename ratio_multiply
  328. <
  329. ratio<__gcd_n1_n2, _R1::den / __gcd_d1_d2>,
  330. ratio
  331. <
  332. __ll_sub
  333. <
  334. __ll_mul<_R1::num / __gcd_n1_n2, _R2::den / __gcd_d1_d2>::value,
  335. __ll_mul<_R2::num / __gcd_n1_n2, _R1::den / __gcd_d1_d2>::value
  336. >::value,
  337. _R2::den
  338. >
  339. >::type type;
  340. };
  341. #ifndef _LIBCPP_CXX03_LANG
  342. template <class _R1, class _R2> using ratio_subtract
  343. = typename __ratio_subtract<_R1, _R2>::type;
  344. #else // _LIBCPP_CXX03_LANG
  345. template <class _R1, class _R2>
  346. struct _LIBCPP_TEMPLATE_VIS ratio_subtract
  347. : public __ratio_subtract<_R1, _R2>::type {};
  348. #endif // _LIBCPP_CXX03_LANG
  349. // ratio_equal
  350. template <class _R1, class _R2>
  351. struct _LIBCPP_TEMPLATE_VIS ratio_equal
  352. : _BoolConstant<(_R1::num == _R2::num && _R1::den == _R2::den)> {};
  353. template <class _R1, class _R2>
  354. struct _LIBCPP_TEMPLATE_VIS ratio_not_equal
  355. : _BoolConstant<!ratio_equal<_R1, _R2>::value> {};
  356. // ratio_less
  357. template <class _R1, class _R2, bool _Odd = false,
  358. intmax_t _Q1 = _R1::num / _R1::den, intmax_t _M1 = _R1::num % _R1::den,
  359. intmax_t _Q2 = _R2::num / _R2::den, intmax_t _M2 = _R2::num % _R2::den>
  360. struct __ratio_less1
  361. {
  362. static const bool value = _Odd ? _Q2 < _Q1 : _Q1 < _Q2;
  363. };
  364. template <class _R1, class _R2, bool _Odd, intmax_t _Qp>
  365. struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, 0>
  366. {
  367. static const bool value = false;
  368. };
  369. template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M2>
  370. struct __ratio_less1<_R1, _R2, _Odd, _Qp, 0, _Qp, _M2>
  371. {
  372. static const bool value = !_Odd;
  373. };
  374. template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1>
  375. struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, 0>
  376. {
  377. static const bool value = _Odd;
  378. };
  379. template <class _R1, class _R2, bool _Odd, intmax_t _Qp, intmax_t _M1,
  380. intmax_t _M2>
  381. struct __ratio_less1<_R1, _R2, _Odd, _Qp, _M1, _Qp, _M2>
  382. {
  383. static const bool value = __ratio_less1<ratio<_R1::den, _M1>,
  384. ratio<_R2::den, _M2>, !_Odd>::value;
  385. };
  386. template <class _R1, class _R2, intmax_t _S1 = __static_sign<_R1::num>::value,
  387. intmax_t _S2 = __static_sign<_R2::num>::value>
  388. struct __ratio_less
  389. {
  390. static const bool value = _S1 < _S2;
  391. };
  392. template <class _R1, class _R2>
  393. struct __ratio_less<_R1, _R2, 1LL, 1LL>
  394. {
  395. static const bool value = __ratio_less1<_R1, _R2>::value;
  396. };
  397. template <class _R1, class _R2>
  398. struct __ratio_less<_R1, _R2, -1LL, -1LL>
  399. {
  400. static const bool value = __ratio_less1<ratio<-_R2::num, _R2::den>, ratio<-_R1::num, _R1::den> >::value;
  401. };
  402. template <class _R1, class _R2>
  403. struct _LIBCPP_TEMPLATE_VIS ratio_less
  404. : _BoolConstant<__ratio_less<_R1, _R2>::value> {};
  405. template <class _R1, class _R2>
  406. struct _LIBCPP_TEMPLATE_VIS ratio_less_equal
  407. : _BoolConstant<!ratio_less<_R2, _R1>::value> {};
  408. template <class _R1, class _R2>
  409. struct _LIBCPP_TEMPLATE_VIS ratio_greater
  410. : _BoolConstant<ratio_less<_R2, _R1>::value> {};
  411. template <class _R1, class _R2>
  412. struct _LIBCPP_TEMPLATE_VIS ratio_greater_equal
  413. : _BoolConstant<!ratio_less<_R1, _R2>::value> {};
  414. template <class _R1, class _R2>
  415. struct __ratio_gcd
  416. {
  417. typedef ratio<__static_gcd<_R1::num, _R2::num>::value,
  418. __static_lcm<_R1::den, _R2::den>::value> type;
  419. };
  420. #if _LIBCPP_STD_VER >= 17
  421. template <class _R1, class _R2>
  422. inline constexpr bool ratio_equal_v = ratio_equal<_R1, _R2>::value;
  423. template <class _R1, class _R2>
  424. inline constexpr bool ratio_not_equal_v = ratio_not_equal<_R1, _R2>::value;
  425. template <class _R1, class _R2>
  426. inline constexpr bool ratio_less_v = ratio_less<_R1, _R2>::value;
  427. template <class _R1, class _R2>
  428. inline constexpr bool ratio_less_equal_v = ratio_less_equal<_R1, _R2>::value;
  429. template <class _R1, class _R2>
  430. inline constexpr bool ratio_greater_v = ratio_greater<_R1, _R2>::value;
  431. template <class _R1, class _R2>
  432. inline constexpr bool ratio_greater_equal_v = ratio_greater_equal<_R1, _R2>::value;
  433. #endif
  434. _LIBCPP_END_NAMESPACE_STD
  435. _LIBCPP_POP_MACROS
  436. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  437. # include <type_traits>
  438. #endif
  439. #endif // _LIBCPP_RATIO