2013-09-12 17:52:10 -05:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Source file for Fraction class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
2019-06-09 08:31:04 -04:00
|
|
|
* @ref License
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-16 01:26:26 -04:00
|
|
|
// Copyright (c) 2008-2019 OpenShot Studios, LLC
|
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2013-09-12 17:52:10 -05:00
|
|
|
|
2020-10-18 07:43:37 -04:00
|
|
|
#include "Fraction.h"
|
2020-11-21 15:17:18 -05:00
|
|
|
#include <cmath>
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
2020-11-21 15:17:18 -05:00
|
|
|
// Delegating constructors
|
2021-08-09 09:54:59 -04:00
|
|
|
Fraction::Fraction() : Fraction::Fraction(1, 1) {}
|
2020-11-21 15:17:18 -05:00
|
|
|
|
|
|
|
|
Fraction::Fraction(std::pair<int, int> pair)
|
2021-08-09 09:54:59 -04:00
|
|
|
: Fraction::Fraction(pair.first, pair.second) {}
|
2020-11-21 15:17:18 -05:00
|
|
|
|
|
|
|
|
Fraction::Fraction(std::map<std::string, int> mapping)
|
2021-08-09 09:54:59 -04:00
|
|
|
: Fraction::Fraction(mapping["num"], mapping["den"]) {}
|
2020-11-21 15:17:18 -05:00
|
|
|
|
|
|
|
|
Fraction::Fraction(std::vector<int> vector)
|
2021-08-09 09:54:59 -04:00
|
|
|
: Fraction::Fraction(vector[0], vector[1]) {}
|
2020-11-21 15:17:18 -05:00
|
|
|
|
|
|
|
|
// Full constructor
|
2011-10-11 08:44:27 -05:00
|
|
|
Fraction::Fraction(int num, int den) :
|
2021-08-09 09:54:59 -04:00
|
|
|
num(num), den(den) {}
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
// Return this fraction as a float (i.e. 1/2 = 0.5)
|
|
|
|
|
float Fraction::ToFloat() {
|
|
|
|
|
return float(num) / float(den);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return this fraction as a double (i.e. 1/2 = 0.5)
|
2021-01-19 16:03:51 -03:00
|
|
|
double Fraction::ToDouble() const {
|
2011-10-11 08:44:27 -05:00
|
|
|
return double(num) / double(den);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-05 23:12:56 -06:00
|
|
|
// Return a rounded integer of the frame rate (for example 30000/1001 returns 30 fps)
|
|
|
|
|
int Fraction::ToInt() {
|
2017-01-24 18:39:17 -06:00
|
|
|
return round((double) num / den);
|
2014-01-05 23:12:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate the greatest common denominator
|
2011-10-11 08:44:27 -05:00
|
|
|
int Fraction::GreatestCommonDenominator() {
|
|
|
|
|
int first = num;
|
|
|
|
|
int second = den;
|
|
|
|
|
|
|
|
|
|
// Find the biggest whole number that will divide into both the numerator
|
|
|
|
|
// and denominator
|
|
|
|
|
int t;
|
|
|
|
|
while (second != 0) {
|
|
|
|
|
t = second;
|
|
|
|
|
second = first % second;
|
|
|
|
|
first = t;
|
|
|
|
|
}
|
|
|
|
|
return first;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Fraction::Reduce() {
|
|
|
|
|
// Get the greatest common denominator
|
|
|
|
|
int GCD = GreatestCommonDenominator();
|
2023-05-01 15:41:44 -05:00
|
|
|
if (GCD == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-10-11 08:44:27 -05:00
|
|
|
|
|
|
|
|
// Reduce this fraction to the smallest possible whole numbers
|
|
|
|
|
num = num / GCD;
|
|
|
|
|
den = den / GCD;
|
|
|
|
|
}
|
2011-12-11 20:42:50 -06:00
|
|
|
|
|
|
|
|
// Return the reciprocal as a new Fraction
|
2021-01-22 20:03:05 -03:00
|
|
|
Fraction Fraction::Reciprocal() const
|
2011-12-11 20:42:50 -06:00
|
|
|
{
|
|
|
|
|
// flip the fraction
|
|
|
|
|
return Fraction(den, num);
|
|
|
|
|
}
|