File: rational.cpp

package info (click to toggle)
olive-editor 20200620-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 40,228 kB
  • sloc: cpp: 51,932; sh: 56; makefile: 7; xml: 7
file content (395 lines) | stat: -rw-r--r-- 7,099 bytes parent folder | download
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
394
395
//Copyright 2015 Adam Quintero
//This program is distributed under the terms of the GNU General Public License.

#include "rational.h"

OLIVE_NAMESPACE_ENTER

rational rational::fromDouble(const double &flt)
{
  // Use FFmpeg function for the time being
  return av_d2q(flt, INT_MAX);
}

rational rational::fromString(const QString &str)
{
  QStringList elements = str.split('/');

  switch (elements.size()) {
  case 0:
    return rational();
  case 1:
    return rational(elements.first().toLongLong());
  default:
    return rational(elements.at(0).toLongLong(), elements.at(1).toLongLong());
  }
}

//Function: print number to cout

void rational::print(std::ostream &out) const
{
  out << this->numer_ << "/" << this->denom_;
}

//Function: ensures denom >= 0

void rational::fix_signs()
{
  if (denom_ < 0) {
    denom_ = -denom_;
    numer_ = -numer_;
  }

  if (numer_ == intType(0) || denom_ == intType(0)) {
    numer_ = intType(0);
    denom_ = intType(0);
  }
}

//Function: ensures lowest form

void rational::reduce()
{
  if (!isNull()) {
    // Euclidean often fails if numbers are negative, we abs it and re-neg it later if necessary
    bool neg = numer_ < 0;

    numer_ = qAbs(numer_);

    intType d = gcd(numer_, denom_);

    if (d > 1) {
      numer_ /= d;
      denom_ /= d;
    }

    if (neg) {
      numer_ = -numer_;
    }
  }
}

//Function: finds greatest common denominator

intType rational::gcd(const intType &x, const intType &y)
{
  if (y == 0) {
    return x;
  } else {
    return gcd(y, x % y);
  }
}

//Function: convert to double

double rational::toDouble() const
{
  if (denom_ != 0) {
    return static_cast<double>(numer_) / static_cast<double>(denom_);
  } else {
    return static_cast<double>(0);
  }
}

AVRational rational::toAVRational() const
{
  AVRational r;

  r.num = static_cast<int>(numer_);
  r.den = static_cast<int>(denom_);

  return r;
}

rational rational::flipped() const
{
  return rational(denom_, numer_);
}

bool rational::isNull() const
{
  return denominator() == 0;
}

const intType &rational::numerator() const
{
  return numer_;
}

const intType &rational::denominator() const
{
  return denom_;
}

QString rational::toString() const
{
  return QStringLiteral("%1/%2").arg(QString::number(numer_), QString::number(denom_));
}

//Assignment Operators

const rational& rational::operator=(const rational &rhs)
{
  if (this != &rhs) {
    numer_ = rhs.numer_;
    denom_ = rhs.denom_;
  }

  return *this;
}

const rational& rational::operator+=(const rational &rhs)
{
  if (!rhs.isNull()) {
    if (isNull()) {
      numer_ = rhs.numer_;
      denom_ = rhs.denom_;
    } else {
      numer_ = (numer_ * rhs.denom_) + (rhs.numer_ * denom_);
      denom_ = denom_ * rhs.denom_;
      fix_signs();
      reduce();
    }
  }

  return *this;
}

const rational& rational::operator-=(const rational &rhs)
{
  if (!rhs.isNull()) {
    if (isNull()) {
      numer_ = -rhs.numer_;
      denom_ = rhs.denom_;
    } else {
      numer_ = (numer_ * rhs.denom_) - (rhs.numer_ * denom_);
      denom_ = denom_ * rhs.denom_;
      fix_signs();
      reduce();
    }
  }

  return *this;
}

const rational& rational::operator/=(const rational &rhs)
{
  numer_ = numer_ * rhs.denom_;
  denom_ = denom_ * rhs.numer_;
  fix_signs();
  reduce();
  return *this;
}

const rational& rational::operator*=(const rational &rhs)
{
  numer_ = numer_ * rhs.numer_;
  denom_ = denom_ * rhs.denom_;
  fix_signs();
  reduce();
  return *this;
}

