/** * @file * @brief Header file for Fraction class * @author Jonathan Thomas * * @ref License */ // Copyright (c) 2008-2019 OpenShot Studios, LLC // // SPDX-License-Identifier: LGPL-3.0-or-later #ifndef OPENSHOT_FRACTION_H #define OPENSHOT_FRACTION_H #include // for std::string #include // for std::pair #include // for std::map #include // for std::vector namespace openshot { /** * @brief This class represents a fraction * * Fractions are often used in video editing to represent ratios and rates, for example: * pixel ratios, frames per second, timebase, and other common ratios. Fractions are preferred * over decimals due to their increased precision. */ class Fraction { public: int num; /// pair); /// Constructor that takes a vector of length 2 (containing {num, den}) Fraction(std::vector vector); /// Constructor that takes a key-value mapping (keys: 'num'. 'den') Fraction(std::map mapping); /// Calculate the greatest common denominator int GreatestCommonDenominator(); /// Reduce this fraction (i.e. 640/480 = 4/3) void Reduce(); /// Return this fraction as a float (i.e. 1/2 = 0.5) float ToFloat(); /// Return this fraction as a double (i.e. 1/2 = 0.5) double ToDouble() const; /// Return a rounded integer of the fraction (for example 30000/1001 returns 30) int ToInt(); /// Return the reciprocal as a Fraction Fraction Reciprocal() const; // Multiplication and division /// Multiply two Fraction objects together openshot::Fraction operator*(openshot::Fraction other) { return openshot::Fraction(num * other.num, den * other.den); } /// Divide a Fraction by another Fraction openshot::Fraction operator/(openshot::Fraction other) { return *this * other.Reciprocal(); } /// Multiplication in the form (openshot_Fraction * numeric_value) template numT operator*(const numT& other) const { return static_cast(ToDouble() * other); } /// Division in the form (openshot_Fraction / numeric_value) template numT operator/(const numT& other) const { return static_cast(ToDouble() / other); } }; /// Multiplication in the form (numeric_value * openshot_Fraction) template numT operator*(const numT& left, const openshot::Fraction& right) { return static_cast(left * right.ToDouble()); } /// Division in the form (numeric_value / openshot_Fraction) template numT operator/(const numT& left, const openshot::Fraction& right) { return static_cast(left / right.ToDouble()); } // Stream output operator for openshot::Fraction template std::basic_ostream& operator<<(std::basic_ostream& o, const openshot::Fraction& frac) { std::basic_ostringstream 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