ratio 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // ratio -*- C++ -*-
  2. // Copyright (C) 2008, 2009 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 ratio
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_RATIO
  24. #define _GLIBCXX_RATIO 1
  25. #pragma GCC system_header
  26. #ifndef __GXX_EXPERIMENTAL_CXX0X__
  27. # include <c++0x_warning.h>
  28. #else
  29. #include <type_traits>
  30. #include <cstdint>
  31. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  32. namespace std
  33. {
  34. /**
  35. * @defgroup ratio Rational Arithmetic
  36. * @ingroup utilities
  37. *
  38. * Compile time representation of fininte rational numbers.
  39. * @{
  40. */
  41. template<intmax_t _Pn>
  42. struct __static_sign
  43. : integral_constant<intmax_t, (_Pn < 0) ? -1 : 1>
  44. { };
  45. template<intmax_t _Pn>
  46. struct __static_abs
  47. : integral_constant<intmax_t, _Pn * __static_sign<_Pn>::value>
  48. { };
  49. template<intmax_t _Pn, intmax_t _Qn>
  50. struct __static_gcd;
  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, "overflow in multiplication");
  86. public:
  87. static const intmax_t value = _Pn * _Qn;
  88. };
  89. // Helpers for __safe_add
  90. template<intmax_t _Pn, intmax_t _Qn, bool>
  91. struct __add_overflow_check_impl
  92. : integral_constant<bool, (_Pn <= __INTMAX_MAX__ - _Qn)>
  93. { };
  94. template<intmax_t _Pn, intmax_t _Qn>
  95. struct __add_overflow_check_impl<_Pn, _Qn, false>
  96. : integral_constant<bool, (_Pn >= -__INTMAX_MAX__ - _Qn)>
  97. { };
  98. template<intmax_t _Pn, intmax_t _Qn>
  99. struct __add_overflow_check
  100. : __add_overflow_check_impl<_Pn, _Qn, (_Qn >= 0)>
  101. { };
  102. template<intmax_t _Pn, intmax_t _Qn>
  103. struct __safe_add
  104. {
  105. static_assert(__add_overflow_check<_Pn, _Qn>::value != 0,
  106. "overflow in addition");
  107. static const intmax_t value = _Pn + _Qn;
  108. };
  109. /**
  110. * @brief Provides compile-time rational arithmetic.
  111. *
  112. * This class template represents any finite rational number with a
  113. * numerator and denominator representable by compile-time constants of
  114. * type intmax_t. The ratio is simplified when instantiated.
  115. *
  116. * For example:
  117. * @code
  118. * std::ratio<7,-21>::num == -1;
  119. * std::ratio<7,-21>::den == 3;
  120. * @endcode
  121. *
  122. */
  123. template<intmax_t _Num, intmax_t _Den = 1>
  124. struct ratio
  125. {
  126. static_assert(_Den != 0, "denominator cannot be zero");
  127. static_assert(_Num >= -__INTMAX_MAX__ && _Den >= -__INTMAX_MAX__,
  128. "out of range");
  129. // Note: sign(N) * abs(N) == N
  130. static const intmax_t num =
  131. _Num * __static_sign<_Den>::value / __static_gcd<_Num, _Den>::value;
  132. static const intmax_t den =
  133. __static_abs<_Den>::value / __static_gcd<_Num, _Den>::value;
  134. };
  135. template<intmax_t _Num, intmax_t _Den>
  136. const intmax_t ratio<_Num, _Den>::num;
  137. template<intmax_t _Num, intmax_t _Den>
  138. const intmax_t ratio<_Num, _Den>::den;
  139. /// ratio_add
  140. template<typename _R1, typename _R2>
  141. struct ratio_add
  142. {
  143. private:
  144. static const intmax_t __gcd =
  145. __static_gcd<_R1::den, _R2::den>::value;
  146. public:
  147. typedef ratio<
  148. __safe_add<
  149. __safe_multiply<_R1::num, (_R2::den / __gcd)>::value,
  150. __safe_multiply<_R2::num, (_R1::den / __gcd)>::value>::value,
  151. __safe_multiply<_R1::den, (_R2::den / __gcd)>::value> type;
  152. };
  153. /// ratio_subtract
  154. template<typename _R1, typename _R2>
  155. struct ratio_subtract
  156. {
  157. typedef typename ratio_add<
  158. _R1,
  159. ratio<-_R2::num, _R2::den>>::type type;
  160. };
  161. /// ratio_multiply
  162. template<typename _R1, typename _R2>
  163. struct ratio_multiply
  164. {
  165. private:
  166. static const intmax_t __gcd1 =
  167. __static_gcd<_R1::num, _R2::den>::value;
  168. static const intmax_t __gcd2 =
  169. __static_gcd<_R2::num, _R1::den>::value;
  170. public:
  171. typedef ratio<
  172. __safe_multiply<(_R1::num / __gcd1),
  173. (_R2::num / __gcd2)>::value,
  174. __safe_multiply<(_R1::den / __gcd2),
  175. (_R2::den / __gcd1)>::value> type;
  176. };
  177. /// ratio_divide
  178. template<typename _R1, typename _R2>
  179. struct ratio_divide
  180. {
  181. static_assert(_R2::num != 0, "division by 0");
  182. typedef typename ratio_multiply<
  183. _R1,
  184. ratio<_R2::den, _R2::num>>::type type;
  185. };
  186. /// ratio_equal
  187. template<typename _R1, typename _R2>
  188. struct ratio_equal
  189. : integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den>
  190. { };
  191. /// ratio_not_equal
  192. template<typename _R1, typename _R2>
  193. struct ratio_not_equal
  194. : integral_constant<bool, !ratio_equal<_R1, _R2>::value>
  195. { };
  196. template<typename _R1, typename _R2>
  197. struct __ratio_less_simple_impl
  198. : integral_constant<bool,
  199. (__safe_multiply<_R1::num, _R2::den>::value
  200. < __safe_multiply<_R2::num, _R1::den>::value)>
  201. { };
  202. // If the denominators are equal or the signs differ, we can just compare
  203. // numerators, otherwise fallback to the simple cross-multiply method.
  204. template<typename _R1, typename _R2>
  205. struct __ratio_less_impl
  206. : conditional<(_R1::den == _R2::den
  207. || (__static_sign<_R1::num>::value
  208. != __static_sign<_R2::num>::value)),
  209. integral_constant<bool, (_R1::num < _R2::num)>,
  210. __ratio_less_simple_impl<_R1, _R2>>::type
  211. { };
  212. /// ratio_less
  213. template<typename _R1, typename _R2>
  214. struct ratio_less
  215. : __ratio_less_impl<_R1, _R2>::type
  216. { };
  217. /// ratio_less_equal
  218. template<typename _R1, typename _R2>
  219. struct ratio_less_equal
  220. : integral_constant<bool, !ratio_less<_R2, _R1>::value>
  221. { };
  222. /// ratio_greater
  223. template<typename _R1, typename _R2>
  224. struct ratio_greater
  225. : integral_constant<bool, ratio_less<_R2, _R1>::value>
  226. { };
  227. /// ratio_greater_equal
  228. template<typename _R1, typename _R2>
  229. struct ratio_greater_equal
  230. : integral_constant<bool, !ratio_less<_R1, _R2>::value>
  231. { };
  232. typedef ratio<1, 1000000000000000000> atto;
  233. typedef ratio<1, 1000000000000000> femto;
  234. typedef ratio<1, 1000000000000> pico;
  235. typedef ratio<1, 1000000000> nano;
  236. typedef ratio<1, 1000000> micro;
  237. typedef ratio<1, 1000> milli;
  238. typedef ratio<1, 100> centi;
  239. typedef ratio<1, 10> deci;
  240. typedef ratio< 10, 1> deca;
  241. typedef ratio< 100, 1> hecto;
  242. typedef ratio< 1000, 1> kilo;
  243. typedef ratio< 1000000, 1> mega;
  244. typedef ratio< 1000000000, 1> giga;
  245. typedef ratio< 1000000000000, 1> tera;
  246. typedef ratio< 1000000000000000, 1> peta;
  247. typedef ratio< 1000000000000000000, 1> exa;
  248. // @} group ratio
  249. }
  250. #endif //_GLIBCXX_USE_C99_STDINT_TR1
  251. #endif //__GXX_EXPERIMENTAL_CXX0X__
  252. #endif //_GLIBCXX_RATIO