1 /**
2  * @file
3  * @brief Source file for Fraction class
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC
12  * <http://www.openshotstudios.com/>. This file is part of
13  * OpenShot Library (libopenshot), an open-source project dedicated to
14  * delivering high quality video editing and animation solutions to the
15  * world. For more information visit <http://www.openshot.org/>.
16  *
17  * OpenShot Library (libopenshot) is free software: you can redistribute it
18  * and/or modify it under the terms of the GNU Lesser General Public License
19  * as published by the Free Software Foundation, either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * OpenShot Library (libopenshot) is distributed in the hope that it will be
23  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29  */
30 
31 #include "Fraction.h"
32 #include <cmath>
33 
34 using namespace openshot;
35 
36 // Delegating constructors
Fraction()37 Fraction::Fraction() : Fraction::Fraction(1, 1) {};
38 
Fraction(std::pair<int,int> pair)39 Fraction::Fraction(std::pair<int, int> pair)
40 	: Fraction::Fraction(pair.first, pair.second) {};
41 
Fraction(std::map<std::string,int> mapping)42 Fraction::Fraction(std::map<std::string, int> mapping)
43 	: Fraction::Fraction(mapping["num"], mapping["den"]) {};
44 
Fraction(std::vector<int> vector)45 Fraction::Fraction(std::vector<int> vector)
46 	: Fraction::Fraction(vector[0], vector[1]) {};
47 
48 // Full constructor
Fraction(int num,int den)49 Fraction::Fraction(int num, int den) :
50 	num(num), den(den) {
51 }
52 
53 // Return this fraction as a float (i.e. 1/2 = 0.5)
ToFloat()54 float Fraction::ToFloat() {
55 	return float(num) / float(den);
56 }
57 
58 // Return this fraction as a double (i.e. 1/2 = 0.5)
ToDouble() const59 double Fraction::ToDouble() const {
60 	return double(num) / double(den);
61 }
62 
63 // Return a rounded integer of the frame rate (for example 30000/1001 returns 30 fps)
ToInt()64 int Fraction::ToInt() {
65 	return round((double) num / den);
66 }
67 
68 // Calculate the greatest common denominator
GreatestCommonDenominator()69 int Fraction::GreatestCommonDenominator() {
70 	int first = num;
71 	int second = den;
72 
73 	// Find the biggest whole number that will divide into both the numerator
74 	// and denominator
75 	int t;
76 	while (second != 0) {
77 		t = second;
78 		second = first % second;
79 		first = t;
80 	}
81 	return first;
82 }
83 
Reduce()84 void Fraction::Reduce() {
85 	// Get the greatest common denominator
86 	int GCD = GreatestCommonDenominator();
87 
88 	// Reduce this fraction to the smallest possible whole numbers
89 	num = num / GCD;
90 	den = den / GCD;
91 }
92 
93 // Return the reciprocal as a new Fraction
Reciprocal() const94 Fraction Fraction::Reciprocal() const
95 {
96 	// flip the fraction
97 	return Fraction(den, num);
98 }
99