libstdc++
|
00001 // ratio -*- C++ -*- 00002 00003 // Copyright (C) 2008-2013 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file include/ratio 00026 * This is a Standard C++ Library header. 00027 */ 00028 00029 #ifndef _GLIBCXX_RATIO 00030 #define _GLIBCXX_RATIO 1 00031 00032 #pragma GCC system_header 00033 00034 #if __cplusplus < 201103L 00035 # include <bits/c++0x_warning.h> 00036 #else 00037 00038 #include <type_traits> 00039 #include <cstdint> 00040 00041 #ifdef _GLIBCXX_USE_C99_STDINT_TR1 00042 00043 namespace std _GLIBCXX_VISIBILITY(default) 00044 { 00045 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00046 00047 /** 00048 * @defgroup ratio Rational Arithmetic 00049 * @ingroup utilities 00050 * 00051 * Compile time representation of finite rational numbers. 00052 * @{ 00053 */ 00054 00055 template<intmax_t _Pn> 00056 struct __static_sign 00057 : integral_constant<intmax_t, (_Pn < 0) ? -1 : 1> 00058 { }; 00059 00060 template<intmax_t _Pn> 00061 struct __static_abs 00062 : integral_constant<intmax_t, _Pn * __static_sign<_Pn>::value> 00063 { }; 00064 00065 template<intmax_t _Pn, intmax_t _Qn> 00066 struct __static_gcd 00067 : __static_gcd<_Qn, (_Pn % _Qn)> 00068 { }; 00069 00070 template<intmax_t _Pn> 00071 struct __static_gcd<_Pn, 0> 00072 : integral_constant<intmax_t, __static_abs<_Pn>::value> 00073 { }; 00074 00075 template<intmax_t _Qn> 00076 struct __static_gcd<0, _Qn> 00077 : integral_constant<intmax_t, __static_abs<_Qn>::value> 00078 { }; 00079 00080 // Let c = 2^(half # of bits in an intmax_t) 00081 // then we find a1, a0, b1, b0 s.t. N = a1*c + a0, M = b1*c + b0 00082 // The multiplication of N and M becomes, 00083 // N * M = (a1 * b1)c^2 + (a0 * b1 + b0 * a1)c + a0 * b0 00084 // Multiplication is safe if each term and the sum of the terms 00085 // is representable by intmax_t. 00086 template<intmax_t _Pn, intmax_t _Qn> 00087 struct __safe_multiply 00088 { 00089 private: 00090 static const uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4); 00091 00092 static const uintmax_t __a0 = __static_abs<_Pn>::value % __c; 00093 static const uintmax_t __a1 = __static_abs<_Pn>::value / __c; 00094 static const uintmax_t __b0 = __static_abs<_Qn>::value % __c; 00095 static const uintmax_t __b1 = __static_abs<_Qn>::value / __c; 00096 00097 static_assert(__a1 == 0 || __b1 == 0, 00098 "overflow in multiplication"); 00099 static_assert(__a0 * __b1 + __b0 * __a1 < (__c >> 1), 00100 "overflow in multiplication"); 00101 static_assert(__b0 * __a0 <= __INTMAX_MAX__, 00102 "overflow in multiplication"); 00103 static_assert((__a0 * __b1 + __b0 * __a1) * __c 00104 <= __INTMAX_MAX__ - __b0 * __a0, 00105 "overflow in multiplication"); 00106 00107 public: 00108 static const intmax_t value = _Pn * _Qn; 00109 }; 00110 00111 // Some double-precision utilities, where numbers are represented as 00112 // __hi*2^(8*sizeof(uintmax_t)) + __lo. 00113 template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2> 00114 struct __big_less 00115 : integral_constant<bool, (__hi1 < __hi2 00116 || (__hi1 == __hi2 && __lo1 < __lo2))> 00117 { }; 00118 00119 template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2> 00120 struct __big_add 00121 { 00122 static constexpr uintmax_t __lo = __lo1 + __lo2; 00123 static constexpr uintmax_t __hi = (__hi1 + __hi2 + 00124 (__lo1 + __lo2 < __lo1)); // carry 00125 }; 00126 00127 // Subtract a number from a bigger one. 00128 template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2> 00129 struct __big_sub 00130 { 00131 static_assert(!__big_less<__hi1, __lo1, __hi2, __lo2>::value, 00132 "Internal library error"); 00133 static constexpr uintmax_t __lo = __lo1 - __lo2; 00134 static constexpr uintmax_t __hi = (__hi1 - __hi2 - 00135 (__lo1 < __lo2)); // carry 00136 }; 00137 00138 // Same principle as __safe_multiply. 00139 template<uintmax_t __x, uintmax_t __y> 00140 struct __big_mul 00141 { 00142 private: 00143 static constexpr uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4); 00144 static constexpr uintmax_t __x0 = __x % __c; 00145 static constexpr uintmax_t __x1 = __x / __c; 00146 static constexpr uintmax_t __y0 = __y % __c; 00147 static constexpr uintmax_t __y1 = __y / __c; 00148 static constexpr uintmax_t __x0y0 = __x0 * __y0; 00149 static constexpr uintmax_t __x0y1 = __x0 * __y1; 00150 static constexpr uintmax_t __x1y0 = __x1 * __y0; 00151 static constexpr uintmax_t __x1y1 = __x1 * __y1; 00152 static constexpr uintmax_t __mix = __x0y1 + __x1y0; // possible carry... 00153 static constexpr uintmax_t __mix_lo = __mix * __c; 00154 static constexpr uintmax_t __mix_hi 00155 = __mix / __c + ((__mix < __x0y1) ? __c : 0); // ... added here 00156 typedef __big_add<__mix_hi, __mix_lo, __x1y1, __x0y0> _Res; 00157 public: 00158 static constexpr uintmax_t __hi = _Res::__hi; 00159 static constexpr uintmax_t __lo = _Res::__lo; 00160 }; 00161 00162 // Adapted from __udiv_qrnnd_c in longlong.h 00163 // This version assumes that the high bit of __d is 1. 00164 template<uintmax_t __n1, uintmax_t __n0, uintmax_t __d> 00165 struct __big_div_impl 00166 { 00167 private: 00168 static_assert(__d >= (uintmax_t(1) << (sizeof(intmax_t) * 8 - 1)), 00169 "Internal library error"); 00170 static_assert(__n1 < __d, "Internal library error"); 00171 static constexpr uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4); 00172 static constexpr uintmax_t __d1 = __d / __c; 00173 static constexpr uintmax_t __d0 = __d % __c; 00174 00175 static constexpr uintmax_t __q1x = __n1 / __d1; 00176 static constexpr uintmax_t __r1x = __n1 % __d1; 00177 static constexpr uintmax_t __m = __q1x * __d0; 00178 static constexpr uintmax_t __r1y = __r1x * __c + __n0 / __c; 00179 static constexpr uintmax_t __r1z = __r1y + __d; 00180 static constexpr uintmax_t __r1 00181 = ((__r1y < __m) ? ((__r1z >= __d) && (__r1z < __m)) 00182 ? (__r1z + __d) : __r1z : __r1y) - __m; 00183 static constexpr uintmax_t __q1 00184 = __q1x - ((__r1y < __m) 00185 ? ((__r1z >= __d) && (__r1z < __m)) ? 2 : 1 : 0); 00186 static constexpr uintmax_t __q0x = __r1 / __d1; 00187 static constexpr uintmax_t __r0x = __r1 % __d1; 00188 static constexpr uintmax_t __n = __q0x * __d0; 00189 static constexpr uintmax_t __r0y = __r0x * __c + __n0 % __c; 00190 static constexpr uintmax_t __r0z = __r0y + __d; 00191 static constexpr uintmax_t __r0 00192 = ((__r0y < __n) ? ((__r0z >= __d) && (__r0z < __n)) 00193 ? (__r0z + __d) : __r0z : __r0y) - __n; 00194 static constexpr uintmax_t __q0 00195 = __q0x - ((__r0y < __n) ? ((__r0z >= __d) 00196 && (__r0z < __n)) ? 2 : 1 : 0); 00197 00198 public: 00199 static constexpr uintmax_t __quot = __q1 * __c + __q0; 00200 static constexpr uintmax_t __rem = __r0; 00201 00202 private: 00203 typedef __big_mul<__quot, __d> _Prod; 00204 typedef __big_add<_Prod::__hi, _Prod::__lo, 0, __rem> _Sum; 00205 static_assert(_Sum::__hi == __n1 && _Sum::__lo == __n0, 00206 "Internal library error"); 00207 }; 00208 00209 template<uintmax_t __n1, uintmax_t __n0, uintmax_t __d> 00210 struct __big_div 00211 { 00212 private: 00213 static_assert(__d != 0, "Internal library error"); 00214 static_assert(sizeof (uintmax_t) == sizeof (unsigned long long), 00215 "This library calls __builtin_clzll on uintmax_t, which " 00216 "is unsafe on your platform. Please complain to " 00217 "http://gcc.gnu.org/bugzilla/"); 00218 static constexpr int __shift = __builtin_clzll(__d); 00219 static constexpr int __coshift_ = sizeof(uintmax_t) * 8 - __shift; 00220 static constexpr int __coshift = (__shift != 0) ? __coshift_ : 0; 00221 static constexpr uintmax_t __c1 = uintmax_t(1) << __shift; 00222 static constexpr uintmax_t __c2 = uintmax_t(1) << __coshift; 00223 static constexpr uintmax_t __new_d = __d * __c1; 00224 static constexpr uintmax_t __new_n0 = __n0 * __c1; 00225 static constexpr uintmax_t __n1_shifted = (__n1 % __d) * __c1; 00226 static constexpr uintmax_t __n0_top = (__shift != 0) ? (__n0 / __c2) : 0; 00227 static constexpr uintmax_t __new_n1 = __n1_shifted + __n0_top; 00228 typedef __big_div_impl<__new_n1, __new_n0, __new_d> _Res; 00229 00230 public: 00231 static constexpr uintmax_t __quot_hi = __n1 / __d; 00232 static constexpr uintmax_t __quot_lo = _Res::__quot; 00233 static constexpr uintmax_t __rem = _Res::__rem / __c1; 00234 00235 private: 00236 typedef __big_mul<__quot_lo, __d> _P0; 00237 typedef __big_mul<__quot_hi, __d> _P1; 00238 typedef __big_add<_P0::__hi, _P0::__lo, _P1::__lo, __rem> _Sum; 00239 // No overflow. 00240 static_assert(_P1::__hi == 0, "Internal library error"); 00241 static_assert(_Sum::__hi >= _P0::__hi, "Internal library error"); 00242 // Matches the input data. 00243 static_assert(_Sum::__hi == __n1 && _Sum::__lo == __n0, 00244 "Internal library error"); 00245 static_assert(__rem < __d, "Internal library error"); 00246 }; 00247 00248 /** 00249 * @brief Provides compile-time rational arithmetic. 00250 * 00251 * This class template represents any finite rational number with a 00252 * numerator and denominator representable by compile-time constants of 00253 * type intmax_t. The ratio is simplified when instantiated. 00254 * 00255 * For example: 00256 * @code 00257 * std::ratio<7,-21>::num == -1; 00258 * std::ratio<7,-21>::den == 3; 00259 * @endcode 00260 * 00261 */ 00262 template<intmax_t _Num, intmax_t _Den = 1> 00263 struct ratio 00264 { 00265 static_assert(_Den != 0, "denominator cannot be zero"); 00266 static_assert(_Num >= -__INTMAX_MAX__ && _Den >= -__INTMAX_MAX__, 00267 "out of range"); 00268 00269 // Note: sign(N) * abs(N) == N 00270 static constexpr intmax_t num = 00271 _Num * __static_sign<_Den>::value / __static_gcd<_Num, _Den>::value; 00272 00273 static constexpr intmax_t den = 00274 __static_abs<_Den>::value / __static_gcd<_Num, _Den>::value; 00275 00276 typedef ratio<num, den> type; 00277 }; 00278 00279 template<intmax_t _Num, intmax_t _Den> 00280 constexpr intmax_t ratio<_Num, _Den>::num; 00281 00282 template<intmax_t _Num, intmax_t _Den> 00283 constexpr intmax_t ratio<_Num, _Den>::den; 00284 00285 template<typename _R1, typename _R2> 00286 struct __ratio_multiply 00287 { 00288 private: 00289 static const intmax_t __gcd1 = 00290 __static_gcd<_R1::num, _R2::den>::value; 00291 static const intmax_t __gcd2 = 00292 __static_gcd<_R2::num, _R1::den>::value; 00293 00294 public: 00295 typedef ratio< 00296 __safe_multiply<(_R1::num / __gcd1), 00297 (_R2::num / __gcd2)>::value, 00298 __safe_multiply<(_R1::den / __gcd2), 00299 (_R2::den / __gcd1)>::value> type; 00300 00301 static constexpr intmax_t num = type::num; 00302 static constexpr intmax_t den = type::den; 00303 }; 00304 00305 template<typename _R1, typename _R2> 00306 constexpr intmax_t __ratio_multiply<_R1, _R2>::num; 00307 00308 template<typename _R1, typename _R2> 00309 constexpr intmax_t __ratio_multiply<_R1, _R2>::den; 00310 00311 /// ratio_multiply 00312 template<typename _R1, typename _R2> 00313 using ratio_multiply = typename __ratio_multiply<_R1, _R2>::type; 00314 00315 template<typename _R1, typename _R2> 00316 struct __ratio_divide 00317 { 00318 static_assert(_R2::num != 0, "division by 0"); 00319 00320 typedef typename __ratio_multiply< 00321 _R1, 00322 ratio<_R2::den, _R2::num>>::type type; 00323 00324 static constexpr intmax_t num = type::num; 00325 static constexpr intmax_t den = type::den; 00326 }; 00327 00328 template<typename _R1, typename _R2> 00329 constexpr intmax_t __ratio_divide<_R1, _R2>::num; 00330 00331 template<typename _R1, typename _R2> 00332 constexpr intmax_t __ratio_divide<_R1, _R2>::den; 00333 00334 /// ratio_divide 00335 template<typename _R1, typename _R2> 00336 using ratio_divide = typename __ratio_divide<_R1, _R2>::type; 00337 00338 /// ratio_equal 00339 template<typename _R1, typename _R2> 00340 struct ratio_equal 00341 : integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den> 00342 { }; 00343 00344 /// ratio_not_equal 00345 template<typename _R1, typename _R2> 00346 struct ratio_not_equal 00347 : integral_constant<bool, !ratio_equal<_R1, _R2>::value> 00348 { }; 00349 00350 // Both numbers are positive. 00351 template<typename _R1, typename _R2, 00352 typename _Left = __big_mul<_R1::num,_R2::den>, 00353 typename _Right = __big_mul<_R2::num,_R1::den> > 00354 struct __ratio_less_impl_1 00355 : integral_constant<bool, __big_less<_Left::__hi, _Left::__lo, 00356 _Right::__hi, _Right::__lo>::value> 00357 { }; 00358 00359 template<typename _R1, typename _R2, 00360 bool = (_R1::num == 0 || _R2::num == 0 00361 || (__static_sign<_R1::num>::value 00362 != __static_sign<_R2::num>::value)), 00363 bool = (__static_sign<_R1::num>::value == -1 00364 && __static_sign<_R2::num>::value == -1)> 00365 struct __ratio_less_impl 00366 : __ratio_less_impl_1<_R1, _R2>::type 00367 { }; 00368 00369 template<typename _R1, typename _R2> 00370 struct __ratio_less_impl<_R1, _R2, true, false> 00371 : integral_constant<bool, _R1::num < _R2::num> 00372 { }; 00373 00374 template<typename _R1, typename _R2> 00375 struct __ratio_less_impl<_R1, _R2, false, true> 00376 : __ratio_less_impl_1<ratio<-_R2::num, _R2::den>, 00377 ratio<-_R1::num, _R1::den> >::type 00378 { }; 00379 00380 /// ratio_less 00381 template<typename _R1, typename _R2> 00382 struct ratio_less 00383 : __ratio_less_impl<_R1, _R2>::type 00384 { }; 00385 00386 /// ratio_less_equal 00387 template<typename _R1, typename _R2> 00388 struct ratio_less_equal 00389 : integral_constant<bool, !ratio_less<_R2, _R1>::value> 00390 { }; 00391 00392 /// ratio_greater 00393 template<typename _R1, typename _R2> 00394 struct ratio_greater 00395 : integral_constant<bool, ratio_less<_R2, _R1>::value> 00396 { }; 00397 00398 /// ratio_greater_equal 00399 template<typename _R1, typename _R2> 00400 struct ratio_greater_equal 00401 : integral_constant<bool, !ratio_less<_R1, _R2>::value> 00402 { }; 00403 00404 template<typename _R1, typename _R2, 00405 bool = (_R1::num >= 0), 00406 bool = (_R2::num >= 0), 00407 bool = ratio_less<ratio<__static_abs<_R1::num>::value, _R1::den>, 00408 ratio<__static_abs<_R2::num>::value, _R2::den> >::value> 00409 struct __ratio_add_impl 00410 { 00411 private: 00412 typedef typename __ratio_add_impl< 00413 ratio<-_R1::num, _R1::den>, 00414 ratio<-_R2::num, _R2::den> >::type __t; 00415 public: 00416 typedef ratio<-__t::num, __t::den> type; 00417 }; 00418 00419 // True addition of nonnegative numbers. 00420 template<typename _R1, typename _R2, bool __b> 00421 struct __ratio_add_impl<_R1, _R2, true, true, __b> 00422 { 00423 private: 00424 static constexpr uintmax_t __g = __static_gcd<_R1::den, _R2::den>::value; 00425 static constexpr uintmax_t __d2 = _R2::den / __g; 00426 typedef __big_mul<_R1::den, __d2> __d; 00427 typedef __big_mul<_R1::num, _R2::den / __g> __x; 00428 typedef __big_mul<_R2::num, _R1::den / __g> __y; 00429 typedef __big_add<__x::__hi, __x::__lo, __y::__hi, __y::__lo> __n; 00430 static_assert(__n::__hi >= __x::__hi, "Internal library error"); 00431 typedef __big_div<__n::__hi, __n::__lo, __g> __ng; 00432 static constexpr uintmax_t __g2 = __static_gcd<__ng::__rem, __g>::value; 00433 typedef __big_div<__n::__hi, __n::__lo, __g2> __n_final; 00434 static_assert(__n_final::__rem == 0, "Internal library error"); 00435 static_assert(__n_final::__quot_hi == 0 && 00436 __n_final::__quot_lo <= __INTMAX_MAX__, "overflow in addition"); 00437 typedef __big_mul<_R1::den / __g2, __d2> __d_final; 00438 static_assert(__d_final::__hi == 0 && 00439 __d_final::__lo <= __INTMAX_MAX__, "overflow in addition"); 00440 public: 00441 typedef ratio<__n_final::__quot_lo, __d_final::__lo> type; 00442 }; 00443 00444 template<typename _R1, typename _R2> 00445 struct __ratio_add_impl<_R1, _R2, false, true, true> 00446 : __ratio_add_impl<_R2, _R1> 00447 { }; 00448 00449 // True subtraction of nonnegative numbers yielding a nonnegative result. 00450 template<typename _R1, typename _R2> 00451 struct __ratio_add_impl<_R1, _R2, true, false, false> 00452 { 00453 private: 00454 static constexpr uintmax_t __g = __static_gcd<_R1::den, _R2::den>::value; 00455 static constexpr uintmax_t __d2 = _R2::den / __g; 00456 typedef __big_mul<_R1::den, __d2> __d; 00457 typedef __big_mul<_R1::num, _R2::den / __g> __x; 00458 typedef __big_mul<-_R2::num, _R1::den / __g> __y; 00459 typedef __big_sub<__x::__hi, __x::__lo, __y::__hi, __y::__lo> __n; 00460 typedef __big_div<__n::__hi, __n::__lo, __g> __ng; 00461 static constexpr uintmax_t __g2 = __static_gcd<__ng::__rem, __g>::value; 00462 typedef __big_div<__n::__hi, __n::__lo, __g2> __n_final; 00463 static_assert(__n_final::__rem == 0, "Internal library error"); 00464 static_assert(__n_final::__quot_hi == 0 && 00465 __n_final::__quot_lo <= __INTMAX_MAX__, "overflow in addition"); 00466 typedef __big_mul<_R1::den / __g2, __d2> __d_final; 00467 static_assert(__d_final::__hi == 0 && 00468 __d_final::__lo <= __INTMAX_MAX__, "overflow in addition"); 00469 public: 00470 typedef ratio<__n_final::__quot_lo, __d_final::__lo> type; 00471 }; 00472 00473 template<typename _R1, typename _R2> 00474 struct __ratio_add 00475 { 00476 typedef typename __ratio_add_impl<_R1, _R2>::type type; 00477 static constexpr intmax_t num = type::num; 00478 static constexpr intmax_t den = type::den; 00479 }; 00480 00481 template<typename _R1, typename _R2> 00482 constexpr intmax_t __ratio_add<_R1, _R2>::num; 00483 00484 template<typename _R1, typename _R2> 00485 constexpr intmax_t __ratio_add<_R1, _R2>::den; 00486 00487 /// ratio_add 00488 template<typename _R1, typename _R2> 00489 using ratio_add = typename __ratio_add<_R1, _R2>::type; 00490 00491 template<typename _R1, typename _R2> 00492 struct __ratio_subtract 00493 { 00494 typedef typename __ratio_add< 00495 _R1, 00496 ratio<-_R2::num, _R2::den>>::type type; 00497 00498 static constexpr intmax_t num = type::num; 00499 static constexpr intmax_t den = type::den; 00500 }; 00501 00502 template<typename _R1, typename _R2> 00503 constexpr intmax_t __ratio_subtract<_R1, _R2>::num; 00504 00505 template<typename _R1, typename _R2> 00506 constexpr intmax_t __ratio_subtract<_R1, _R2>::den; 00507 00508 /// ratio_subtract 00509 template<typename _R1, typename _R2> 00510 using ratio_subtract = typename __ratio_subtract<_R1, _R2>::type; 00511 00512 00513 typedef ratio<1, 1000000000000000000> atto; 00514 typedef ratio<1, 1000000000000000> femto; 00515 typedef ratio<1, 1000000000000> pico; 00516 typedef ratio<1, 1000000000> nano; 00517 typedef ratio<1, 1000000> micro; 00518 typedef ratio<1, 1000> milli; 00519 typedef ratio<1, 100> centi; 00520 typedef ratio<1, 10> deci; 00521 typedef ratio< 10, 1> deca; 00522 typedef ratio< 100, 1> hecto; 00523 typedef ratio< 1000, 1> kilo; 00524 typedef ratio< 1000000, 1> mega; 00525 typedef ratio< 1000000000, 1> giga; 00526 typedef ratio< 1000000000000, 1> tera; 00527 typedef ratio< 1000000000000000, 1> peta; 00528 typedef ratio< 1000000000000000000, 1> exa; 00529 00530 // @} group ratio 00531 _GLIBCXX_END_NAMESPACE_VERSION 00532 } // namespace 00533 00534 #endif //_GLIBCXX_USE_C99_STDINT_TR1 00535 00536 #endif // C++11 00537 00538 #endif //_GLIBCXX_RATIO