ratio 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. // ratio -*- C++ -*-
  2. // Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/ratio
  21. * This is a Standard C++ Library header.
  22. * @ingroup ratio
  23. */
  24. #ifndef _GLIBCXX_RATIO
  25. #define _GLIBCXX_RATIO 1
  26. #pragma GCC system_header
  27. #if __cplusplus < 201103L
  28. # include <bits/c++0x_warning.h>
  29. #else
  30. #include <type_traits>
  31. #include <cstdint> // intmax_t, uintmax_t
  32. namespace std _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. /**
  36. * @defgroup ratio Rational Arithmetic
  37. * @ingroup utilities
  38. *
  39. * Compile time representation of finite rational numbers.
  40. * @{
  41. */
  42. /// @cond undocumented
  43. template<intmax_t _Pn>
  44. struct __static_sign
  45. : integral_constant<intmax_t, (_Pn < 0) ? -1 : 1>
  46. { };
  47. template<intmax_t _Pn>
  48. struct __static_abs
  49. : integral_constant<intmax_t, _Pn * __static_sign<_Pn>::value>
  50. { };
  51. template<intmax_t _Pn, intmax_t _Qn>
  52. struct __static_gcd
  53. : __static_gcd<_Qn, (_Pn % _Qn)>
  54. { };
  55. template<intmax_t _Pn>
  56. struct __static_gcd<_Pn, 0>
  57. : integral_constant<intmax_t, __static_abs<_Pn>::value>
  58. { };
  59. template<intmax_t _Qn>
  60. struct __static_gcd<0, _Qn>
  61. : integral_constant<intmax_t, __static_abs<_Qn>::value>
  62. { };
  63. // Let c = 2^(half # of bits in an intmax_t)
  64. // then we find a1, a0, b1, b0 s.t. N = a1*c + a0, M = b1*c + b0
  65. // The multiplication of N and M becomes,
  66. // N * M = (a1 * b1)c^2 + (a0 * b1 + b0 * a1)c + a0 * b0
  67. // Multiplication is safe if each term and the sum of the terms
  68. // is representable by intmax_t.
  69. template<intmax_t _Pn, intmax_t _Qn>
  70. struct __safe_multiply
  71. {
  72. private:
  73. static const uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
  74. static const uintmax_t __a0 = __static_abs<_Pn>::value % __c;
  75. static const uintmax_t __a1 = __static_abs<_Pn>::value / __c;
  76. static const uintmax_t __b0 = __static_abs<_Qn>::value % __c;
  77. static const uintmax_t __b1 = __static_abs<_Qn>::value / __c;
  78. static_assert(__a1 == 0 || __b1 == 0,
  79. "overflow in multiplication");
  80. static_assert(__a0 * __b1 + __b0 * __a1 < (__c >> 1),
  81. "overflow in multiplication");
  82. static_assert(__b0 * __a0 <= __INTMAX_MAX__,
  83. "overflow in multiplication");
  84. static_assert((__a0 * __b1 + __b0 * __a1) * __c
  85. <= __INTMAX_MAX__ - __b0 * __a0,
  86. "overflow in multiplication");
  87. public:
  88. static const intmax_t value = _Pn * _Qn;
  89. };
  90. // Some double-precision utilities, where numbers are represented as
  91. // __hi*2^(8*sizeof(uintmax_t)) + __lo.
  92. template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2>
  93. struct __big_less
  94. : integral_constant<bool, (__hi1 < __hi2
  95. || (__hi1 == __hi2 && __lo1 < __lo2))>
  96. { };
  97. template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2>
  98. struct __big_add
  99. {
  100. static constexpr uintmax_t __lo = __lo1 + __lo2;
  101. static constexpr uintmax_t __hi = (__hi1 + __hi2 +
  102. (__lo1 + __lo2 < __lo1)); // carry
  103. };
  104. // Subtract a number from a bigger one.
  105. template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2>
  106. struct __big_sub
  107. {
  108. static_assert(!__big_less<__hi1, __lo1, __hi2, __lo2>::value,
  109. "Internal library error");
  110. static constexpr uintmax_t __lo = __lo1 - __lo2;
  111. static constexpr uintmax_t __hi = (__hi1 - __hi2 -
  112. (__lo1 < __lo2)); // carry
  113. };
  114. // Same principle as __safe_multiply.
  115. template<uintmax_t __x, uintmax_t __y>
  116. struct __big_mul
  117. {
  118. private:
  119. static constexpr uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
  120. static constexpr uintmax_t __x0 = __x % __c;
  121. static constexpr uintmax_t __x1 = __x / __c;
  122. static constexpr uintmax_t __y0 = __y % __c;
  123. static constexpr uintmax_t __y1 = __y / __c;
  124. static constexpr uintmax_t __x0y0 = __x0 * __y0;
  125. static constexpr uintmax_t __x0y1 = __x0 * __y1;
  126. static constexpr uintmax_t __x1y0 = __x1 * __y0;
  127. static constexpr uintmax_t __x1y1 = __x1 * __y1;
  128. static constexpr uintmax_t __mix = __x0y1 + __x1y0; // possible carry...
  129. static constexpr uintmax_t __mix_lo = __mix * __c;
  130. static constexpr uintmax_t __mix_hi
  131. = __mix / __c + ((__mix < __x0y1) ? __c : 0); // ... added here
  132. typedef __big_add<__mix_hi, __mix_lo, __x1y1, __x0y0> _Res;
  133. public:
  134. static constexpr uintmax_t __hi = _Res::__hi;
  135. static constexpr uintmax_t __lo = _Res::__lo;
  136. };
  137. // Adapted from __udiv_qrnnd_c in longlong.h
  138. // This version assumes that the high bit of __d is 1.
  139. template<uintmax_t __n1, uintmax_t __n0, uintmax_t __d>
  140. struct __big_div_impl
  141. {
  142. private:
  143. static_assert(__d >= (uintmax_t(1) << (sizeof(intmax_t) * 8 - 1)),
  144. "Internal library error");
  145. static_assert(__n1 < __d, "Internal library error");
  146. static constexpr uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
  147. static constexpr uintmax_t __d1 = __d / __c;
  148. static constexpr uintmax_t __d0 = __d % __c;
  149. static constexpr uintmax_t __q1x = __n1 / __d1;
  150. static constexpr uintmax_t __r1x = __n1 % __d1;
  151. static constexpr uintmax_t __m = __q1x * __d0;
  152. static constexpr uintmax_t __r1y = __r1x * __c + __n0 / __c;
  153. static constexpr uintmax_t __r1z = __r1y + __d;
  154. static constexpr uintmax_t __r1
  155. = ((__r1y < __m) ? ((__r1z >= __d) && (__r1z < __m))
  156. ? (__r1z + __d) : __r1z : __r1y) - __m;
  157. static constexpr uintmax_t __q1
  158. = __q1x - ((__r1y < __m)
  159. ? ((__r1z >= __d) && (__r1z < __m)) ? 2 : 1 : 0);
  160. static constexpr uintmax_t __q0x = __r1 / __d1;
  161. static constexpr uintmax_t __r0x = __r1 % __d1;
  162. static constexpr uintmax_t __n = __q0x * __d0;
  163. static constexpr uintmax_t __r0y = __r0x * __c + __n0 % __c;
  164. static constexpr uintmax_t __r0z = __r0y + __d;
  165. static constexpr uintmax_t __r0
  166. = ((__r0y < __n) ? ((__r0z >= __d) && (__r0z < __n))
  167. ? (__r0z + __d) : __r0z : __r0y) - __n;
  168. static constexpr uintmax_t __q0
  169. = __q0x - ((__r0y < __n) ? ((__r0z >= __d)
  170. && (__r0z < __n)) ? 2 : 1 : 0);
  171. public:
  172. static constexpr uintmax_t __quot = __q1 * __c + __q0;
  173. static constexpr uintmax_t __rem = __r0;
  174. private:
  175. typedef __big_mul<__quot, __d> _Prod;
  176. typedef __big_add<_Prod::__hi, _Prod::__lo, 0, __rem> _Sum;
  177. static_assert(_Sum::__hi == __n1 && _Sum::__lo == __n0,
  178. "Internal library error");
  179. };
  180. template<uintmax_t __n1, uintmax_t __n0, uintmax_t __d>
  181. struct __big_div
  182. {
  183. private:
  184. static_assert(__d != 0, "Internal library error");
  185. static_assert(sizeof (uintmax_t) == sizeof (unsigned long long),
  186. "This library calls __builtin_clzll on uintmax_t, which "
  187. "is unsafe on your platform. Please complain to "
  188. "http://gcc.gnu.org/bugzilla/");
  189. static constexpr int __shift = __builtin_clzll(__d);
  190. static constexpr int __coshift_ = sizeof(uintmax_t) * 8 - __shift;
  191. static constexpr int __coshift = (__shift != 0) ? __coshift_ : 0;
  192. static constexpr uintmax_t __c1 = uintmax_t(1) << __shift;
  193. static constexpr uintmax_t __c2 = uintmax_t(1) << __coshift;
  194. static constexpr uintmax_t __new_d = __d * __c1;
  195. static constexpr uintmax_t __new_n0 = __n0 * __c1;
  196. static constexpr uintmax_t __n1_shifted = (__n1 % __d) * __c1;
  197. static constexpr uintmax_t __n0_top = (__shift != 0) ? (__n0 / __c2) : 0;
  198. static constexpr uintmax_t __new_n1 = __n1_shifted + __n0_top;
  199. typedef __big_div_impl<__new_n1, __new_n0, __new_d> _Res;
  200. public:
  201. static constexpr uintmax_t __quot_hi = __n1 / __d;
  202. static constexpr uintmax_t __quot_lo = _Res::__quot;
  203. static constexpr uintmax_t __rem = _Res::__rem / __c1;
  204. private:
  205. typedef __big_mul<__quot_lo, __d> _P0;
  206. typedef __big_mul<__quot_hi, __d> _P1;
  207. typedef __big_add<_P0::__hi, _P0::__lo, _P1::__lo, __rem> _Sum;
  208. // No overflow.
  209. static_assert(_P1::__hi == 0, "Internal library error");
  210. static_assert(_Sum::__hi >= _P0::__hi, "Internal library error");
  211. // Matches the input data.
  212. static_assert(_Sum::__hi == __n1 && _Sum::__lo == __n0,
  213. "Internal library error");
  214. static_assert(__rem < __d, "Internal library error");
  215. };
  216. /// @endcond
  217. /**
  218. * @brief Provides compile-time rational arithmetic.
  219. *
  220. * This class template represents any finite rational number with a
  221. * numerator and denominator representable by compile-time constants of
  222. * type intmax_t. The ratio is simplified when instantiated.
  223. *
  224. * For example:
  225. * @code
  226. * std::ratio<7,-21>::num == -1;
  227. * std::ratio<7,-21>::den == 3;
  228. * @endcode
  229. *
  230. */
  231. template<intmax_t _Num, intmax_t _Den = 1>
  232. struct ratio
  233. {
  234. static_assert(_Den != 0, "denominator cannot be zero");
  235. static_assert(_Num >= -__INTMAX_MAX__ && _Den >= -__INTMAX_MAX__,
  236. "out of range");
  237. // Note: sign(N) * abs(N) == N
  238. static constexpr intmax_t num =
  239. _Num * __static_sign<_Den>::value / __static_gcd<_Num, _Den>::value;
  240. static constexpr intmax_t den =
  241. __static_abs<_Den>::value / __static_gcd<_Num, _Den>::value;
  242. typedef ratio<num, den> type;
  243. };
  244. #if ! __cpp_inline_variables
  245. template<intmax_t _Num, intmax_t _Den>
  246. constexpr intmax_t ratio<_Num, _Den>::num;
  247. template<intmax_t _Num, intmax_t _Den>
  248. constexpr intmax_t ratio<_Num, _Den>::den;
  249. #endif
  250. /// @cond undocumented
  251. template<typename _R1, typename _R2>
  252. struct __ratio_multiply
  253. {
  254. private:
  255. static const intmax_t __gcd1 =
  256. __static_gcd<_R1::num, _R2::den>::value;
  257. static const intmax_t __gcd2 =
  258. __static_gcd<_R2::num, _R1::den>::value;
  259. public:
  260. typedef ratio<
  261. __safe_multiply<(_R1::num / __gcd1),
  262. (_R2::num / __gcd2)>::value,
  263. __safe_multiply<(_R1::den / __gcd2),
  264. (_R2::den / __gcd1)>::value> type;
  265. static constexpr intmax_t num = type::num;
  266. static constexpr intmax_t den = type::den;
  267. };
  268. #if ! __cpp_inline_variables
  269. template<typename _R1, typename _R2>
  270. constexpr intmax_t __ratio_multiply<_R1, _R2>::num;
  271. template<typename _R1, typename _R2>
  272. constexpr intmax_t __ratio_multiply<_R1, _R2>::den;
  273. #endif
  274. /// @endcond
  275. /// ratio_multiply
  276. template<typename _R1, typename _R2>
  277. using ratio_multiply = typename __ratio_multiply<_R1, _R2>::type;
  278. /// @cond undocumented
  279. template<typename _R1, typename _R2>
  280. struct __ratio_divide
  281. {
  282. static_assert(_R2::num != 0, "division by 0");
  283. typedef typename __ratio_multiply<
  284. _R1,
  285. ratio<_R2::den, _R2::num>>::type type;
  286. static constexpr intmax_t num = type::num;
  287. static constexpr intmax_t den = type::den;
  288. };
  289. #if ! __cpp_inline_variables
  290. template<typename _R1, typename _R2>
  291. constexpr intmax_t __ratio_divide<_R1, _R2>::num;
  292. template<typename _R1, typename _R2>
  293. constexpr intmax_t __ratio_divide<_R1, _R2>::den;
  294. #endif
  295. /// @endcond
  296. /// ratio_divide
  297. template<typename _R1, typename _R2>
  298. using ratio_divide = typename __ratio_divide<_R1, _R2>::type;
  299. /// ratio_equal
  300. template<typename _R1, typename _R2>
  301. struct ratio_equal
  302. : integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den>
  303. { };
  304. /// ratio_not_equal
  305. template<typename _R1, typename _R2>
  306. struct ratio_not_equal
  307. : integral_constant<bool, !ratio_equal<_R1, _R2>::value>
  308. { };
  309. /// @cond undocumented
  310. // Both numbers are positive.
  311. template<typename _R1, typename _R2,
  312. typename _Left = __big_mul<_R1::num,_R2::den>,
  313. typename _Right = __big_mul<_R2::num,_R1::den> >
  314. struct __ratio_less_impl_1
  315. : integral_constant<bool, __big_less<_Left::__hi, _Left::__lo,
  316. _Right::__hi, _Right::__lo>::value>
  317. { };
  318. template<typename _R1, typename _R2,
  319. bool = (_R1::num == 0 || _R2::num == 0
  320. || (__static_sign<_R1::num>::value
  321. != __static_sign<_R2::num>::value)),
  322. bool = (__static_sign<_R1::num>::value == -1
  323. && __static_sign<_R2::num>::value == -1)>
  324. struct __ratio_less_impl
  325. : __ratio_less_impl_1<_R1, _R2>::type
  326. { };
  327. template<typename _R1, typename _R2>
  328. struct __ratio_less_impl<_R1, _R2, true, false>
  329. : integral_constant<bool, _R1::num < _R2::num>
  330. { };
  331. template<typename _R1, typename _R2>
  332. struct __ratio_less_impl<_R1, _R2, false, true>
  333. : __ratio_less_impl_1<ratio<-_R2::num, _R2::den>,
  334. ratio<-_R1::num, _R1::den> >::type
  335. { };
  336. /// @endcond
  337. /// ratio_less
  338. template<typename _R1, typename _R2>
  339. struct ratio_less
  340. : __ratio_less_impl<_R1, _R2>::type
  341. { };
  342. /// ratio_less_equal
  343. template<typename _R1, typename _R2>
  344. struct ratio_less_equal
  345. : integral_constant<bool, !ratio_less<_R2, _R1>::value>
  346. { };
  347. /// ratio_greater
  348. template<typename _R1, typename _R2>
  349. struct ratio_greater
  350. : integral_constant<bool, ratio_less<_R2, _R1>::value>
  351. { };
  352. /// ratio_greater_equal
  353. template<typename _R1, typename _R2>
  354. struct ratio_greater_equal
  355. : integral_constant<bool, !ratio_less<_R1, _R2>::value>
  356. { };
  357. #if __cplusplus > 201402L
  358. template <typename _R1, typename _R2>
  359. inline constexpr bool ratio_equal_v = ratio_equal<_R1, _R2>::value;
  360. template <typename _R1, typename _R2>
  361. inline constexpr bool ratio_not_equal_v = ratio_not_equal<_R1, _R2>::value;
  362. template <typename _R1, typename _R2>
  363. inline constexpr bool ratio_less_v = ratio_less<_R1, _R2>::value;
  364. template <typename _R1, typename _R2>
  365. inline constexpr bool ratio_less_equal_v =
  366. ratio_less_equal<_R1, _R2>::value;
  367. template <typename _R1, typename _R2>
  368. inline constexpr bool ratio_greater_v = ratio_greater<_R1, _R2>::value;
  369. template <typename _R1, typename _R2>
  370. inline constexpr bool ratio_greater_equal_v
  371. = ratio_greater_equal<_R1, _R2>::value;
  372. #endif // C++17
  373. /// @cond undocumented
  374. template<typename _R1, typename _R2,
  375. bool = (_R1::num >= 0),
  376. bool = (_R2::num >= 0),
  377. bool = ratio_less<ratio<__static_abs<_R1::num>::value, _R1::den>,
  378. ratio<__static_abs<_R2::num>::value, _R2::den> >::value>
  379. struct __ratio_add_impl
  380. {
  381. private:
  382. typedef typename __ratio_add_impl<
  383. ratio<-_R1::num, _R1::den>,
  384. ratio<-_R2::num, _R2::den> >::type __t;
  385. public:
  386. typedef ratio<-__t::num, __t::den> type;
  387. };
  388. // True addition of nonnegative numbers.
  389. template<typename _R1, typename _R2, bool __b>
  390. struct __ratio_add_impl<_R1, _R2, true, true, __b>
  391. {
  392. private:
  393. static constexpr uintmax_t __g = __static_gcd<_R1::den, _R2::den>::value;
  394. static constexpr uintmax_t __d2 = _R2::den / __g;
  395. typedef __big_mul<_R1::den, __d2> __d;
  396. typedef __big_mul<_R1::num, _R2::den / __g> __x;
  397. typedef __big_mul<_R2::num, _R1::den / __g> __y;
  398. typedef __big_add<__x::__hi, __x::__lo, __y::__hi, __y::__lo> __n;
  399. static_assert(__n::__hi >= __x::__hi, "Internal library error");
  400. typedef __big_div<__n::__hi, __n::__lo, __g> __ng;
  401. static constexpr uintmax_t __g2 = __static_gcd<__ng::__rem, __g>::value;
  402. typedef __big_div<__n::__hi, __n::__lo, __g2> __n_final;
  403. static_assert(__n_final::__rem == 0, "Internal library error");
  404. static_assert(__n_final::__quot_hi == 0 &&
  405. __n_final::__quot_lo <= __INTMAX_MAX__, "overflow in addition");
  406. typedef __big_mul<_R1::den / __g2, __d2> __d_final;
  407. static_assert(__d_final::__hi == 0 &&
  408. __d_final::__lo <= __INTMAX_MAX__, "overflow in addition");
  409. public:
  410. typedef ratio<__n_final::__quot_lo, __d_final::__lo> type;
  411. };
  412. template<typename _R1, typename _R2>
  413. struct __ratio_add_impl<_R1, _R2, false, true, true>
  414. : __ratio_add_impl<_R2, _R1>
  415. { };
  416. // True subtraction of nonnegative numbers yielding a nonnegative result.
  417. template<typename _R1, typename _R2>
  418. struct __ratio_add_impl<_R1, _R2, true, false, false>
  419. {
  420. private:
  421. static constexpr uintmax_t __g = __static_gcd<_R1::den, _R2::den>::value;
  422. static constexpr uintmax_t __d2 = _R2::den / __g;
  423. typedef __big_mul<_R1::den, __d2> __d;
  424. typedef __big_mul<_R1::num, _R2::den / __g> __x;
  425. typedef __big_mul<-_R2::num, _R1::den / __g> __y;
  426. typedef __big_sub<__x::__hi, __x::__lo, __y::__hi, __y::__lo> __n;
  427. typedef __big_div<__n::__hi, __n::__lo, __g> __ng;
  428. static constexpr uintmax_t __g2 = __static_gcd<__ng::__rem, __g>::value;
  429. typedef __big_div<__n::__hi, __n::__lo, __g2> __n_final;
  430. static_assert(__n_final::__rem == 0, "Internal library error");
  431. static_assert(__n_final::__quot_hi == 0 &&
  432. __n_final::__quot_lo <= __INTMAX_MAX__, "overflow in addition");
  433. typedef __big_mul<_R1::den / __g2, __d2> __d_final;
  434. static_assert(__d_final::__hi == 0 &&
  435. __d_final::__lo <= __INTMAX_MAX__, "overflow in addition");
  436. public:
  437. typedef ratio<__n_final::__quot_lo, __d_final::__lo> type;
  438. };
  439. template<typename _R1, typename _R2>
  440. struct __ratio_add
  441. {
  442. typedef typename __ratio_add_impl<_R1, _R2>::type type;
  443. static constexpr intmax_t num = type::num;
  444. static constexpr intmax_t den = type::den;
  445. };
  446. #if ! __cpp_inline_variables
  447. template<typename _R1, typename _R2>
  448. constexpr intmax_t __ratio_add<_R1, _R2>::num;
  449. template<typename _R1, typename _R2>
  450. constexpr intmax_t __ratio_add<_R1, _R2>::den;
  451. #endif
  452. /// @endcond
  453. /// ratio_add
  454. template<typename _R1, typename _R2>
  455. using ratio_add = typename __ratio_add<_R1, _R2>::type;
  456. /// @cond undocumented
  457. template<typename _R1, typename _R2>
  458. struct __ratio_subtract
  459. {
  460. typedef typename __ratio_add<
  461. _R1,
  462. ratio<-_R2::num, _R2::den>>::type type;
  463. static constexpr intmax_t num = type::num;
  464. static constexpr intmax_t den = type::den;
  465. };
  466. #if ! __cpp_inline_variables
  467. template<typename _R1, typename _R2>
  468. constexpr intmax_t __ratio_subtract<_R1, _R2>::num;
  469. template<typename _R1, typename _R2>
  470. constexpr intmax_t __ratio_subtract<_R1, _R2>::den;
  471. #endif
  472. /// @endcond
  473. /// ratio_subtract
  474. template<typename _R1, typename _R2>
  475. using ratio_subtract = typename __ratio_subtract<_R1, _R2>::type;
  476. typedef ratio<1, 1000000000000000000> atto;
  477. typedef ratio<1, 1000000000000000> femto;
  478. typedef ratio<1, 1000000000000> pico;
  479. typedef ratio<1, 1000000000> nano;
  480. typedef ratio<1, 1000000> micro;
  481. typedef ratio<1, 1000> milli;
  482. typedef ratio<1, 100> centi;
  483. typedef ratio<1, 10> deci;
  484. typedef ratio< 10, 1> deca;
  485. typedef ratio< 100, 1> hecto;
  486. typedef ratio< 1000, 1> kilo;
  487. typedef ratio< 1000000, 1> mega;
  488. typedef ratio< 1000000000, 1> giga;
  489. typedef ratio< 1000000000000, 1> tera;
  490. typedef ratio< 1000000000000000, 1> peta;
  491. typedef ratio< 1000000000000000000, 1> exa;
  492. /// @} group ratio
  493. _GLIBCXX_END_NAMESPACE_VERSION
  494. } // namespace
  495. #endif // C++11
  496. #endif //_GLIBCXX_RATIO