29 #ifndef _GLIBCXX_RATIO
30 #define _GLIBCXX_RATIO 1
32 #pragma GCC system_header
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
41 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 template<
intmax_t _Pn>
57 : integral_constant<intmax_t, (_Pn < 0) ? -1 : 1>
60 template<intmax_t _Pn>
62 : integral_constant<intmax_t, _Pn * __static_sign<_Pn>::value>
65 template<intmax_t _Pn, intmax_t _Qn>
68 template<intmax_t _Pn, intmax_t _Qn>
70 : __static_gcd<_Qn, (_Pn % _Qn)>
73 template<intmax_t _Pn>
74 struct __static_gcd<_Pn, 0>
75 : integral_constant<intmax_t, __static_abs<_Pn>::value>
78 template<intmax_t _Qn>
79 struct __static_gcd<0, _Qn>
80 : integral_constant<intmax_t, __static_abs<_Qn>::value>
89 template<intmax_t _Pn, intmax_t _Qn>
90 struct __safe_multiply
93 static const uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
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;
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, "overflow in multiplication");
110 static const intmax_t value = _Pn * _Qn;
114 template<intmax_t _Pn, intmax_t _Qn, bool>
115 struct __add_overflow_check_impl
116 : integral_constant<bool, (_Pn <= __INTMAX_MAX__ - _Qn)>
119 template<intmax_t _Pn, intmax_t _Qn>
120 struct __add_overflow_check_impl<_Pn, _Qn, false>
121 : integral_constant<bool, (_Pn >= -__INTMAX_MAX__ - _Qn)>
124 template<intmax_t _Pn, intmax_t _Qn>
125 struct __add_overflow_check
126 : __add_overflow_check_impl<_Pn, _Qn, (_Qn >= 0)>
129 template<intmax_t _Pn, intmax_t _Qn>
132 static_assert(__add_overflow_check<_Pn, _Qn>::value != 0,
133 "overflow in addition");
135 static const intmax_t value = _Pn + _Qn;
152 template<intmax_t _Num, intmax_t _Den = 1>
155 static_assert(_Den != 0, "denominator cannot be zero");
156 static_assert(_Num >= -__INTMAX_MAX__ && _Den >= -__INTMAX_MAX__,
160 static constexpr intmax_t num =
161 _Num * __static_sign<_Den>::value / __static_gcd<_Num, _Den>::value;
163 static constexpr intmax_t den =
164 __static_abs<_Den>::value / __static_gcd<_Num, _Den>::value;
166 typedef ratio<num, den> type;
169 template<intmax_t _Num, intmax_t _Den>
170 constexpr intmax_t ratio<_Num, _Den>::num;
172 template<intmax_t _Num, intmax_t _Den>
173 constexpr intmax_t ratio<_Num, _Den>::den;
176 template<typename _R1, typename _R2>
180 static constexpr intmax_t __gcd =
181 __static_gcd<_R1::den, _R2::den>::value;
182 static constexpr intmax_t __n = __safe_add<
183 __safe_multiply<_R1::num, (_R2::den / __gcd)>::value,
184 __safe_multiply<_R2::num, (_R1::den / __gcd)>::value>::value;
188 static constexpr intmax_t __gcd2 = __static_gcd<__n, __gcd>::value;
191 typedef ratio<__n / __gcd2,
192 __safe_multiply<_R1::den / __gcd2, _R2::den / __gcd>::value> type;
194 static constexpr intmax_t num = type::num;
195 static constexpr intmax_t den = type::den;
198 template<typename _R1, typename _R2>
199 constexpr intmax_t ratio_add<_R1, _R2>::num;
201 template<typename _R1, typename _R2>
202 constexpr intmax_t ratio_add<_R1, _R2>::den;
205 template<typename _R1, typename _R2>
206 struct ratio_subtract
208 typedef typename ratio_add<
210 ratio<-_R2::num, _R2::den>>::type type;
212 static constexpr intmax_t num = type::num;
213 static constexpr intmax_t den = type::den;
216 template<typename _R1, typename _R2>
217 constexpr intmax_t ratio_subtract<_R1, _R2>::num;
219 template<typename _R1, typename _R2>
220 constexpr intmax_t ratio_subtract<_R1, _R2>::den;
223 template<typename _R1, typename _R2>
224 struct ratio_multiply
227 static const intmax_t __gcd1 =
228 __static_gcd<_R1::num, _R2::den>::value;
229 static const intmax_t __gcd2 =
230 __static_gcd<_R2::num, _R1::den>::value;
234 __safe_multiply<(_R1::num / __gcd1),
235 (_R2::num / __gcd2)>::value,
236 __safe_multiply<(_R1::den / __gcd2),
237 (_R2::den / __gcd1)>::value> type;
239 static constexpr intmax_t num = type::num;
240 static constexpr intmax_t den = type::den;
243 template<typename _R1, typename _R2>
244 constexpr intmax_t ratio_multiply<_R1, _R2>::num;
246 template<typename _R1, typename _R2>
247 constexpr intmax_t ratio_multiply<_R1, _R2>::den;
250 template<typename _R1, typename _R2>
253 static_assert(_R2::num != 0, "division by 0");
255 typedef typename ratio_multiply<
257 ratio<_R2::den, _R2::num>>::type type;
259 static constexpr intmax_t num = type::num;
260 static constexpr intmax_t den = type::den;
263 template<typename _R1, typename _R2>
264 constexpr intmax_t ratio_divide<_R1, _R2>::num;
266 template<typename _R1, typename _R2>
267 constexpr intmax_t ratio_divide<_R1, _R2>::den;
270 template<typename _R1, typename _R2>
272 : integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den>
276 template<typename _R1, typename _R2>
277 struct ratio_not_equal
278 : integral_constant<bool, !ratio_equal<_R1, _R2>::value>
284 template<typename _R1, typename _R2>
285 struct __ratio_less_impl_2;
289 template<typename _R1, typename _R2, intmax_t __q1 = _R1::num / _R1::den,
290 intmax_t __q2 = _R2::num / _R2::den, bool __eq = (__q1 == __q2)>
291 struct __ratio_less_impl_1
292 : __ratio_less_impl_2<ratio<_R1::num % _R1::den, _R1::den>,
293 ratio<_R2::num % _R2::den, _R2::den> >::type
296 template<typename _R1, typename _R2, intmax_t __q1, intmax_t __q2>
297 struct __ratio_less_impl_1<_R1, _R2, __q1, __q2, false>
298 : integral_constant<bool, (__q1 < __q2) >
301 template<typename _R1, typename _R2>
302 struct __ratio_less_impl_2
303 : __ratio_less_impl_1<ratio<_R2::den, _R2::num>,
304 ratio<_R1::den, _R1::num> >::type
307 template<intmax_t __d1, typename _R2>
308 struct __ratio_less_impl_2<ratio<0, __d1>, _R2>
309 : integral_constant<bool, true>
312 template<typename _R1, intmax_t __d2>
313 struct __ratio_less_impl_2<_R1, ratio<0, __d2> >
314 : integral_constant<bool, false>
317 template<intmax_t __d1, intmax_t __d2>
318 struct __ratio_less_impl_2<ratio<0, __d1>, ratio<0, __d2> >
319 : integral_constant<bool, false>
322 template<typename _R1, typename _R2,
323 bool = (_R1::num == 0 || _R2::num == 0
324 || (__static_sign<_R1::num>::value
325 != __static_sign<_R2::num>::value)),
326 bool = (__static_sign<_R1::num>::value == -1
327 && __static_sign<_R2::num>::value == -1)>
328 struct __ratio_less_impl
329 : __ratio_less_impl_1<_R1, _R2>::type
332 template<typename _R1, typename _R2>
333 struct __ratio_less_impl<_R1, _R2, true, false>
334 : integral_constant<bool, _R1::num < _R2::num>
337 template<typename _R1, typename _R2>
338 struct __ratio_less_impl<_R1, _R2, false, true>
339 : __ratio_less_impl_1<ratio<-_R2::num, _R2::den>,
340 ratio<-_R1::num, _R1::den> >::type
345 template<typename _R1, typename _R2>
347 : __ratio_less_impl<_R1, _R2>::type
351 template<typename _R1, typename _R2>
352 struct ratio_less_equal
353 : integral_constant<bool, !ratio_less<_R2, _R1>::value>
357 template<typename _R1, typename _R2>
359 : integral_constant<bool, ratio_less<_R2, _R1>::value>
363 template<typename _R1, typename _R2>
364 struct ratio_greater_equal
365 : integral_constant<bool, !ratio_less<_R1, _R2>::value>
368 typedef ratio<1, 1000000000000000000> atto;
369 typedef ratio<1, 1000000000000000> femto;
370 typedef ratio<1, 1000000000000> pico;
371 typedef ratio<1, 1000000000> nano;
372 typedef ratio<1, 1000000> micro;
373 typedef ratio<1, 1000> milli;
374 typedef ratio<1, 100> centi;
375 typedef ratio<1, 10> deci;
376 typedef ratio< 10, 1> deca;
377 typedef ratio< 100, 1> hecto;
378 typedef ratio< 1000, 1> kilo;
379 typedef ratio< 1000000, 1> mega;
380 typedef ratio< 1000000000, 1> giga;
381 typedef ratio< 1000000000000, 1> tera;
382 typedef ratio< 1000000000000000, 1> peta;
383 typedef ratio< 1000000000000000000, 1> exa;
386 _GLIBCXX_END_NAMESPACE_VERSION