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