ratio 16 KB

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