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
53 template<
intmax_t _Pn>
55 : integral_constant<intmax_t, (_Pn < 0) ? -1 : 1>
58 template<intmax_t _Pn>
60 : integral_constant<intmax_t, _Pn * __static_sign<_Pn>::value>
63 template<intmax_t _Pn, intmax_t _Qn>
66 template<intmax_t _Pn, intmax_t _Qn>
68 : __static_gcd<_Qn, (_Pn % _Qn)>
71 template<intmax_t _Pn>
72 struct __static_gcd<_Pn, 0>
73 : integral_constant<intmax_t, __static_abs<_Pn>::value>
76 template<intmax_t _Qn>
77 struct __static_gcd<0, _Qn>
78 : integral_constant<intmax_t, __static_abs<_Qn>::value>
87 template<intmax_t _Pn, intmax_t _Qn>
88 struct __safe_multiply
91 static const uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);
93 static const uintmax_t __a0 = __static_abs<_Pn>::value % __c;
94 static const uintmax_t __a1 = __static_abs<_Pn>::value / __c;
95 static const uintmax_t __b0 = __static_abs<_Qn>::value % __c;
96 static const uintmax_t __b1 = __static_abs<_Qn>::value / __c;
98 static_assert(__a1 == 0 || __b1 == 0,
99 "overflow in multiplication");
100 static_assert(__a0 * __b1 + __b0 * __a1 < (__c >> 1),
101 "overflow in multiplication");
102 static_assert(__b0 * __a0 <= __INTMAX_MAX__,
103 "overflow in multiplication");
104 static_assert((__a0 * __b1 + __b0 * __a1) * __c <=
105 __INTMAX_MAX__ - __b0 * __a0, "overflow in multiplication");
108 static const intmax_t value = _Pn * _Qn;
112 template<intmax_t _Pn, intmax_t _Qn, bool>
113 struct __add_overflow_check_impl
114 : integral_constant<bool, (_Pn <= __INTMAX_MAX__ - _Qn)>
117 template<intmax_t _Pn, intmax_t _Qn>
118 struct __add_overflow_check_impl<_Pn, _Qn, false>
119 : integral_constant<bool, (_Pn >= -__INTMAX_MAX__ - _Qn)>
122 template<intmax_t _Pn, intmax_t _Qn>
123 struct __add_overflow_check
124 : __add_overflow_check_impl<_Pn, _Qn, (_Qn >= 0)>
127 template<intmax_t _Pn, intmax_t _Qn>
130 static_assert(__add_overflow_check<_Pn, _Qn>::value != 0,
131 "overflow in addition");
133 static const intmax_t value = _Pn + _Qn;
150 template<intmax_t _Num, intmax_t _Den = 1>
153 static_assert(_Den != 0, "denominator cannot be zero");
154 static_assert(_Num >= -__INTMAX_MAX__ && _Den >= -__INTMAX_MAX__,
158 static const intmax_t num =
159 _Num * __static_sign<_Den>::value / __static_gcd<_Num, _Den>::value;
161 static const intmax_t den =
162 __static_abs<_Den>::value / __static_gcd<_Num, _Den>::value;
165 template<intmax_t _Num, intmax_t _Den>
166 const intmax_t ratio<_Num, _Den>::num;
168 template<intmax_t _Num, intmax_t _Den>
169 const intmax_t ratio<_Num, _Den>::den;
172 template<typename _R1, typename _R2>
176 static const intmax_t __gcd =
177 __static_gcd<_R1::den, _R2::den>::value;
182 __safe_multiply<_R1::num, (_R2::den / __gcd)>::value,
183 __safe_multiply<_R2::num, (_R1::den / __gcd)>::value>::value,
184 __safe_multiply<_R1::den, (_R2::den / __gcd)>::value> type;
188 template<typename _R1, typename _R2>
189 struct ratio_subtract
191 typedef typename ratio_add<
193 ratio<-_R2::num, _R2::den>>::type type;
197 template<typename _R1, typename _R2>
198 struct ratio_multiply
201 static const intmax_t __gcd1 =
202 __static_gcd<_R1::num, _R2::den>::value;
203 static const intmax_t __gcd2 =
204 __static_gcd<_R2::num, _R1::den>::value;
208 __safe_multiply<(_R1::num / __gcd1),
209 (_R2::num / __gcd2)>::value,
210 __safe_multiply<(_R1::den / __gcd2),
211 (_R2::den / __gcd1)>::value> type;
215 template<typename _R1, typename _R2>
218 static_assert(_R2::num != 0, "division by 0");
220 typedef typename ratio_multiply<
222 ratio<_R2::den, _R2::num>>::type type;
226 template<typename _R1, typename _R2>
228 : integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den>
232 template<typename _R1, typename _R2>
233 struct ratio_not_equal
234 : integral_constant<bool, !ratio_equal<_R1, _R2>::value>
237 template<typename _R1, typename _R2>
238 struct __ratio_less_simple_impl
239 : integral_constant<bool,
240 (__safe_multiply<_R1::num, _R2::den>::value
241 < __safe_multiply<_R2::num, _R1::den>::value)>
246 template<typename _R1, typename _R2>
247 struct __ratio_less_impl
248 : conditional<(_R1::den == _R2::den
249 || (__static_sign<_R1::num>::value
250 != __static_sign<_R2::num>::value)),
251 integral_constant<bool, (_R1::num < _R2::num)>,
252 __ratio_less_simple_impl<_R1, _R2>>::type
256 template<typename _R1, typename _R2>
258 : __ratio_less_impl<_R1, _R2>::type
262 template<typename _R1, typename _R2>
263 struct ratio_less_equal
264 : integral_constant<bool, !ratio_less<_R2, _R1>::value>
268 template<typename _R1, typename _R2>
270 : integral_constant<bool, ratio_less<_R2, _R1>::value>
274 template<typename _R1, typename _R2>
275 struct ratio_greater_equal
276 : integral_constant<bool, !ratio_less<_R1, _R2>::value>
279 typedef ratio<1, 1000000000000000000> atto;
280 typedef ratio<1, 1000000000000000> femto;
281 typedef ratio<1, 1000000000000> pico;
282 typedef ratio<1, 1000000000> nano;
283 typedef ratio<1, 1000000> micro;
284 typedef ratio<1, 1000> milli;
285 typedef ratio<1, 100> centi;
286 typedef ratio<1, 10> deci;
287 typedef ratio< 10, 1> deca;
288 typedef ratio< 100, 1> hecto;
289 typedef ratio< 1000, 1> kilo;
290 typedef ratio< 1000000, 1> mega;
291 typedef ratio< 1000000000, 1> giga;
292 typedef ratio< 1000000000000, 1> tera;
293 typedef ratio< 1000000000000000, 1> peta;
294 typedef ratio< 1000000000000000000, 1> exa;