ratio 16 KB

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