ratio 16 KB

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