1 | // ratio -*- C++ -*- |
2 | |
3 | // Copyright (C) 2008-2023 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 | * @ingroup ratio |
28 | */ |
29 | |
30 | #ifndef _GLIBCXX_RATIO |
31 | #define _GLIBCXX_RATIO 1 |
32 | |
33 | #pragma GCC system_header |
34 | |
35 | #if __cplusplus < 201103L |
36 | # include <bits/c++0x_warning.h> |
37 | #else |
38 | |
39 | #include <type_traits> |
40 | #include <cstdint> // intmax_t, uintmax_t |
41 | |
42 | namespace std _GLIBCXX_VISIBILITY(default) |
43 | { |
44 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
45 | |
46 | /** |
47 | * @defgroup ratio Rational Arithmetic |
48 | * @ingroup utilities |
49 | * |
50 | * Compile time representation of finite rational numbers. |
51 | * @{ |
52 | */ |
53 | |
54 | /// @cond undocumented |
55 | |
56 | template<intmax_t _Pn> |
57 | struct __static_sign |
58 | : integral_constant<intmax_t, (_Pn < 0) ? -1 : 1> |
59 | { }; |
60 | |
61 | template<intmax_t _Pn> |
62 | struct __static_abs |
63 | : integral_constant<intmax_t, _Pn * __static_sign<_Pn>::value> |
64 | { }; |
65 | |
66 | template<intmax_t _Pn, intmax_t _Qn> |
67 | struct __static_gcd |
68 | : __static_gcd<_Qn, (_Pn % _Qn)> |
69 | { }; |
70 | |
71 | template<intmax_t _Pn> |
72 | struct __static_gcd<_Pn, 0> |
73 | : integral_constant<intmax_t, __static_abs<_Pn>::value> |
74 | { }; |
75 | |
76 | template<intmax_t _Qn> |
77 | struct __static_gcd<0, _Qn> |
78 | : integral_constant<intmax_t, __static_abs<_Qn>::value> |
79 | { }; |
80 | |
81 | // Let c = 2^(half # of bits in an intmax_t) |
82 | // then we find a1, a0, b1, b0 s.t. N = a1*c + a0, M = b1*c + b0 |
83 | // The multiplication of N and M becomes, |
84 | // N * M = (a1 * b1)c^2 + (a0 * b1 + b0 * a1)c + a0 * b0 |
85 | // Multiplication is safe if each term and the sum of the terms |
86 | // is representable by intmax_t. |
87 | template<intmax_t _Pn, intmax_t _Qn> |
88 | struct __safe_multiply |
89 | { |
90 | private: |
91 | static const uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4); |
92 | |
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; |
97 | |
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, |
106 | "overflow in multiplication" ); |
107 | |
108 | public: |
109 | static const intmax_t value = _Pn * _Qn; |
110 | }; |
111 | |
112 | // Some double-precision utilities, where numbers are represented as |
113 | // __hi*2^(8*sizeof(uintmax_t)) + __lo. |
114 | template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2> |
115 | struct __big_less |
116 | : integral_constant<bool, (__hi1 < __hi2 |
117 | || (__hi1 == __hi2 && __lo1 < __lo2))> |
118 | { }; |
119 | |
120 | template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2> |
121 | struct __big_add |
122 | { |
123 | static constexpr uintmax_t __lo = __lo1 + __lo2; |
124 | static constexpr uintmax_t __hi = (__hi1 + __hi2 + |
125 | (__lo1 + __lo2 < __lo1)); // carry |
126 | }; |
127 | |
128 | // Subtract a number from a bigger one. |
129 | template<uintmax_t __hi1, uintmax_t __lo1, uintmax_t __hi2, uintmax_t __lo2> |
130 | struct __big_sub |
131 | { |
132 | static_assert(!__big_less<__hi1, __lo1, __hi2, __lo2>::value, |
133 | "Internal library error" ); |
134 | static constexpr uintmax_t __lo = __lo1 - __lo2; |
135 | static constexpr uintmax_t __hi = (__hi1 - __hi2 - |
136 | (__lo1 < __lo2)); // carry |
137 | }; |
138 | |
139 | // Same principle as __safe_multiply. |
140 | template<uintmax_t __x, uintmax_t __y> |
141 | struct __big_mul |
142 | { |
143 | private: |
144 | static constexpr uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4); |
145 | static constexpr uintmax_t __x0 = __x % __c; |
146 | static constexpr uintmax_t __x1 = __x / __c; |
147 | static constexpr uintmax_t __y0 = __y % __c; |
148 | static constexpr uintmax_t __y1 = __y / __c; |
149 | static constexpr uintmax_t __x0y0 = __x0 * __y0; |
150 | static constexpr uintmax_t __x0y1 = __x0 * __y1; |
151 | static constexpr uintmax_t __x1y0 = __x1 * __y0; |
152 | static constexpr uintmax_t __x1y1 = __x1 * __y1; |
153 | static constexpr uintmax_t __mix = __x0y1 + __x1y0; // possible carry... |
154 | static constexpr uintmax_t __mix_lo = __mix * __c; |
155 | static constexpr uintmax_t __mix_hi |
156 | = __mix / __c + ((__mix < __x0y1) ? __c : 0); // ... added here |
157 | typedef __big_add<__mix_hi, __mix_lo, __x1y1, __x0y0> _Res; |
158 | public: |
159 | static constexpr uintmax_t __hi = _Res::__hi; |
160 | static constexpr uintmax_t __lo = _Res::__lo; |
161 | }; |
162 | |
163 | // Adapted from __udiv_qrnnd_c in longlong.h |
164 | // This version assumes that the high bit of __d is 1. |
165 | template<uintmax_t __n1, uintmax_t __n0, uintmax_t __d> |
166 | struct __big_div_impl |
167 | { |
168 | private: |
169 | static_assert(__d >= (uintmax_t(1) << (sizeof(intmax_t) * 8 - 1)), |
170 | "Internal library error" ); |
171 | static_assert(__n1 < __d, "Internal library error" ); |
172 | static constexpr uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4); |
173 | static constexpr uintmax_t __d1 = __d / __c; |
174 | static constexpr uintmax_t __d0 = __d % __c; |
175 | |
176 | static constexpr uintmax_t __q1x = __n1 / __d1; |
177 | static constexpr uintmax_t __r1x = __n1 % __d1; |
178 | static constexpr uintmax_t __m = __q1x * __d0; |
179 | static constexpr uintmax_t __r1y = __r1x * __c + __n0 / __c; |
180 | static constexpr uintmax_t __r1z = __r1y + __d; |
181 | static constexpr uintmax_t __r1 |
182 | = ((__r1y < __m) ? ((__r1z >= __d) && (__r1z < __m)) |
183 | ? (__r1z + __d) : __r1z : __r1y) - __m; |
184 | static constexpr uintmax_t __q1 |
185 | = __q1x - ((__r1y < __m) |
186 | ? ((__r1z >= __d) && (__r1z < __m)) ? 2 : 1 : 0); |
187 | static constexpr uintmax_t __q0x = __r1 / __d1; |
188 | static constexpr uintmax_t __r0x = __r1 % __d1; |
189 | static constexpr uintmax_t __n = __q0x * __d0; |
190 | static constexpr uintmax_t __r0y = __r0x * __c + __n0 % __c; |
191 | static constexpr uintmax_t __r0z = __r0y + __d; |
192 | static constexpr uintmax_t __r0 |
193 | = ((__r0y < __n) ? ((__r0z >= __d) && (__r0z < __n)) |
194 | ? (__r0z + __d) : __r0z : __r0y) - __n; |
195 | static constexpr uintmax_t __q0 |
196 | = __q0x - ((__r0y < __n) ? ((__r0z >= __d) |
197 | && (__r0z < __n)) ? 2 : 1 : 0); |
198 | |
199 | public: |
200 | static constexpr uintmax_t __quot = __q1 * __c + __q0; |
201 | static constexpr uintmax_t __rem = __r0; |
202 | |
203 | private: |
204 | typedef __big_mul<__quot, __d> _Prod; |
205 | typedef __big_add<_Prod::__hi, _Prod::__lo, 0, __rem> _Sum; |
206 | static_assert(_Sum::__hi == __n1 && _Sum::__lo == __n0, |
207 | "Internal library error" ); |
208 | }; |
209 | |
210 | template<uintmax_t __n1, uintmax_t __n0, uintmax_t __d> |
211 | struct __big_div |
212 | { |
213 | private: |
214 | static_assert(__d != 0, "Internal library error" ); |
215 | static_assert(sizeof (uintmax_t) == sizeof (unsigned long long), |
216 | "This library calls __builtin_clzll on uintmax_t, which " |
217 | "is unsafe on your platform. Please complain to " |
218 | "http://gcc.gnu.org/bugzilla/" ); |
219 | static constexpr int __shift = __builtin_clzll(__d); |
220 | static constexpr int __coshift_ = sizeof(uintmax_t) * 8 - __shift; |
221 | static constexpr int __coshift = (__shift != 0) ? __coshift_ : 0; |
222 | static constexpr uintmax_t __c1 = uintmax_t(1) << __shift; |
223 | static constexpr uintmax_t __c2 = uintmax_t(1) << __coshift; |
224 | static constexpr uintmax_t __new_d = __d * __c1; |
225 | static constexpr uintmax_t __new_n0 = __n0 * __c1; |
226 | static constexpr uintmax_t __n1_shifted = (__n1 % __d) * __c1; |
227 | static constexpr uintmax_t __n0_top = (__shift != 0) ? (__n0 / __c2) : 0; |
228 | static constexpr uintmax_t __new_n1 = __n1_shifted + __n0_top; |
229 | typedef __big_div_impl<__new_n1, __new_n0, __new_d> _Res; |
230 | |
231 | public: |
232 | static constexpr uintmax_t __quot_hi = __n1 / __d; |
233 | static constexpr uintmax_t __quot_lo = _Res::__quot; |
234 | static constexpr uintmax_t __rem = _Res::__rem / __c1; |
235 | |
236 | private: |
237 | typedef __big_mul<__quot_lo, __d> _P0; |
238 | typedef __big_mul<__quot_hi, __d> _P1; |
239 | typedef __big_add<_P0::__hi, _P0::__lo, _P1::__lo, __rem> _Sum; |
240 | // No overflow. |
241 | static_assert(_P1::__hi == 0, "Internal library error" ); |
242 | static_assert(_Sum::__hi >= _P0::__hi, "Internal library error" ); |
243 | // Matches the input data. |
244 | static_assert(_Sum::__hi == __n1 && _Sum::__lo == __n0, |
245 | "Internal library error" ); |
246 | static_assert(__rem < __d, "Internal library error" ); |
247 | }; |
248 | |
249 | /// @endcond |
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 | #if ! __cpp_inline_variables |
283 | template<intmax_t _Num, intmax_t _Den> |
284 | constexpr intmax_t ratio<_Num, _Den>::num; |
285 | |
286 | template<intmax_t _Num, intmax_t _Den> |
287 | constexpr intmax_t ratio<_Num, _Den>::den; |
288 | #endif |
289 | |
290 | /// @cond undocumented |
291 | |
292 | template<typename _Tp> |
293 | struct __is_ratio |
294 | : std::false_type |
295 | { }; |
296 | |
297 | template<intmax_t _Num, intmax_t _Den> |
298 | struct __is_ratio<ratio<_Num, _Den>> |
299 | : std::true_type |
300 | { }; |
301 | |
302 | #if __cpp_variable_templates |
303 | template<typename _Tp> |
304 | constexpr bool __is_ratio_v = false; |
305 | template<intmax_t _Num, intmax_t _Den> |
306 | constexpr bool __is_ratio_v<ratio<_Num, _Den>> = true; |
307 | #endif |
308 | |
309 | template<typename _R1, typename _R2> |
310 | constexpr bool |
311 | __are_both_ratios() noexcept |
312 | { |
313 | #if __cpp_variable_templates && __cpp_if_constexpr |
314 | if constexpr (__is_ratio_v<_R1>) |
315 | if constexpr (__is_ratio_v<_R2>) |
316 | return true; |
317 | return false; |
318 | #else |
319 | return __and_<__is_ratio<_R1>, __is_ratio<_R2>>::value; |
320 | #endif |
321 | } |
322 | |
323 | template<typename _R1, typename _R2> |
324 | struct __ratio_multiply |
325 | { |
326 | static_assert(std::__are_both_ratios<_R1, _R2>(), |
327 | "both template arguments must be a std::ratio" ); |
328 | |
329 | private: |
330 | static const intmax_t __gcd1 = |
331 | __static_gcd<_R1::num, _R2::den>::value; |
332 | static const intmax_t __gcd2 = |
333 | __static_gcd<_R2::num, _R1::den>::value; |
334 | |
335 | public: |
336 | typedef ratio< |
337 | __safe_multiply<(_R1::num / __gcd1), |
338 | (_R2::num / __gcd2)>::value, |
339 | __safe_multiply<(_R1::den / __gcd2), |
340 | (_R2::den / __gcd1)>::value> type; |
341 | |
342 | static constexpr intmax_t num = type::num; |
343 | static constexpr intmax_t den = type::den; |
344 | }; |
345 | |
346 | #if ! __cpp_inline_variables |
347 | template<typename _R1, typename _R2> |
348 | constexpr intmax_t __ratio_multiply<_R1, _R2>::num; |
349 | |
350 | template<typename _R1, typename _R2> |
351 | constexpr intmax_t __ratio_multiply<_R1, _R2>::den; |
352 | #endif |
353 | |
354 | /// @endcond |
355 | |
356 | /// ratio_multiply |
357 | template<typename _R1, typename _R2> |
358 | using ratio_multiply = typename __ratio_multiply<_R1, _R2>::type; |
359 | |
360 | /// @cond undocumented |
361 | |
362 | template<typename _R1, typename _R2> |
363 | struct __ratio_divide |
364 | { |
365 | static_assert(_R2::num != 0, "division by 0" ); |
366 | |
367 | typedef typename __ratio_multiply< |
368 | _R1, |
369 | ratio<_R2::den, _R2::num>>::type type; |
370 | |
371 | static constexpr intmax_t num = type::num; |
372 | static constexpr intmax_t den = type::den; |
373 | }; |
374 | |
375 | #if ! __cpp_inline_variables |
376 | template<typename _R1, typename _R2> |
377 | constexpr intmax_t __ratio_divide<_R1, _R2>::num; |
378 | |
379 | template<typename _R1, typename _R2> |
380 | constexpr intmax_t __ratio_divide<_R1, _R2>::den; |
381 | #endif |
382 | |
383 | /// @endcond |
384 | |
385 | /// ratio_divide |
386 | template<typename _R1, typename _R2> |
387 | using ratio_divide = typename __ratio_divide<_R1, _R2>::type; |
388 | |
389 | /// ratio_equal |
390 | template<typename _R1, typename _R2> |
391 | struct ratio_equal |
392 | : integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den> |
393 | { |
394 | static_assert(std::__are_both_ratios<_R1, _R2>(), |
395 | "both template arguments must be a std::ratio" ); |
396 | }; |
397 | |
398 | /// ratio_not_equal |
399 | template<typename _R1, typename _R2> |
400 | struct ratio_not_equal |
401 | : integral_constant<bool, !ratio_equal<_R1, _R2>::value> |
402 | { }; |
403 | |
404 | /// @cond undocumented |
405 | |
406 | // Both numbers are positive. |
407 | template<typename _R1, typename _R2, |
408 | typename _Left = __big_mul<_R1::num,_R2::den>, |
409 | typename _Right = __big_mul<_R2::num,_R1::den> > |
410 | struct __ratio_less_impl_1 |
411 | : integral_constant<bool, __big_less<_Left::__hi, _Left::__lo, |
412 | _Right::__hi, _Right::__lo>::value> |
413 | { }; |
414 | |
415 | template<typename _R1, typename _R2, |
416 | bool = (_R1::num == 0 || _R2::num == 0 |
417 | || (__static_sign<_R1::num>::value |
418 | != __static_sign<_R2::num>::value)), |
419 | bool = (__static_sign<_R1::num>::value == -1 |
420 | && __static_sign<_R2::num>::value == -1)> |
421 | struct __ratio_less_impl |
422 | : __ratio_less_impl_1<_R1, _R2>::type |
423 | { }; |
424 | |
425 | template<typename _R1, typename _R2> |
426 | struct __ratio_less_impl<_R1, _R2, true, false> |
427 | : integral_constant<bool, _R1::num < _R2::num> |
428 | { }; |
429 | |
430 | template<typename _R1, typename _R2> |
431 | struct __ratio_less_impl<_R1, _R2, false, true> |
432 | : __ratio_less_impl_1<ratio<-_R2::num, _R2::den>, |
433 | ratio<-_R1::num, _R1::den> >::type |
434 | { }; |
435 | |
436 | /// @endcond |
437 | |
438 | /// ratio_less |
439 | template<typename _R1, typename _R2> |
440 | struct ratio_less |
441 | : __ratio_less_impl<_R1, _R2>::type |
442 | { |
443 | static_assert(std::__are_both_ratios<_R1, _R2>(), |
444 | "both template arguments must be a std::ratio" ); |
445 | }; |
446 | |
447 | /// ratio_less_equal |
448 | template<typename _R1, typename _R2> |
449 | struct ratio_less_equal |
450 | : integral_constant<bool, !ratio_less<_R2, _R1>::value> |
451 | { }; |
452 | |
453 | /// ratio_greater |
454 | template<typename _R1, typename _R2> |
455 | struct ratio_greater |
456 | : integral_constant<bool, ratio_less<_R2, _R1>::value> |
457 | { }; |
458 | |
459 | /// ratio_greater_equal |
460 | template<typename _R1, typename _R2> |
461 | struct ratio_greater_equal |
462 | : integral_constant<bool, !ratio_less<_R1, _R2>::value> |
463 | { }; |
464 | |
465 | #if __cplusplus > 201402L |
466 | template <typename _R1, typename _R2> |
467 | inline constexpr bool ratio_equal_v = ratio_equal<_R1, _R2>::value; |
468 | template <typename _R1, typename _R2> |
469 | inline constexpr bool ratio_not_equal_v = ratio_not_equal<_R1, _R2>::value; |
470 | template <typename _R1, typename _R2> |
471 | inline constexpr bool ratio_less_v = ratio_less<_R1, _R2>::value; |
472 | template <typename _R1, typename _R2> |
473 | inline constexpr bool ratio_less_equal_v |
474 | = ratio_less_equal<_R1, _R2>::value; |
475 | template <typename _R1, typename _R2> |
476 | inline constexpr bool ratio_greater_v = ratio_greater<_R1, _R2>::value; |
477 | template <typename _R1, typename _R2> |
478 | inline constexpr bool ratio_greater_equal_v |
479 | = ratio_greater_equal<_R1, _R2>::value; |
480 | #endif // C++17 |
481 | |
482 | /// @cond undocumented |
483 | |
484 | template<typename _R1, typename _R2, |
485 | bool = (_R1::num >= 0), |
486 | bool = (_R2::num >= 0), |
487 | bool = ratio_less<ratio<__static_abs<_R1::num>::value, _R1::den>, |
488 | ratio<__static_abs<_R2::num>::value, _R2::den> >::value> |
489 | struct __ratio_add_impl |
490 | { |
491 | private: |
492 | typedef typename __ratio_add_impl< |
493 | ratio<-_R1::num, _R1::den>, |
494 | ratio<-_R2::num, _R2::den> >::type __t; |
495 | public: |
496 | typedef ratio<-__t::num, __t::den> type; |
497 | }; |
498 | |
499 | // True addition of nonnegative numbers. |
500 | template<typename _R1, typename _R2, bool __b> |
501 | struct __ratio_add_impl<_R1, _R2, true, true, __b> |
502 | { |
503 | private: |
504 | static constexpr uintmax_t __g = __static_gcd<_R1::den, _R2::den>::value; |
505 | static constexpr uintmax_t __d2 = _R2::den / __g; |
506 | typedef __big_mul<_R1::den, __d2> __d; |
507 | typedef __big_mul<_R1::num, _R2::den / __g> __x; |
508 | typedef __big_mul<_R2::num, _R1::den / __g> __y; |
509 | typedef __big_add<__x::__hi, __x::__lo, __y::__hi, __y::__lo> __n; |
510 | static_assert(__n::__hi >= __x::__hi, "Internal library error" ); |
511 | typedef __big_div<__n::__hi, __n::__lo, __g> __ng; |
512 | static constexpr uintmax_t __g2 = __static_gcd<__ng::__rem, __g>::value; |
513 | typedef __big_div<__n::__hi, __n::__lo, __g2> __n_final; |
514 | static_assert(__n_final::__rem == 0, "Internal library error" ); |
515 | static_assert(__n_final::__quot_hi == 0 && |
516 | __n_final::__quot_lo <= __INTMAX_MAX__, "overflow in addition" ); |
517 | typedef __big_mul<_R1::den / __g2, __d2> __d_final; |
518 | static_assert(__d_final::__hi == 0 && |
519 | __d_final::__lo <= __INTMAX_MAX__, "overflow in addition" ); |
520 | public: |
521 | typedef ratio<__n_final::__quot_lo, __d_final::__lo> type; |
522 | }; |
523 | |
524 | template<typename _R1, typename _R2> |
525 | struct __ratio_add_impl<_R1, _R2, false, true, true> |
526 | : __ratio_add_impl<_R2, _R1> |
527 | { }; |
528 | |
529 | // True subtraction of nonnegative numbers yielding a nonnegative result. |
530 | template<typename _R1, typename _R2> |
531 | struct __ratio_add_impl<_R1, _R2, true, false, false> |
532 | { |
533 | private: |
534 | static constexpr uintmax_t __g = __static_gcd<_R1::den, _R2::den>::value; |
535 | static constexpr uintmax_t __d2 = _R2::den / __g; |
536 | typedef __big_mul<_R1::den, __d2> __d; |
537 | typedef __big_mul<_R1::num, _R2::den / __g> __x; |
538 | typedef __big_mul<-_R2::num, _R1::den / __g> __y; |
539 | typedef __big_sub<__x::__hi, __x::__lo, __y::__hi, __y::__lo> __n; |
540 | typedef __big_div<__n::__hi, __n::__lo, __g> __ng; |
541 | static constexpr uintmax_t __g2 = __static_gcd<__ng::__rem, __g>::value; |
542 | typedef __big_div<__n::__hi, __n::__lo, __g2> __n_final; |
543 | static_assert(__n_final::__rem == 0, "Internal library error" ); |
544 | static_assert(__n_final::__quot_hi == 0 && |
545 | __n_final::__quot_lo <= __INTMAX_MAX__, "overflow in addition" ); |
546 | typedef __big_mul<_R1::den / __g2, __d2> __d_final; |
547 | static_assert(__d_final::__hi == 0 && |
548 | __d_final::__lo <= __INTMAX_MAX__, "overflow in addition" ); |
549 | public: |
550 | typedef ratio<__n_final::__quot_lo, __d_final::__lo> type; |
551 | }; |
552 | |
553 | template<typename _R1, typename _R2> |
554 | struct __ratio_add |
555 | { |
556 | static_assert(std::__are_both_ratios<_R1, _R2>(), |
557 | "both template arguments must be a std::ratio" ); |
558 | |
559 | typedef typename __ratio_add_impl<_R1, _R2>::type type; |
560 | static constexpr intmax_t num = type::num; |
561 | static constexpr intmax_t den = type::den; |
562 | }; |
563 | |
564 | #if ! __cpp_inline_variables |
565 | template<typename _R1, typename _R2> |
566 | constexpr intmax_t __ratio_add<_R1, _R2>::num; |
567 | |
568 | template<typename _R1, typename _R2> |
569 | constexpr intmax_t __ratio_add<_R1, _R2>::den; |
570 | #endif |
571 | |
572 | /// @endcond |
573 | |
574 | /// ratio_add |
575 | template<typename _R1, typename _R2> |
576 | using ratio_add = typename __ratio_add<_R1, _R2>::type; |
577 | |
578 | /// @cond undocumented |
579 | |
580 | template<typename _R1, typename _R2> |
581 | struct __ratio_subtract |
582 | { |
583 | typedef typename __ratio_add< |
584 | _R1, |
585 | ratio<-_R2::num, _R2::den>>::type type; |
586 | |
587 | static constexpr intmax_t num = type::num; |
588 | static constexpr intmax_t den = type::den; |
589 | }; |
590 | |
591 | #if ! __cpp_inline_variables |
592 | template<typename _R1, typename _R2> |
593 | constexpr intmax_t __ratio_subtract<_R1, _R2>::num; |
594 | |
595 | template<typename _R1, typename _R2> |
596 | constexpr intmax_t __ratio_subtract<_R1, _R2>::den; |
597 | #endif |
598 | |
599 | /// @endcond |
600 | |
601 | /// ratio_subtract |
602 | template<typename _R1, typename _R2> |
603 | using ratio_subtract = typename __ratio_subtract<_R1, _R2>::type; |
604 | |
605 | |
606 | typedef ratio<1, 1000000000000000000> atto; |
607 | typedef ratio<1, 1000000000000000> femto; |
608 | typedef ratio<1, 1000000000000> pico; |
609 | typedef ratio<1, 1000000000> nano; |
610 | typedef ratio<1, 1000000> micro; |
611 | typedef ratio<1, 1000> milli; |
612 | typedef ratio<1, 100> centi; |
613 | typedef ratio<1, 10> deci; |
614 | typedef ratio< 10, 1> deca; |
615 | typedef ratio< 100, 1> hecto; |
616 | typedef ratio< 1000, 1> kilo; |
617 | typedef ratio< 1000000, 1> mega; |
618 | typedef ratio< 1000000000, 1> giga; |
619 | typedef ratio< 1000000000000, 1> tera; |
620 | typedef ratio< 1000000000000000, 1> peta; |
621 | typedef ratio< 1000000000000000000, 1> exa; |
622 | |
623 | /// @} group ratio |
624 | _GLIBCXX_END_NAMESPACE_VERSION |
625 | } // namespace |
626 | |
627 | #endif // C++11 |
628 | |
629 | #endif //_GLIBCXX_RATIO |
630 | |