OpenShot Library | libopenshot  0.2.5
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 "../include/Fraction.h"
32 
33 using namespace openshot;
34 
35 // Constructor
37  num(1), den(1) {
38 }
39 Fraction::Fraction(int num, int den) :
40  num(num), den(den) {
41 }
42 
43 // Return this fraction as a float (i.e. 1/2 = 0.5)
45  return float(num) / float(den);
46 }
47 
48 // Return this fraction as a double (i.e. 1/2 = 0.5)
50  return double(num) / double(den);
51 }
52 
53 // Return a rounded integer of the frame rate (for example 30000/1001 returns 30 fps)
55  return round((double) num / den);
56 }
57 
58 // Calculate the greatest common denominator
60  int first = num;
61  int second = den;
62 
63  // Find the biggest whole number that will divide into both the numerator
64  // and denominator
65  int t;
66  while (second != 0) {
67  t = second;
68  second = first % second;
69  first = t;
70  }
71  return first;
72 }
73 
75  // Get the greatest common denominator
76  int GCD = GreatestCommonDenominator();
77 
78  // Reduce this fraction to the smallest possible whole numbers
79  num = num / GCD;
80  den = den / GCD;
81 }
82 
83 // Return the reciprocal as a new Fraction
85 {
86  // flip the fraction
87  return Fraction(den, num);
88 }
openshot::Fraction::ToFloat
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:44
openshot::Fraction::ToInt
int ToInt()
Return a rounded integer of the fraction (for example 30000/1001 returns 30)
Definition: Fraction.cpp:54
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:38
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:45
openshot::Fraction::Reciprocal
Fraction Reciprocal()
Return the reciprocal as a Fraction.
Definition: Fraction.cpp:84
openshot::Fraction::GreatestCommonDenominator
int GreatestCommonDenominator()
Calculate the greatest common denominator.
Definition: Fraction.cpp:59
openshot::Fraction::Fraction
Fraction()
Default Constructor.
Definition: Fraction.cpp:36
openshot::Fraction::num
int num
Numerator for the fraction.
Definition: Fraction.h:47
openshot::Fraction::den
int den
Denominator for the fraction.
Definition: Fraction.h:48
openshot::Fraction::Reduce
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:74
openshot::Fraction::ToDouble
double ToDouble()
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:49