OpenShot Library | libopenshot 0.3.3
Loading...
Searching...
No Matches
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
21namespace openshot {
22
30class Fraction {
31public:
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
94template<class numT>
95numT operator*(const numT& left, const openshot::Fraction& right) {
96 return static_cast<numT>(left * right.ToDouble());
97}
98
100template<class numT>
101numT 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
106template<class charT, class traits>
107std::basic_ostream<charT, traits>&
108operator<<(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
This class represents a fraction.
Definition Fraction.h:30
int num
Numerator for the fraction.
Definition Fraction.h:32
openshot::Fraction operator*(openshot::Fraction other)
Multiply two Fraction objects together.
Definition Fraction.h:71
Fraction()
Default Constructor.
Definition Fraction.cpp:19
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5)
Definition Fraction.cpp:35
numT operator*(const numT &other) const
Multiplication in the form (openshot_Fraction * numeric_value)
Definition Fraction.h:82
openshot::Fraction operator/(openshot::Fraction other)
Divide a Fraction by another Fraction.
Definition Fraction.h:76
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition Fraction.cpp:40
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition Fraction.cpp:65
int ToInt()
Return a rounded integer of the fraction (for example 30000/1001 returns 30)
Definition Fraction.cpp:45
Fraction Reciprocal() const
Return the reciprocal as a Fraction.
Definition Fraction.cpp:78
int GreatestCommonDenominator()
Calculate the greatest common denominator.
Definition Fraction.cpp:50
numT operator/(const numT &other) const
Division in the form (openshot_Fraction / numeric_value)
Definition Fraction.h:88
int den
Denominator for the fraction.
Definition Fraction.h:33
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
numT operator*(const numT &left, const openshot::Fraction &right)
Multiplication in the form (numeric_value * openshot_Fraction)
Definition Fraction.h:95
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
numT operator/(const numT &left, const openshot::Fraction &right)
Division in the form (numeric_value / openshot_Fraction)
Definition Fraction.h:101