MNX Document Model
Loading...
Searching...
No Matches
mnx::FractionValue Class Reference

Represents a detached arithmetic fraction with normalization. More...

#include <CommonClasses.h>

Public Types

using NumType = unsigned
 Unsigned integer type used for numerator and denominator.
 

Public Member Functions

constexpr FractionValue ()=default
 Default constructor initializes the value to 0/1.
 
 FractionValue (NumType num, NumType den)
 Constructs a fraction from a numerator and denominator.
 
constexpr FractionValue (NumType value)
 Constructs a Fraction object from an integer.
 
constexpr NumType numerator () const noexcept
 Returns the numerator.
 
constexpr NumType denominator () const noexcept
 Returns the denominator.
 
constexpr NumType quotient () const
 Returns the integer (whole number) part of the fraction.
 
FractionValue constexpr remainder () const
 Returns the fractional part of the fraction.
 
constexpr FractionValueoperator+= (const FractionValue &rhs)
 Adds another FractionValue to this one.
 
constexpr FractionValueoperator-= (const FractionValue &rhs)
 Subtracts another FractionValue from this one.
 
constexpr FractionValueoperator*= (const FractionValue &rhs)
 Multiplies this fraction by another.
 
FractionValueoperator/= (const FractionValue &rhs)
 Divides this fraction by another.
 
constexpr void reduce ()
 Reduces the fraction to lowest terms using std::gcd.
 
constexpr bool expressWithDenominator (NumType targetDenominator)
 Attempts to express this fraction with the given denominator.
 

Static Public Member Functions

static constexpr FractionValue max () noexcept
 Constructs the max fractional value.
 

Detailed Description

Represents a detached arithmetic fraction with normalization.

This class provides arithmetic operations on rational numbers without attaching them to the JSON DOM. It is intended for musical-duration and proportional calculations where a simple fraction is needed independently of the MNX DOM structure.

A FractionValue constructed from a numerator and denominator preserves the values exactly as provided. Arithmetic operations (+=, -=, *=, /=) reduce the result to lowest terms using std::gcd. Call reduce() explicitly if you need a reduced form immediately after construction.

The class supports:

  • Exact rational arithmetic (addition, subtraction, multiplication, division)
  • Normalization to lowest terms after each operation
  • Comparison operators (in helper section)

Compound assignment operators (+=, -=, *=, /=) are implemented as members. Binary operators (+, -, *, /) are implemented as non-member functions to allow symmetric conversions and correct C++ arithmetic semantics.

Constructor & Destructor Documentation

◆ FractionValue() [1/2]

mnx::FractionValue::FractionValue ( NumType  num,
NumType  den 
)
inline

Constructs a fraction from a numerator and denominator.

The fraction is not automatically reduced. Use reduce() if you need the value in lowest terms. However, fractions are automatically reduced after arithmetical operations.

Parameters
numThe numerator.
denThe denominator. Must not be zero.
Exceptions
std::invalid_argumentif den is zero.
Todo:
Make this constructor constexpr when we drop C++17 support.

◆ FractionValue() [2/2]

constexpr mnx::FractionValue::FractionValue ( NumType  value)
inlineconstexpr

Constructs a Fraction object from an integer.

Parameters
valueThe integer value of the fraction.
Exceptions
std::invalid_argumentif the m_denominator is zero.

Member Function Documentation

◆ expressWithDenominator()

constexpr bool mnx::FractionValue::expressWithDenominator ( NumType  targetDenominator)
inlineconstexpr

Attempts to express this fraction with the given denominator.

If the current value can be written exactly with targetDenominator using an integer numerator, the numerator and denominator are updated to use that denominator and the function returns true. Otherwise, the fraction is left unchanged and the function returns false.

This function does not require the fraction to be in reduced form. It computes a reduced logical form internally when determining whether the requested denominator is compatible.

Examples:

  • 1/1 expressed with denominator 4 becomes 4/4 and returns true.
  • 3/2 expressed with denominator 4 becomes 6/4 and returns true.
  • 2/6 (i.e. 1/3) expressed with denominator 3 becomes 1/3 and returns true.
  • 1/3 expressed with denominator 4 cannot be represented exactly with an integer numerator, so the call returns false and the fraction remains 1/3.

A zero fraction (0 / d) can always be expressed with any nonzero targetDenominator; in that case the result becomes 0 / targetDenominator.

Parameters
targetDenominatorThe desired denominator. If zero, the call fails and the fraction is left unchanged.
Returns
true if the fraction was successfully rewritten with the given denominator; false if no exact integer representation exists or if targetDenominator is zero.

◆ operator*=()

constexpr FractionValue & mnx::FractionValue::operator*= ( const FractionValue rhs)
inlineconstexpr

Multiplies this fraction by another.

Parameters
rhsThe fraction to multiply with.
Returns
Reference to this object after modification.

The result is normalized.

◆ operator+=()

constexpr FractionValue & mnx::FractionValue::operator+= ( const FractionValue rhs)
inlineconstexpr

Adds another FractionValue to this one.

Parameters
rhsThe fraction to add.
Returns
Reference to this object after modification.

The result is normalized.

◆ operator-=()

constexpr FractionValue & mnx::FractionValue::operator-= ( const FractionValue rhs)
inlineconstexpr

Subtracts another FractionValue from this one.

Parameters
rhsThe fraction to subtract.
Returns
Reference to this object after modification.

The result is normalized.

◆ operator/=()

FractionValue & mnx::FractionValue::operator/= ( const FractionValue rhs)
inline

Divides this fraction by another.

Parameters
rhsThe divisor.
Returns
Reference to this object after modification.
Exceptions
std::invalid_argumentif rhs has a numerator of zero.

The result is normalized.

◆ quotient()

constexpr NumType mnx::FractionValue::quotient ( ) const
inlineconstexpr

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

Returns
The integer part of the fraction.

◆ remainder()

FractionValue constexpr mnx::FractionValue::remainder ( ) const
inlineconstexpr

Returns the fractional part of the fraction.

Returns
The remainder as a fraction, satisfying -1 < remainder < 1.