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