OpenShot Library | libopenshot 0.2.7
Fraction.cpp
Go to the documentation of this file.
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
34using namespace openshot;
35
36// Delegating constructors
37Fraction::Fraction() : Fraction::Fraction(1, 1) {};
38
39Fraction::Fraction(std::pair<int, int> pair)
40 : Fraction::Fraction(pair.first, pair.second) {};
41
42Fraction::Fraction(std::map<std::string, int> mapping)
43 : Fraction::Fraction(mapping["num"], mapping["den"]) {};
44
45Fraction::Fraction(std::vector<int> vector)
46 : Fraction::Fraction(vector[0], vector[1]) {};
47
48// Full constructor
49Fraction::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)
55 return float(num) / float(den);
56}
57
58// Return this fraction as a double (i.e. 1/2 = 0.5)
59double 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)
65 return round((double) num / den);
66}
67
68// Calculate the greatest common denominator
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
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
95{
96 // flip the fraction
97 return Fraction(den, num);
98}
Header file for Fraction class.
This class represents a fraction.
Definition: Fraction.h:48
int num
Numerator for the fraction.
Definition: Fraction.h:50
Fraction()
Default Constructor.
Definition: Fraction.cpp:37
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:54
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:59
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:84
int ToInt()
Return a rounded integer of the fraction (for example 30000/1001 returns 30)
Definition: Fraction.cpp:64
Fraction Reciprocal() const
Return the reciprocal as a Fraction.
Definition: Fraction.cpp:94
int GreatestCommonDenominator()
Calculate the greatest common denominator.
Definition: Fraction.cpp:69
int den
Denominator for the fraction.
Definition: Fraction.h:51
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47