MUSX Document Model
Loading...
Searching...
No Matches
musx::util::Fraction Class Reference

A class to represent fractions with integer m_numerator and m_denominator, automatically reduced to simplest form. More...

#include <Fraction.h>

Public Member Functions

 Fraction (int num=0, int den=1)
 Constructs a Fraction object.
 
constexpr int numerator () const
 Gets the m_numerator of the fraction.
 
constexpr int denominator () const
 Gets the m_denominator of the fraction.
 
constexpr int quotient () const
 Returns the integer (whole number) part of the fraction.
 
Fraction remainder () const
 Returns the fractional part of the fraction.
 
Fraction reciprocal () const
 Returns the reciprocal fraction.
 
constexpr dom::Edu calcEduDuration () const
 Calculates duration as a fraction of a whole note.
 
constexpr double toDouble () const
 Converts the fraction to floating point double.
 
Fraction operator+ (const Fraction &other) const
 Adds two fractions.
 
Fraction operator- (const Fraction &other) const
 Subtracts one fraction from another.
 
Fraction operator* (const Fraction &other) const
 Multiplies two fractions.
 
Fraction operator/ (const Fraction &other) const
 Divides one fraction by another.
 
Fractionoperator+= (const Fraction &other)
 Compound addition assignment operator.
 
Fractionoperator-= (const Fraction &other)
 Compound subtraction assignment operator.
 
Fractionoperator*= (const Fraction &other)
 Compound multiplication assignment operator.
 
Fractionoperator/= (const Fraction &other)
 Compound division assignment operator.
 
constexpr bool operator== (const Fraction &other) const
 Equality comparison operator. (This depends on the fact that instances of Fraction are always reduced in the constructor.)
 
constexpr bool operator!= (const Fraction &other) const
 Inequality comparison operator.
 
constexpr bool operator< (const Fraction &other) const
 Less-than comparison operator.
 
constexpr bool operator<= (const Fraction &other) const
 Less-than-or-equal-to comparison operator.
 
constexpr bool operator> (const Fraction &other) const
 Greater-than comparison operator.
 
constexpr bool operator>= (const Fraction &other) const
 Greater-than-or-equal-to comparison operator.
 
constexpr operator bool () const
 Checks if the fraction is nonzero.
 

Static Public Member Functions

static Fraction fromEdu (dom::Edu edu)
 Constructs a Fraction from edu.
 
static Fraction fromPercent (int percent)
 Constructs a Fraction from a percent (where 100 is 100%)
 

Friends

std::ostream & operator<< (std::ostream &os, const Fraction &frac)
 Stream output operator.
 
std::istream & operator>> (std::istream &is, Fraction &frac)
 Stream input operator.
 

Detailed Description

A class to represent fractions with integer m_numerator and m_denominator, automatically reduced to simplest form.

Constructor & Destructor Documentation

◆ Fraction()

musx::util::Fraction::Fraction ( int  num = 0,
int  den = 1 
)
inline

Constructs a Fraction object.

Parameters
numThe m_numerator of the fraction.
denThe m_denominator of the fraction. Defaults to 1.
Exceptions
std::invalid_argumentif the m_denominator is zero.
Todo:
Make this constructor constexpr when we drop C++17 support.

Member Function Documentation

◆ denominator()

constexpr int musx::util::Fraction::denominator ( ) const
inlineconstexpr

Gets the m_denominator of the fraction.

Returns
The m_denominator.

◆ fromEdu()

static Fraction musx::util::Fraction::fromEdu ( dom::Edu  edu)
inlinestatic

Constructs a Fraction from edu.

Parameters
eduThe Edu value to convert. It is converted to a fraction of a whole note, so 1024 is constructed as Fraction(1, 4).
Todo:
Make this function constexpr when we drop C++17 support.

◆ fromPercent()

static Fraction musx::util::Fraction::fromPercent ( int  percent)
inlinestatic

Constructs a Fraction from a percent (where 100 is 100%)

Parameters
percentThe integral percent value to convert.
Todo:
Make this function constexpr when we drop C++17 support.

◆ numerator()

constexpr int musx::util::Fraction::numerator ( ) const
inlineconstexpr

Gets the m_numerator of the fraction.

Returns
The m_numerator.

◆ operator bool()

constexpr musx::util::Fraction::operator bool ( ) const
inlineexplicitconstexpr

Checks if the fraction is nonzero.

Returns
True if the fraction is nonzero, false otherwise.

◆ operator!=()

constexpr bool musx::util::Fraction::operator!= ( const Fraction other) const
inlineconstexpr

Inequality comparison operator.

Parameters
otherThe other fraction to compare.
Returns
True if the fractions are not equal, false otherwise.

◆ operator*()

Fraction musx::util::Fraction::operator* ( const Fraction other) const
inline

