MusicTheory Docs API Toolbox GitHub NuGet
Contents

API / MusicTheory

Rational struct

An exact fraction of Int64 parts, always in lowest terms with the sign on the numerator. Arithmetic is checked: it throws OverflowException rather than wrapping.
default(Rational) is zero. The denominator is stored minus one so that the default value is valid and equal to Rational.Zero.

Properties

long Numerator { get; }
The numerator, in lowest terms; it carries the sign.
long Denominator { get; }
The denominator, in lowest terms; always positive.

Fields

static readonly Rational Zero
The value 0.
static readonly Rational One
The value 1.

Methods

int CompareTo(Rational other)
static Rational Of(long numerator, long denominator = 1)
Builds the fraction numerator/denominator in lowest terms.

Throws DivideByZeroException The denominator is zero.

Throws OverflowException A part is Int64.MinValue and cannot be negated or reduced.

double ToDouble()
The nearest Double.
string ToString()
Formats as 3/8, -1/4, or just 3 when the denominator is 1.