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.
 
int numerator () const
 Gets the m_numerator of the fraction.
 
int denominator () const
 Gets the m_denominator of the fraction.
 
int quotient () const
 Returns the integer (whole number) part of the fraction.
 
Fraction remainder () const
 Returns the fractional part of the fraction.
 
dom::Edu calcEduDuration () const
 Calculates duration as a fraction of a whole note.
 
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.
 
bool operator== (const Fraction &other) const
 Equality comparison operator.
 
bool operator!= (const Fraction &other) const
 Inequality comparison operator.
 
bool operator< (const Fraction &other) const
 Less-than comparison operator.
 
bool operator<= (const Fraction &other) const
 Less-than-or-equal-to comparison operator.
 
bool operator> (const Fraction &other) const
 Greater-than comparison operator.
 
bool operator>= (const Fraction &other) const
 Greater-than-or-equal-to comparison operator.
 
 operator bool () const
 Checks if the fraction is nonzero.
 

Static Public Member Functions

static Fraction fromEdu (int edu)
 Constructs a Fraction from edu.
 

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.

Member Function Documentation

◆ denominator()

int musx::util::Fraction::denominator ( ) const
inline

Gets the m_denominator of the fraction.

Returns
The m_denominator.

◆ fromEdu()

Fraction musx::util::Fraction::fromEdu ( int  edu)
static

Constructs a Fraction from edu.

Parameters
edu

◆ numerator()

int musx::util::Fraction::numerator ( ) const
inline

Gets the m_numerator of the fraction.

Returns
The m_numerator.

◆ operator bool()

musx::util::Fraction::operator bool ( ) const
inlineexplicit

Checks if the fraction is nonzero.

Returns
True if the fraction is nonzero, false otherwise.

◆ operator!=()

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

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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.
Exceptions
std::invalid_argumentif attempting to divide by a fraction with a zero m_numerator.

◆ operator<()

bool musx::util::Fraction::operator< ( const Fraction other) const
inline

Less-than comparison operator.

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

◆ operator<=()

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

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==()

bool musx::util::Fraction::operator== ( const Fraction other) const
inline

Equality comparison operator.

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

◆ operator>()

bool musx::util::Fraction::operator> ( const Fraction other) const
inline

Greater-than comparison operator.

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

◆ operator>=()

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

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()

int musx::util::Fraction::quotient ( ) const
inline

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

Returns
The integer part of the fraction.

◆ 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.

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.