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