summaryrefslogtreecommitdiff
path: root/libstdc++-v3/include/std/ratio
blob: 17d28a10f465b36214d08c334a16886adcb6e08b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// ratio -*- C++ -*-

// Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the 
// terms of the GNU General Public License as published by the 
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/** @file include/ratio
 *  This is a Standard C++ Library header.
 */

#ifndef _GLIBCXX_RATIO
#define _GLIBCXX_RATIO 1

#pragma GCC system_header

#ifndef __GXX_EXPERIMENTAL_CXX0X__
# include <bits/c++0x_warning.h>
#else

#include <type_traits>
#include <cstdint>

#ifdef _GLIBCXX_USE_C99_STDINT_TR1

namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION

  /**
   * @defgroup ratio Rational Arithmetic
   * @ingroup utilities
   *
   * Compile time representation of finite rational numbers.
   * @{
   */

  template<intmax_t _Pn>
    struct __static_sign
    : integral_constant<intmax_t, (_Pn < 0) ? -1 : 1>
    { };

  template<intmax_t _Pn>
    struct __static_abs
    : integral_constant<intmax_t, _Pn * __static_sign<_Pn>::value>
    { };

  template<intmax_t _Pn, intmax_t _Qn>
    struct __static_gcd;
 
  template<intmax_t _Pn, intmax_t _Qn>
    struct __static_gcd
    : __static_gcd<_Qn, (_Pn % _Qn)>
    { };

  template<intmax_t _Pn>
    struct __static_gcd<_Pn, 0>
    : integral_constant<intmax_t, __static_abs<_Pn>::value>
    { };

  template<intmax_t _Qn>
    struct __static_gcd<0, _Qn>
    : integral_constant<intmax_t, __static_abs<_Qn>::value>
    { };

  // Let c = 2^(half # of bits in an intmax_t)
  // then we find a1, a0, b1, b0 s.t. N = a1*c + a0, M = b1*c + b0
  // The multiplication of N and M becomes,
  // N * M = (a1 * b1)c^2 + (a0 * b1 + b0 * a1)c + a0 * b0
  // Multiplication is safe if each term and the sum of the terms
  // is representable by intmax_t.
  template<intmax_t _Pn, intmax_t _Qn>
    struct __safe_multiply
    {
    private:
      static const uintmax_t __c = uintmax_t(1) << (sizeof(intmax_t) * 4);

      static const uintmax_t __a0 = __static_abs<_Pn>::value % __c;
      static const uintmax_t __a1 = __static_abs<_Pn>::value / __c;
      static const uintmax_t __b0 = __static_abs<_Qn>::value % __c;
      static const uintmax_t __b1 = __static_abs<_Qn>::value / __c;

      static_assert(__a1 == 0 || __b1 == 0, 
        "overflow in multiplication");
      static_assert(__a0 * __b1 + __b0 * __a1 < (__c >> 1), 
        "overflow in multiplication");
      static_assert(__b0 * __a0 <= __INTMAX_MAX__, 
        "overflow in multiplication");
      static_assert((__a0 * __b1 + __b0 * __a1) * __c <= 
        __INTMAX_MAX__ -  __b0 * __a0, "overflow in multiplication");

    public:
      static const intmax_t value = _Pn * _Qn;
    };

  // Helpers for __safe_add
  template<intmax_t _Pn, intmax_t _Qn, bool>
    struct __add_overflow_check_impl
    : integral_constant<bool, (_Pn <= __INTMAX_MAX__ - _Qn)>
    { };

  template<intmax_t _Pn, intmax_t _Qn>
    struct __add_overflow_check_impl<_Pn, _Qn, false>
    : integral_constant<bool, (_Pn >= -__INTMAX_MAX__ - _Qn)>
    { };

  template<intmax_t _Pn, intmax_t _Qn>
    struct __add_overflow_check
    : __add_overflow_check_impl<_Pn, _Qn, (_Qn >= 0)>
    { };

  template<intmax_t _Pn, intmax_t _Qn>
    struct __safe_add
    {
      static_assert(__add_overflow_check<_Pn, _Qn>::value != 0, 
        "overflow in addition");

      static const intmax_t value = _Pn + _Qn;
    };

  /**
   *  @brief Provides compile-time rational arithmetic.
   *
   *  This class template represents any finite rational number with a
   *  numerator and denominator representable by compile-time constants of
   *  type intmax_t. The ratio is simplified when instantiated.
   *
   *  For example:
   *  @code
   *    std::ratio<7,-21>::num == -1;
   *    std::ratio<7,-21>::den == 3;
   *  @endcode
   *  
  */
  template<intmax_t _Num, intmax_t _Den = 1>
    struct ratio
    {
      static_assert(_Den != 0, "denominator cannot be zero");
      static_assert(_Num >= -__INTMAX_MAX__ && _Den >= -__INTMAX_MAX__,
		    "out of range");

      // Note: sign(N) * abs(N) == N
      static constexpr intmax_t num =
        _Num * __static_sign<_Den>::value / __static_gcd<_Num, _Den>::value;

      static constexpr intmax_t den =
        __static_abs<_Den>::value / __static_gcd<_Num, _Den>::value;

      typedef ratio<num, den> type;
    };

  template<intmax_t _Num, intmax_t _Den>
    constexpr intmax_t ratio<_Num, _Den>::num;

  template<intmax_t _Num, intmax_t _Den>
    constexpr intmax_t ratio<_Num, _Den>::den;

  /// ratio_add
  template<typename _R1, typename _R2>
    struct ratio_add
    {
    private:
      static constexpr intmax_t __gcd =
        __static_gcd<_R1::den, _R2::den>::value;
      static constexpr intmax_t __n = __safe_add<
        __safe_multiply<_R1::num, (_R2::den / __gcd)>::value,
        __safe_multiply<_R2::num, (_R1::den / __gcd)>::value>::value;

      // The new numerator may have common factors with the denominator,
      // but they have to also be factors of __gcd.
      static constexpr intmax_t __gcd2 = __static_gcd<__n, __gcd>::value;
      
    public:
      typedef ratio<__n / __gcd2,
        __safe_multiply<_R1::den / __gcd2, _R2::den / __gcd>::value> type;

      static constexpr intmax_t num = type::num;
      static constexpr intmax_t den = type::den;
    };

  template<typename _R1, typename _R2>
    constexpr intmax_t ratio_add<_R1, _R2>::num;

  template<typename _R1, typename _R2>
    constexpr intmax_t ratio_add<_R1, _R2>::den;

  /// ratio_subtract
  template<typename _R1, typename _R2>
    struct ratio_subtract
    {
      typedef typename ratio_add<
        _R1,
        ratio<-_R2::num, _R2::den>>::type type;

      static constexpr intmax_t num = type::num;
      static constexpr intmax_t den = type::den;
    };

  template<typename _R1, typename _R2>
    constexpr intmax_t ratio_subtract<_R1, _R2>::num;

  template<typename _R1, typename _R2>
    constexpr intmax_t ratio_subtract<_R1, _R2>::den;

  /// ratio_multiply
  template<typename _R1, typename _R2>
    struct ratio_multiply
    {
    private:
      static const intmax_t __gcd1 =
        __static_gcd<_R1::num, _R2::den>::value;
      static const intmax_t __gcd2 =
        __static_gcd<_R2::num, _R1::den>::value;

    public:
      typedef ratio<
        __safe_multiply<(_R1::num / __gcd1),
                        (_R2::num / __gcd2)>::value,
        __safe_multiply<(_R1::den / __gcd2),
                        (_R2::den / __gcd1)>::value> type;

      static constexpr intmax_t num = type::num;
      static constexpr intmax_t den = type::den;
    };

  template<typename _R1, typename _R2>
    constexpr intmax_t ratio_multiply<_R1, _R2>::num;

  template<typename _R1, typename _R2>
    constexpr intmax_t ratio_multiply<_R1, _R2>::den;

  /// ratio_divide
  template<typename _R1, typename _R2>
    struct ratio_divide
    {
      static_assert(_R2::num != 0, "division by 0");

      typedef typename ratio_multiply<
        _R1,
        ratio<_R2::den, _R2::num>>::type type;

      static constexpr intmax_t num = type::num;
      static constexpr intmax_t den = type::den;
    };

  template<typename _R1, typename _R2>
    constexpr intmax_t ratio_divide<_R1, _R2>::num;

  template<typename _R1, typename _R2>
    constexpr intmax_t ratio_divide<_R1, _R2>::den;

  /// ratio_equal
  template<typename _R1, typename _R2>
    struct ratio_equal
    : integral_constant<bool, _R1::num == _R2::num && _R1::den == _R2::den>
    { };
  
  /// ratio_not_equal
  template<typename _R1, typename _R2>
    struct ratio_not_equal
    : integral_constant<bool, !ratio_equal<_R1, _R2>::value>
    { };

  // 0 <= _Ri < 1
  // If one is 0, conclude
  // Otherwise, x < y iff 1/y < 1/x
  template<typename _R1, typename _R2>
    struct __ratio_less_impl_2;

  // _Ri > 0
  // Compare the integral parts, and remove them if they are equal
  template<typename _R1, typename _R2, intmax_t __q1 = _R1::num / _R1::den,
           intmax_t __q2 = _R2::num / _R2::den, bool __eq = (__q1 == __q2)>
    struct __ratio_less_impl_1
    : __ratio_less_impl_2<ratio<_R1::num % _R1::den, _R1::den>,
           ratio<_R2::num % _R2::den, _R2::den> >::type
    { }; 

  template<typename _R1, typename _R2, intmax_t __q1, intmax_t __q2>
    struct __ratio_less_impl_1<_R1, _R2, __q1, __q2, false>
    : integral_constant<bool, (__q1 < __q2) >
    { };

  template<typename _R1, typename _R2>
    struct __ratio_less_impl_2
    : __ratio_less_impl_1<ratio<_R2::den, _R2::num>,
           ratio<_R1::den, _R1::num> >::type
    { }; 

  template<intmax_t __d1, typename _R2>
    struct __ratio_less_impl_2<ratio<0, __d1>, _R2>
    : integral_constant<bool, true>
    { }; 

  template<typename _R1, intmax_t __d2>
    struct __ratio_less_impl_2<_R1, ratio<0, __d2> >
    : integral_constant<bool, false>
    { }; 

  template<intmax_t __d1, intmax_t __d2>
    struct __ratio_less_impl_2<ratio<0, __d1>, ratio<0, __d2> >
    : integral_constant<bool, false>
    { }; 

  template<typename _R1, typename _R2,
	   bool = (_R1::num == 0 || _R2::num == 0
		   || (__static_sign<_R1::num>::value
		       != __static_sign<_R2::num>::value)),
	   bool = (__static_sign<_R1::num>::value == -1
		   && __static_sign<_R2::num>::value == -1)>
    struct __ratio_less_impl
    : __ratio_less_impl_1<_R1, _R2>::type
    { };

  template<typename _R1, typename _R2>
    struct __ratio_less_impl<_R1, _R2, true, false>
    : integral_constant<bool, _R1::num < _R2::num>
    { };

  template<typename _R1, typename _R2>
    struct __ratio_less_impl<_R1, _R2, false, true>
    : __ratio_less_impl_1<ratio<-_R2::num, _R2::den>,
           ratio<-_R1::num, _R1::den> >::type
    { };

  /// ratio_less
  // using a continued fraction expansion
  template<typename _R1, typename _R2>
    struct ratio_less
    : __ratio_less_impl<_R1, _R2>::type
    { };
    
  /// ratio_less_equal
  template<typename _R1, typename _R2>
    struct ratio_less_equal
    : integral_constant<bool, !ratio_less<_R2, _R1>::value>
    { };
  
  /// ratio_greater
  template<typename _R1, typename _R2>
    struct ratio_greater
    : integral_constant<bool, ratio_less<_R2, _R1>::value>
    { };

  /// ratio_greater_equal
  template<typename _R1, typename _R2>
    struct ratio_greater_equal
    : integral_constant<bool, !ratio_less<_R1, _R2>::value>
    { };

  typedef ratio<1,       1000000000000000000> atto;
  typedef ratio<1,          1000000000000000> femto;
  typedef ratio<1,             1000000000000> pico;
  typedef ratio<1,                1000000000> nano;
  typedef ratio<1,                   1000000> micro;
  typedef ratio<1,                      1000> milli;
  typedef ratio<1,                       100> centi;
  typedef ratio<1,                        10> deci;
  typedef ratio<                       10, 1> deca;
  typedef ratio<                      100, 1> hecto;
  typedef ratio<                     1000, 1> kilo;
  typedef ratio<                  1000000, 1> mega;
  typedef ratio<               1000000000, 1> giga;
  typedef ratio<            1000000000000, 1> tera;
  typedef ratio<         1000000000000000, 1> peta;
  typedef ratio<      1000000000000000000, 1> exa;

  // @} group ratio
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace

#endif //_GLIBCXX_USE_C99_STDINT_TR1

#endif //__GXX_EXPERIMENTAL_CXX0X__

#endif //_GLIBCXX_RATIO