libstdc++
ratio
Go to the documentation of this file.
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     : __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   /// ratio_multiply
00286   template<typename _R1, typename _R2>
00287     struct ratio_multiply
00288     {
00289     private:
00290       static const intmax_t __gcd1 =
00291         __static_gcd<_R1::num, _R2::den>::value;
00292       static const intmax_t __gcd2 =
00293         __static_gcd<_R2::num, _R1::den>::value;
00294 
00295     public:
00296       typedef ratio<
00297         __safe_multiply<(_R1::num / __gcd1),
00298                         (_R2::num / __gcd2)>::value,
00299         __safe_multiply<(_R1::den / __gcd2),
00300                         (_R2::den / __gcd1)>::value> type;
00301 
00302       static constexpr intmax_t num = type::num;
00303       static constexpr intmax_t den = type::den;
00304     };
00305 
00306   template<typename _R1, typename _R2>
00307     constexpr intmax_t ratio_multiply<_R1, _R2>::num;
00308 
00309   template<typename _R1, typename _R2>
00310     constexpr intmax_t ratio_multiply<_R1, _R2>::den;
00311 
00312   /// ratio_divide
00313   template<typename _R1, typename _R2>
00314     struct ratio_divide
00315     {
00316       static_assert(_R2::num != 0, "division by 0");
00317 
00318       typedef typename ratio_multiply<
00319         _R1,
00320         ratio<_R2::den, _R2::num>>::type type;
00321 
00322       static constexpr intmax_t num = type::num;
00323       static constexpr intmax_t den = type::den;
00324     };
00325 
00326   template<typename _R1, typename _R2>
00327     constexpr intmax_t ratio_divide<_R1, _R2>::num;
00328 
00329   template<typename _R1, typename _R2>
00330     constexpr intmax_t ratio_divide<_R1, _R2>::den;
00331 
00332   /// ratio_equal
00333   template<typename _R1, typename _R2>
00334     struct ratio_equal
00335     : integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den>
00336     { };
00337   
00338   /// ratio_not_equal
00339   template<typename _R1, typename _R2>
00340     struct ratio_not_equal
00341     : integral_constant<bool, !ratio_equal<_R1, _R2>::value>
00342     { };
00343 
00344   // Both numbers are positive.
00345   template<typename _R1, typename _R2,
00346            typename _Left = __big_mul<_R1::num,_R2::den>,
00347            typename _Right = __big_mul<_R2::num,_R1::den> >
00348     struct __ratio_less_impl_1
00349     : integral_constant<bool, __big_less<_Left::__hi, _Left::__lo,
00350            _Right::__hi, _Right::__lo>::value>
00351     { }; 
00352 
00353   template<typename _R1, typename _R2,
00354        bool = (_R1::num == 0 || _R2::num == 0
00355            || (__static_sign<_R1::num>::value
00356                != __static_sign<_R2::num>::value)),
00357        bool = (__static_sign<_R1::num>::value == -1
00358            && __static_sign<_R2::num>::value == -1)>
00359     struct __ratio_less_impl
00360     : __ratio_less_impl_1<_R1, _R2>::type
00361     { };
00362 
00363   template<typename _R1, typename _R2>
00364     struct __ratio_less_impl<_R1, _R2, true, false>
00365     : integral_constant<bool, _R1::num < _R2::num>
00366     { };
00367 
00368   template<typename _R1, typename _R2>
00369     struct __ratio_less_impl<_R1, _R2, false, true>
00370     : __ratio_less_impl_1<ratio<-_R2::num, _R2::den>,
00371            ratio<-_R1::num, _R1::den> >::type
00372     { };
00373 
00374   /// ratio_less
00375   template<typename _R1, typename _R2>
00376     struct ratio_less
00377     : __ratio_less_impl<_R1, _R2>::type
00378     { };
00379     
00380   /// ratio_less_equal
00381   template<typename _R1, typename _R2>
00382     struct ratio_less_equal
00383     : integral_constant<bool, !ratio_less<_R2, _R1>::value>
00384     { };
00385   
00386   /// ratio_greater
00387   template<typename _R1, typename _R2>
00388     struct ratio_greater
00389     : integral_constant<bool, ratio_less<_R2, _R1>::value>
00390     { };
00391 
00392   /// ratio_greater_equal
00393   template<typename _R1, typename _R2>
00394     struct ratio_greater_equal
00395     : integral_constant<bool, !ratio_less<_R1, _R2>::value>
00396     { };
00397 
00398   template<typename _R1, typename _R2,
00399       bool = (_R1::num >= 0),
00400       bool = (_R2::num >= 0),
00401       bool = ratio_less<ratio<__static_abs<_R1::num>::value, _R1::den>,
00402         ratio<__static_abs<_R2::num>::value, _R2::den> >::value>
00403     struct __ratio_add_impl
00404     {
00405     private:
00406       typedef typename __ratio_add_impl<
00407         ratio<-_R1::num, _R1::den>,
00408         ratio<-_R2::num, _R2::den> >::type __t;
00409     public:
00410       typedef ratio<-__t::num, __t::den> type;
00411     };
00412 
00413   // True addition of nonnegative numbers.
00414   template<typename _R1, typename _R2, bool __b>
00415     struct __ratio_add_impl<_R1, _R2, true, true, __b>
00416     {
00417     private:
00418       static constexpr uintmax_t __g = __static_gcd<_R1::den, _R2::den>::value;
00419       static constexpr uintmax_t __d2 = _R2::den / __g;
00420       typedef __big_mul<_R1::den, __d2> __d;
00421       typedef __big_mul<_R1::num, _R2::den / __g> __x;
00422       typedef __big_mul<_R2::num, _R1::den / __g> __y;
00423       typedef __big_add<__x::__hi, __x::__lo, __y::__hi, __y::__lo> __n;
00424       static_assert(__n::__hi >= __x::__hi, "Internal library error");
00425       typedef __big_div<__n::__hi, __n::__lo, __g> __ng;
00426       static constexpr uintmax_t __g2 = __static_gcd<__ng::__rem, __g>::value;
00427       typedef __big_div<__n::__hi, __n::__lo, __g2> __n_final;
00428       static_assert(__n_final::__rem == 0, "Internal library error");
00429       static_assert(__n_final::__quot_hi == 0 &&
00430         __n_final::__quot_lo <= __INTMAX_MAX__, "overflow in addition");
00431       typedef __big_mul<_R1::den / __g2, __d2> __d_final;
00432       static_assert(__d_final::__hi == 0 &&
00433         __d_final::__lo <= __INTMAX_MAX__, "overflow in addition");
00434     public:
00435       typedef ratio<__n_final::__quot_lo, __d_final::__lo> type;
00436     };
00437 
00438   template<typename _R1, typename _R2>
00439     struct __ratio_add_impl<_R1, _R2, false, true, true>
00440     : __ratio_add_impl<_R2, _R1>
00441     { };
00442 
00443   // True subtraction of nonnegative numbers yielding a nonnegative result.
00444   template<typename _R1, typename _R2>
00445     struct __ratio_add_impl<_R1, _R2, true, false, false>
00446     {
00447     private:
00448       static constexpr uintmax_t __g = __static_gcd<_R1::den, _R2::den>::value;
00449       static constexpr uintmax_t __d2 = _R2::den / __g;
00450       typedef __big_mul<_R1::den, __d2> __d;
00451       typedef __big_mul<_R1::num, _R2::den / __g> __x;
00452       typedef __big_mul<-_R2::num, _R1::den / __g> __y;
00453       typedef __big_sub<__x::__hi, __x::__lo, __y::__hi, __y::__lo> __n;
00454       typedef __big_div<__n::__hi, __n::__lo, __g> __ng;
00455       static constexpr uintmax_t __g2 = __static_gcd<__ng::__rem, __g>::value;
00456       typedef __big_div<__n::__hi, __n::__lo, __g2> __n_final;
00457       static_assert(__n_final::__rem == 0, "Internal library error");
00458       static_assert(__n_final::__quot_hi == 0 &&
00459         __n_final::__quot_lo <= __INTMAX_MAX__, "overflow in addition");
00460       typedef __big_mul<_R1::den / __g2, __d2> __d_final;
00461       static_assert(__d_final::__hi == 0 &&
00462         __d_final::__lo <= __INTMAX_MAX__, "overflow in addition");
00463     public:
00464       typedef ratio<__n_final::__quot_lo, __d_final::__lo> type;
00465     };
00466 
00467   /// ratio_add
00468   template<typename _R1, typename _R2>
00469     struct ratio_add
00470     {
00471       typedef typename __ratio_add_impl<_R1, _R2>::type type;
00472       static constexpr intmax_t num = type::num;
00473       static constexpr intmax_t den = type::den;
00474     };
00475 
00476   template<typename _R1, typename _R2>
00477     constexpr intmax_t ratio_add<_R1, _R2>::num;
00478 
00479   template<typename _R1, typename _R2>
00480     constexpr intmax_t ratio_add<_R1, _R2>::den;
00481 
00482   /// ratio_subtract
00483   template<typename _R1, typename _R2>
00484     struct ratio_subtract
00485     {
00486       typedef typename ratio_add<
00487         _R1,
00488         ratio<-_R2::num, _R2::den>>::type type;
00489 
00490       static constexpr intmax_t num = type::num;
00491       static constexpr intmax_t den = type::den;
00492     };
00493 
00494   template<typename _R1, typename _R2>
00495     constexpr intmax_t ratio_subtract<_R1, _R2>::num;
00496 
00497   template<typename _R1, typename _R2>
00498     constexpr intmax_t ratio_subtract<_R1, _R2>::den;
00499 
00500 
00501 
00502   typedef ratio<1,       1000000000000000000> atto;
00503   typedef ratio<1,          1000000000000000> femto;
00504   typedef ratio<1,             1000000000000> pico;
00505   typedef ratio<1,                1000000000> nano;
00506   typedef ratio<1,                   1000000> micro;
00507   typedef ratio<1,                      1000> milli;
00508   typedef ratio<1,                       100> centi;
00509   typedef ratio<1,                        10> deci;
00510   typedef ratio<                       10, 1> deca;
00511   typedef ratio<                      100, 1> hecto;
00512   typedef ratio<                     1000, 1> kilo;
00513   typedef ratio<                  1000000, 1> mega;
00514   typedef ratio<               1000000000, 1> giga;
00515   typedef ratio<            1000000000000, 1> tera;
00516   typedef ratio<         1000000000000000, 1> peta;
00517   typedef ratio<      1000000000000000000, 1> exa;
00518 
00519   // @} group ratio
00520 _GLIBCXX_END_NAMESPACE_VERSION
00521 } // namespace
00522 
00523 #endif //_GLIBCXX_USE_C99_STDINT_TR1
00524 
00525 #endif //__GXX_EXPERIMENTAL_CXX0X__
00526 
00527 #endif //_GLIBCXX_RATIO