3 // Copyright (C) 2008 Free Software Foundation, Inc.
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 2, or (at your option)
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.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 * This is a Standard C++ Library header.
34 #ifndef _GLIBCXX_RATIO
35 #define _GLIBCXX_RATIO 1
37 #pragma GCC system_header
39 #ifndef __GXX_EXPERIMENTAL_CXX0X__
40 # include <c++0x_warning.h>
43 #include <type_traits>
46 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
50 template<intmax_t _Pn>
52 : integral_constant<intmax_t, (_Pn < 0) ? -1 : 1>
55 template<intmax_t _Pn>
57 : integral_constant<intmax_t, _Pn * __static_sign<_Pn>::value>
60 template<intmax_t _Pn, intmax_t _Qn>
63 template<intmax_t _Pn, intmax_t _Qn>
65 : __static_gcd<_Qn, (_Pn % _Qn)>
68 template<intmax_t _Pn>
69 struct __static_gcd<_Pn, 0>
70 : integral_constant<intmax_t, __static_abs<_Pn>::value>
73 template<intmax_t _Qn>
74 struct __static_gcd<0, _Qn>
75 : integral_constant<intmax_t, __static_abs<_Qn>::value>
78 // Let c = 2^(half # of bits in an intmax_t)
79 // then we find a1, a0, b1, b0 s.t. N = a1*c + a0, M = b1*c + b0
80 // The multiplication of N and M becomes,
81 // N * M = (a1 * b1)c^2 + (a0 * b1 + b0 * a1)c + a0 * b0
82 // Multiplication is safe if each term and the sum of the terms
83 // is representable by intmax_t.
84 template<intmax_t _Pn, intmax_t _Qn>
85 struct __safe_multiply
88 static const uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
90 static const uintmax_t __a0 = __static_abs<_Pn>::value % __c;
91 static const uintmax_t __a1 = __static_abs<_Pn>::value / __c;
92 static const uintmax_t __b0 = __static_abs<_Qn>::value % __c;
93 static const uintmax_t __b1 = __static_abs<_Qn>::value / __c;
95 static_assert(__a1 == 0 || __b1 == 0,
96 "overflow in multiplication");
97 static_assert(__a0 * __b1 + __b0 * __a1 < (__c >> 1),
98 "overflow in multiplication");
99 static_assert(__b0 * __a0 <= __INTMAX_MAX__,
100 "overflow in multiplication");
101 static_assert((__a0 * __b1 + __b0 * __a1) * __c <=
102 __INTMAX_MAX__ - __b0 * __a0, "overflow in multiplication");
105 static const intmax_t value = _Pn * _Qn;
108 // Helpers for __safe_add
109 template<intmax_t _Pn, intmax_t _Qn, bool>
110 struct __add_overflow_check_impl
111 : integral_constant<bool, (_Pn <= __INTMAX_MAX__ - _Qn)>
114 template<intmax_t _Pn, intmax_t _Qn>
115 struct __add_overflow_check_impl<_Pn, _Qn, false>
116 : integral_constant<bool, (_Pn >= -__INTMAX_MAX__ - _Qn)>
119 template<intmax_t _Pn, intmax_t _Qn>
120 struct __add_overflow_check
121 : __add_overflow_check_impl<_Pn, _Qn, (_Qn >= 0)>
124 template<intmax_t _Pn, intmax_t _Qn>
127 static_assert(__add_overflow_check<_Pn, _Qn>::value != 0,
128 "overflow in addition");
130 static const intmax_t value = _Pn + _Qn;
134 * @brief Provides compile-time rational arithmetic.
136 * This class template represents any finite rational number with a
137 * numerator and denominator representable by compile-time constants of
138 * type intmax_t. The ratio is simplified when instantiated.
142 * std::ratio<7,-21>::num == -1;
143 * std::ratio<7,-21>::den == 3;
147 template<intmax_t _Num, intmax_t _Den = 1>
150 static_assert(_Den != 0, "denominator cannot be zero");
151 static_assert(_Num >= -__INTMAX_MAX__ && _Den >= -__INTMAX_MAX__,
154 // Note: sign(N) * abs(N) == N
155 static const intmax_t num =
156 _Num * __static_sign<_Den>::value / __static_gcd<_Num, _Den>::value;
158 static const intmax_t den =
159 __static_abs<_Den>::value / __static_gcd<_Num, _Den>::value;
162 template<intmax_t _Num, intmax_t _Den>
163 const intmax_t ratio<_Num, _Den>::num;
165 template<intmax_t _Num, intmax_t _Den>
166 const intmax_t ratio<_Num, _Den>::den;
168 template<typename _R1, typename _R2>
172 static const intmax_t __gcd =
173 __static_gcd<_R1::den, _R2::den>::value;
178 __safe_multiply<_R1::num, (_R2::den / __gcd)>::value,
179 __safe_multiply<_R2::num, (_R1::den / __gcd)>::value>::value,
180 __safe_multiply<_R1::den, (_R2::den / __gcd)>::value> type;
183 template<typename _R1, typename _R2>
184 struct ratio_subtract
186 typedef typename ratio_add<
188 ratio<-_R2::num, _R2::den>>::type type;
191 template<typename _R1, typename _R2>
192 struct ratio_multiply
195 static const intmax_t __gcd1 =
196 __static_gcd<_R1::num, _R2::den>::value;
197 static const intmax_t __gcd2 =
198 __static_gcd<_R2::num, _R1::den>::value;
202 __safe_multiply<(_R1::num / __gcd1),
203 (_R2::num / __gcd2)>::value,
204 __safe_multiply<(_R1::den / __gcd2),
205 (_R2::den / __gcd1)>::value> type;
208 template<typename _R1, typename _R2>
211 static_assert(_R2::num != 0, "division by 0");
213 typedef typename ratio_multiply<
215 ratio<_R2::den, _R2::num>>::type type;
218 template<typename _R1, typename _R2>
220 : integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den>
223 template<typename _R1, typename _R2>
224 struct ratio_not_equal
225 : integral_constant<bool, !ratio_equal<_R1, _R2>::value>
228 template<typename _R1, typename _R2>
229 struct __ratio_less_simple_impl
230 : integral_constant<bool,
231 (__safe_multiply<_R1::num, _R2::den>::value
232 < __safe_multiply<_R2::num, _R1::den>::value)>
235 // If the denominators are equal or the signs differ, we can just compare
236 // numerators, otherwise fallback to the simple cross-multiply method.
237 template<typename _R1, typename _R2>
238 struct __ratio_less_impl
239 : conditional<(_R1::den == _R2::den
240 || (__static_sign<_R1::num>::value
241 != __static_sign<_R2::num>::value)),
242 integral_constant<bool, (_R1::num < _R2::num)>,
243 __ratio_less_simple_impl<_R1, _R2>>::type
246 template<typename _R1, typename _R2>
248 : __ratio_less_impl<_R1, _R2>::type
251 template<typename _R1, typename _R2>
252 struct ratio_less_equal
253 : integral_constant<bool, !ratio_less<_R2, _R1>::value>
256 template<typename _R1, typename _R2>
258 : integral_constant<bool, ratio_less<_R2, _R1>::value>
261 template<typename _R1, typename _R2>
262 struct ratio_greater_equal
263 : integral_constant<bool, !ratio_less<_R1, _R2>::value>
266 typedef ratio<1, 1000000000000000000> atto;
267 typedef ratio<1, 1000000000000000> femto;
268 typedef ratio<1, 1000000000000> pico;
269 typedef ratio<1, 1000000000> nano;
270 typedef ratio<1, 1000000> micro;
271 typedef ratio<1, 1000> milli;
272 typedef ratio<1, 100> centi;
273 typedef ratio<1, 10> deci;
274 typedef ratio< 10, 1> deca;
275 typedef ratio< 100, 1> hecto;
276 typedef ratio< 1000, 1> kilo;
277 typedef ratio< 1000000, 1> mega;
278 typedef ratio< 1000000000, 1> giga;
279 typedef ratio< 1000000000000, 1> tera;
280 typedef ratio< 1000000000000000, 1> peta;
281 typedef ratio< 1000000000000000000, 1> exa;
284 #endif //_GLIBCXX_USE_C99_STDINT_TR1
286 #endif //__GXX_EXPERIMENTAL_CXX0X__
288 #endif //_GLIBCXX_RATIO