//Binary math operators

rational rational::operator+(const rational &rhs) const
{
  rational answer(*this);
  answer += rhs;
  return answer;
}

rational rational::operator-(const rational &rhs) const
{
  rational answer(*this);
  answer -= rhs;
  return answer;
}

rational rational::operator/(const rational &rhs) const
{
  rational answer(*this);
  answer /= rhs;
  return answer;
}

rational rational::operator*(const rational &rhs) const
{
  rational answer(*this);
  answer *= rhs;
  return answer;
}

//Relational and equality operators

bool rational::operator<(const rational &rhs) const
{
  if (isNull() && rhs.isNull()) {
    return false;
  }

  if (rhs == RATIONAL_MAX
      || *this == RATIONAL_MIN) {
    // We will always wither be LESS THAN (true) or EQUAL (false)
    return (*this != rhs);
  }

  if (*this == RATIONAL_MAX
      || rhs == RATIONAL_MIN) {
    // We will always be GREATER THAN (false) or EQUAL (false)
    return false;
  }

  if (!isNull() && rhs.isNull()) {
    return (numer_ * denom_ < intType(0));
  }

  if (isNull() && !rhs.isNull()) {
    return !(rhs.numer_ * rhs.denom_ < intType(0));
  }

  return ((numer_ * rhs.denom_) < (denom_ * rhs.numer_));
}

bool rational::operator<=(const rational &rhs) const
{
  if (isNull() && rhs.isNull()) {
    return true;
  }

  if (rhs == RATIONAL_MAX
      || *this == RATIONAL_MIN) {
    // We will always wither be LESS THAN (true) or EQUAL (true)
    return true;
  }

  if (*this == RATIONAL_MAX
      || rhs == RATIONAL_MIN) {
    // We will always be GREATER THAN (false) or EQUAL (true)
    return rhs == *this;
  }

  if (!isNull() && rhs.isNull()) {
    return (numer_ * denom_ < intType(0));
  }

  if (isNull() && !rhs.isNull()) {
    return !(rhs.numer_ * rhs.denom_ < intType(0));
  }

  return ((numer_ * rhs.denom_) <= (denom_ * rhs.numer_));
}

bool rational::operator>(const rational &rhs) const
{
  return rhs < *this;
}

bool rational::operator>=(const rational &rhs) const
{
  return rhs <= *this;
}

bool rational::operator==(const rational &rhs) const
{
  return (numer_ == rhs.numer_ && denom_ == rhs.denom_);
}

bool rational::operator!=(const rational &rhs) const
{
  return (numer_ != rhs.numer_) || (denom_ != rhs.denom_);
}

//Unary operators

const rational& rational::operator++()
{
  numer_ += denom_;
  return *this;
}

rational rational::operator++(int)
{
  rational tmp = *this;
  numer_ += denom_;
  return tmp;
}

const rational& rational::operator--()
{
  numer_ -= denom_;
  return *this;
}

rational rational::operator--(int)
{
  rational tmp;
  numer_ -= denom_;
  return tmp;
}

const rational& rational::operator+() const
{
  return *this;
}

rational rational::operator-() const
{
  return rational(numer_, -denom_);
}

bool rational::operator!() const
{
  return !numer_;
}

//IO

std::ostream& operator<<(std::ostream &out, const rational &value)
{
  out << value.numer_;

  if (value.denom_ != 1) {
    out << '/' << value.denom_;
    return out;
  }

  return out;
}

std::istream& operator>>(std::istream &in, rational &value)
{
  in >> value.numer_;
  value.denom_ = 1;

  char ch;
  in.get(ch);

  if(!in.eof()) {
    if(ch == '/') {
      in >> value.denom_;
      value.fix_signs();
      value.reduce();
    } else {
      in.putback(ch);
    }
  }
  return in;
}

uint qHash(const rational &r, uint seed)
{
  return ::qHash(r.toDouble(), seed);
}

OLIVE_NAMESPACE_EXIT

QDebug operator<<(QDebug debug, const OLIVE_NAMESPACE::rational &r)
{
  return debug.space() << r.toDouble();
  /*
  debug.nospace() << r.numerator() << "/" << r.denominator();
  return debug.space();
  */
}