OpenShot Library | libopenshot  0.4.0
Fraction.h
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #ifndef OPENSHOT_FRACTION_H
14 #define OPENSHOT_FRACTION_H
15 
16 #include <string> // for std::string
17 #include <utility> // for std::pair
18 #include <map> // for std::map
19 #include <vector> // for std::vector
20 
21 namespace openshot {
22 
30 class Fraction {
31 public:
32  int num;
33  int den;
34 
36  Fraction();
37 
39  Fraction(int num, int den);
40 
42  Fraction(std::pair<int, int> pair);
43 
45  Fraction(std::vector<int> vector);
46 
48  Fraction(std::map<std::string, int> mapping);
49 
52 
54  void Reduce();
55 
57  float ToFloat();
58 
60  double ToDouble() const;
61 
63  int ToInt();
64 
66  Fraction Reciprocal() const;
67 
68  // Multiplication and division
69 
72  return openshot::Fraction(num * other.num, den * other.den);
73  }
74 
77  return *this * other.Reciprocal();
78  }
79 
81  template<class numT>
82  numT operator*(const numT& other) const {
83  return static_cast<numT>(ToDouble() * other);
84  }
85 
87  template<class numT>
88  numT operator/(const numT& other) const {
89  return static_cast<numT>(ToDouble() / other);
90  }
91 };
92 
94 template<class numT>
95 numT operator*(const numT& left, const openshot::Fraction& right) {
96  return static_cast<numT>(left * right.ToDouble());
97 }
98 
100 template<class numT>
101 numT operator/(const numT& left, const openshot::Fraction& right) {
102  return static_cast<numT>(left / right.ToDouble());
103 }
104 
105 // Stream output operator for openshot::Fraction
106 template<class charT, class traits>
107 std::basic_ostream<charT, traits>&
108 operator<<(std::basic_ostream<charT, traits>& o, const openshot::Fraction& frac) {
109  std::basic_ostringstream<charT, traits> s;
110  s.flags(o.flags());
111  s.imbue(o.getloc());
112  s.precision(o.precision());
113  s << "Fraction(" << frac.num << ", " << frac.den << ")";
114  return o << s.str();
115 }
116 
117 } // namespace openshot
118 #endif
openshot::Fraction::ToFloat
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:35
openshot::Fraction::operator/
openshot::Fraction operator/(openshot::Fraction other)
Divide a Fraction by another Fraction.
Definition: Fraction.h:76
openshot::Fraction::ToInt
int ToInt()
Return a rounded integer of the fraction (for example 30000/1001 returns 30)
Definition: Fraction.cpp:45
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:30
openshot::Fraction::GreatestCommonDenominator
int GreatestCommonDenominator()
Calculate the greatest common denominator.
Definition: Fraction.cpp:50
openshot::operator*
numT operator*(const numT &left, const openshot::Fraction &right)
Multiplication in the form (numeric_value * openshot_Fraction)
Definition: Fraction.h:95
openshot::Fraction::ToDouble
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition: Fraction.cpp:40
openshot::Fraction::Fraction
Fraction()
Default Constructor.
Definition: Fraction.cpp:19
openshot::Fraction::num
int num
Numerator for the fraction.
Definition: Fraction.h:32
openshot::operator/
numT operator/(const numT &left, const openshot::Fraction &right)
Division in the form (numeric_value / openshot_Fraction)
Definition: Fraction.h:101
openshot::Fraction::den
int den
Denominator for the fraction.
Definition: Fraction.h:33
openshot::Fraction::Reduce
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition: Fraction.cpp:65
openshot::Fraction::Reciprocal
Fraction Reciprocal() const
Return the reciprocal as a Fraction.
Definition: Fraction.cpp:78
openshot::operator<<
std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &o, const openshot::Coordinate &co)
Stream output operator for openshot::Coordinate.
Definition: Coordinate.h:65
openshot::Fraction::operator/
numT operator/(const numT &other) const
Division in the form (openshot_Fraction / numeric_value)
Definition: Fraction.h:88
openshot::Fraction::operator*
numT operator*(const numT &other) const
Multiplication in the form (openshot_Fraction * numeric_value)
Definition: Fraction.h:82
openshot::Fraction::operator*
openshot::Fraction operator*(openshot::Fraction other)
Multiply two Fraction objects together.
Definition: Fraction.h:71