Loading...
Searching...
No Matches
Fraction.cpp
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "Fraction.h"
#include <cmath>
using namespace openshot;
// Delegating constructors
Fraction::Fraction() : Fraction::Fraction(1, 1) {}
Fraction::Fraction(std::pair<int, int> pair)
: Fraction::Fraction(pair.first, pair.second) {}
Fraction::Fraction(std::map<std::string, int> mapping)
: Fraction::Fraction(mapping["num"], mapping["den"]) {}
Fraction::Fraction(std::vector<int> vector)
: Fraction::Fraction(vector[0], vector[1]) {}
// Full constructor
Fraction::Fraction(int num, int den) :
num(num), den(den) {}
// 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)
double Fraction::ToDouble() const {
return double(num) / double(den);
}
// Return a rounded integer of the frame rate (for example 30000/1001 returns 30 fps)
int Fraction::ToInt() {
return round((double) num / den);
}
// Calculate the greatest common denominator
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();
if (GCD == 0) {
return;
}
// Reduce this fraction to the smallest possible whole numbers
num = num / GCD;
den = den / GCD;
}
// Return the reciprocal as a new Fraction
Fraction Fraction::Reciprocal() const
{
// flip the fraction
return Fraction(den, num);
}
Fraction.h
Header file for Fraction class.
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::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::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::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