OpenShot Library | libopenshot  0.3.2
Fraction.cpp
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #include "Fraction.h"
14 #include <cmath>
15 
16 using namespace openshot;
17 
18 // Delegating constructors
20 
21 Fraction::Fraction(std::pair<int, int> pair)
22  : Fraction::Fraction(pair.first, pair.second) {}
23 
24 Fraction::Fraction(std::map<std::string, int> mapping)
25  : Fraction::Fraction(mapping["num"], mapping["den"]) {}
26 
27 Fraction::Fraction(std::vector<int> vector)
28  : Fraction::Fraction(vector[0], vector[1]) {}
29 
30 // Full constructor
31 Fraction::Fraction(int num, int den) :
32  num(num), den(den) {}
33 
34 // Return this fraction as a float (i.e. 1/2 = 0.5)
36  return float(num) / float(den);
37 }
38 
39 // Return this fraction as a double (i.e. 1/2 = 0.5)
40 double Fraction::ToDouble() const {
41  return double(num) / double(den);
42 }
43 
44 // Return a rounded integer of the frame rate (for example 30000/1001 returns 30 fps)
46  return round((double) num / den);
47 }
48 
49 // Calculate the greatest common denominator
51  int first = num;
52  int second = den;
53 
54  // Find the biggest whole number that will divide into both the numerator
55  // and denominator
56  int t;
57  while (second != 0) {
58  t = second;
59  second = first % second;
60  first = t;
61  }
62  return first;
63 }
64 
66  // Get the greatest common denominator
67  int GCD = GreatestCommonDenominator();
68  if (GCD == 0) {
69  return;
70  }
71 
72  // Reduce this fraction to the smallest possible whole numbers
73  num = num / GCD;
74  den = den / GCD;
75 }
76 
77 // Return the reciprocal as a new Fraction
79 {
80  // flip the fraction
81  return Fraction(den, num);
82 }
openshot::Fraction::ToFloat
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:35
Fraction.h
Header file for Fraction class.
openshot::Fraction::ToInt
int ToInt()
Return a rounded integer of the fraction (for example 30000/1001 returns 30)
Definition: Fraction.cpp:45
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:30
openshot::Fraction::GreatestCommonDenominator
int GreatestCommonDenominator()
Calculate the greatest common denominator.
Definition: Fraction.cpp:50
openshot::Fraction::ToDouble
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:40
openshot::Fraction::Fraction
Fraction()
Default Constructor.
Definition: Fraction.cpp:19
openshot::Fraction::num
int num
Numerator for the fraction.
Definition: Fraction.h:32
openshot::Fraction::den
int den
Denominator for the fraction.
Definition: Fraction.h:33
openshot::Fraction::Reduce
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:65
openshot::Fraction::Reciprocal
Fraction Reciprocal() const
Return the reciprocal as a Fraction.
Definition: Fraction.cpp:78