Loading...
Searching...
No Matches
Fraction.h
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#ifndef OPENSHOT_FRACTION_H
#define OPENSHOT_FRACTION_H
#include <string> // for std::string
#include <utility> // for std::pair
#include <map> // for std::map
#include <vector> // for std::vector
namespace openshot {
class Fraction {
public:
int num;
int den;
Fraction();
Fraction(int num, int den);
Fraction(std::pair<int, int> pair);
Fraction(std::vector<int> vector);
Fraction(std::map<std::string, int> mapping);
int GreatestCommonDenominator();
void Reduce();
float ToFloat();
double ToDouble() const;
int ToInt();
Fraction Reciprocal() const;
// Multiplication and division
openshot::Fraction operator*(openshot::Fraction other) {
return openshot::Fraction(num * other.num, den * other.den);
}
openshot::Fraction operator/(openshot::Fraction other) {
return *this * other.Reciprocal();
}
template<class numT>
numT operator*(const numT& other) const {
return static_cast<numT>(ToDouble() * other);
}
template<class numT>
numT operator/(const numT& other) const {
return static_cast<numT>(ToDouble() / other);
}
};
template<class numT>
numT operator*(const numT& left, const openshot::Fraction& right) {
return static_cast<numT>(left * right.ToDouble());
}
template<class numT>
numT operator/(const numT& left, const openshot::Fraction& right) {
return static_cast<numT>(left / right.ToDouble());
}
// Stream output operator for openshot::Fraction
template<class charT, class traits>
std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& o, const openshot::Fraction& frac) {
std::basic_ostringstream<charT, traits> s;
s.flags(o.flags());
s.imbue(o.getloc());
s.precision(o.precision());
s << "Fraction(" << frac.num << ", " << frac.den << ")";
return o << s.str();
}
} // namespace openshot
#endif
openshot::Fraction
This class represents a fraction.
Definition Fraction.h:30
openshot::Fraction::num
int num
Numerator for the fraction.
Definition Fraction.h:32
openshot::Fraction::operator*
openshot::Fraction operator*(openshot::Fraction other)
Multiply two Fraction objects together.
Definition Fraction.h:71
openshot::Fraction::Fraction
Fraction()
Default Constructor.
Definition Fraction.cpp:19
openshot::Fraction::ToFloat
float ToFloat()
Return this fraction as a float (i.e. 1/2 = 0.5).
Definition Fraction.cpp:35
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)
Divide a Fraction by another Fraction.
Definition Fraction.h:76
openshot::Fraction::ToDouble
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5).
Definition Fraction.cpp:40
openshot::Fraction::Reduce
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3).
Definition Fraction.cpp:65
openshot::Fraction::ToInt
int ToInt()
Return a rounded integer of the fraction (for example 30000/1001 returns 30).
Definition Fraction.cpp:45
openshot::Fraction::Reciprocal
Fraction Reciprocal() const
Return the reciprocal as a Fraction.
Definition Fraction.cpp:78
openshot::Fraction::GreatestCommonDenominator
int GreatestCommonDenominator()
Calculate the greatest common denominator.
Definition Fraction.cpp:50
openshot::Fraction::operator/
numT operator/(const numT &other) const
Division in the form (openshot_Fraction / numeric_value).
Definition Fraction.h:88
openshot::Fraction::den
int den
Denominator for the fraction.
Definition Fraction.h:33
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::operator*
numT operator*(const numT &left, const openshot::Fraction &right)
Multiplication in the form (numeric_value * openshot_Fraction).
Definition Fraction.h:95
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::operator/
numT operator/(const numT &left, const openshot::Fraction &right)
Division in the form (numeric_value / openshot_Fraction).
Definition Fraction.h:101