OpenShot Library | libopenshot  0.2.7
Fraction.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header 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 #ifndef OPENSHOT_FRACTION_H
32 #define OPENSHOT_FRACTION_H
33 
34 #include <string> // for std::string
35 #include <utility> // for std::pair
36 #include <map> // for std::map
37 #include <vector> // for std::vector
38 
39 namespace openshot {
40 
41  /**
42  * @brief This class represents a fraction
43  *
44  * Fractions are often used in video editing to represent ratios and rates, for example:
45  * pixel ratios, frames per second, timebase, and other common ratios. Fractions are preferred
46  * over decimals due to their increased precision.
47  */
48  class Fraction {
49  public:
50  int num; ///<Numerator for the fraction
51  int den; ///<Denominator for the fraction
52 
53  /// Default Constructor
54  Fraction();
55 
56  /// Constructor with numerator and denominator
57  Fraction(int num, int den);
58 
59  /// Constructor that accepts a (num, den) pair
60  Fraction(std::pair<int, int> pair);
61 
62  /// Constructor that takes a vector of length 2 (containing {num, den})
63  Fraction(std::vector<int> vector);
64 
65  /// Constructor that takes a key-value mapping (keys: 'num'. 'den')
66  Fraction(std::map<std::string, int> mapping);
67 
68  /// Calculate the greatest common denominator
70 
71  /// Reduce this fraction (i.e. 640/480 = 4/3)
72  void Reduce();
73 
74  /// Return this fraction as a float (i.e. 1/2 = 0.5)
75  float ToFloat();
76 
77  /// Return this fraction as a double (i.e. 1/2 = 0.5)
78  double ToDouble() const;
79 
80  /// Return a rounded integer of the fraction (for example 30000/1001 returns 30)
81  int ToInt();
82 
83  /// Return the reciprocal as a Fraction
84  Fraction Reciprocal() const;
85  };
86 
87 
88 }
89 
90 #endif
int num
Numerator for the fraction.
Definition: Fraction.h:50
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 GreatestCommonDenominator()
Calculate the greatest common denominator.
Definition: Fraction.cpp:69
This class represents a fraction.
Definition: Fraction.h:48
Fraction Reciprocal() const
Return the reciprocal as a Fraction.
Definition: Fraction.cpp:94
int ToInt()
Return a rounded integer of the fraction (for example 30000/1001 returns 30)
Definition: Fraction.cpp:64
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:46
Fraction()
Default Constructor.
Definition: Fraction.cpp:37
int den
Denominator for the fraction.
Definition: Fraction.h:51