Multiplies two fractions.

Parameters
otherThe other fraction to multiply.
Returns
The resulting fraction after multiplication.
Todo:
Make this function constexpr when we drop C++17 support.

◆ operator*=()

Fraction & musx::util::Fraction::operator*= ( const Fraction other)
inline

Compound multiplication assignment operator.

Parameters
otherThe other fraction to multiply.
Returns
A reference to the updated fraction.
Todo:
Make this function constexpr when we drop C++17 support.

◆ operator+()

Fraction musx::util::Fraction::operator+ ( const Fraction other) const
inline

Adds two fractions.

Parameters
otherThe other fraction to add.
Returns
The resulting fraction after addition.
Todo:
Make this function constexpr when we drop C++17 support.

◆ operator+=()

Fraction & musx::util::Fraction::operator+= ( const Fraction other)
inline

Compound addition assignment operator.

Parameters
otherThe other fraction to add.
Returns
A reference to the updated fraction.
Todo:
Make this function constexpr when we drop C++17 support.

◆ operator-()

Fraction musx::util::Fraction::operator- ( const Fraction other) const
inline

Subtracts one fraction from another.

Parameters
otherThe other fraction to subtract.
Returns
The resulting fraction after subtraction.
Todo:
Make this function constexpr when we drop C++17 support.

◆ operator-=()

Fraction & musx::util::Fraction::operator-= ( const Fraction other)
inline

Compound subtraction assignment operator.

Parameters
otherThe other fraction to subtract.
Returns
A reference to the updated fraction.
Todo:
Make this function constexpr when we drop C++17 support.

◆ operator/()

Fraction musx::util::Fraction::operator/ ( const Fraction other) const
inline

Divides one fraction by another.

Parameters
otherThe other fraction to divide by.
Returns
The resulting fraction after division.
Exceptions
std::invalid_argumentif attempting to divide by a fraction with a zero m_numerator.
Todo:
Make this function constexpr when we drop C++17 support.

◆ operator/=()

Fraction & musx::util::Fraction::operator/= ( const Fraction other)
inline

Compound division assignment operator.

Parameters
otherThe other fraction to divide by.
Returns
A reference to the updated fraction.
Todo:
Make this function constexpr when we drop C++17 support.

◆ operator<()

constexpr bool musx::util::Fraction::operator< ( const Fraction other) const
inlineconstexpr

Less-than comparison operator.

Parameters
otherThe other fraction to compare.
Returns
True if this fraction is less than the other, false otherwise.

◆ operator<=()

constexpr bool musx::util::Fraction::operator<= ( const Fraction other) const
inlineconstexpr

Less-than-or-equal-to comparison operator.

Parameters
otherThe other fraction to compare.
Returns
True if this fraction is less than or equal to the other, false otherwise.

◆ operator==()

constexpr bool musx::util::Fraction::operator== ( const Fraction other) const
inlineconstexpr

Equality comparison operator. (This depends on the fact that instances of Fraction are always reduced in the constructor.)

Parameters
otherThe other fraction to compare.
Returns
True if the fractions are equal, false otherwise.

◆ operator>()

constexpr bool musx::util::Fraction::operator> ( const Fraction other) const
inlineconstexpr

Greater-than comparison operator.

Parameters
otherThe other fraction to compare.
Returns
True if this fraction is greater than the other, false otherwise.

◆ operator>=()

constexpr bool musx::util::Fraction::operator>= ( const Fraction other) const
inlineconstexpr

Greater-than-or-equal-to comparison operator.

Parameters
otherThe other fraction to compare.
Returns
True if this fraction is greater than or equal to the other, false otherwise.

◆ quotient()

constexpr int musx::util::Fraction::quotient ( ) const
inlineconstexpr

Returns the integer (whole number) part of the fraction.

Returns
The integer part of the fraction.

◆ reciprocal()

Fraction musx::util::Fraction::reciprocal ( ) const
inline

Returns the reciprocal fraction.

Todo:
Make this function constexpr when we drop C++17 support.

◆ remainder()

Fraction musx::util::Fraction::remainder ( ) const
inline

Returns the fractional part of the fraction.

Returns
The remainder as a fraction, satisfying -1 < remainder < 1.
Todo:
Make this function constexpr when we drop C++17 support.

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  os,
const Fraction frac 
)
friend

Stream output operator.

Parameters
osThe output stream.
fracThe fraction to output.
Returns
A reference to the output stream.

◆ operator>>

std::istream & operator>> ( std::istream &  is,
Fraction frac 
)
friend

Stream input operator.

Parameters
isThe input stream.
fracThe fraction to populate.
Returns
A reference to the input stream.
Exceptions
std::invalid_argumentif the input format is invalid or the m_denominator is zero.