Struct Rational
High-performance rational number for precise musical time representation. Automatically normalizes to the lowest terms; the denominator is always positive.
default(Rational) is a valid zero (0/1). Comparisons are exact (128-bit
cross-multiplication, no overflow). Arithmetic operators reduce intermediate values
via GCD and throw OverflowException if the true result does not fit
in a 64-bit numerator/denominator, instead of silently wrapping.
public readonly record struct Rational : IComparable<Rational>, IEquatable<Rational>
- Implements
- Inherited Members
Constructors
Rational(long, long)
Creates a rational number, normalized to lowest terms with a positive denominator.
public Rational(long numerator, long denominator)
Parameters
Exceptions
- ArgumentException
denominatoris zero.- OverflowException
Normalization does not fit in a 64-bit numerator/denominator.
Properties
Denominator
The denominator, in lowest terms; always positive (at least 1).
public long Denominator { get; }
Property Value
Eighth
An eighth note (1/8 of a whole note).
public static Rational Eighth { get; }
Property Value
Half
A half note (1/2 of a whole note).
public static Rational Half { get; }
Property Value
Numerator
The signed numerator, in lowest terms.
public long Numerator { get; }
Property Value
Quarter
A quarter note (1/4 of a whole note).
public static Rational Quarter { get; }
Property Value
Sixteenth
A sixteenth note (1/16 of a whole note).
public static Rational Sixteenth { get; }
Property Value
Whole
A whole note (1/1), the unit of the whole-note time model.
public static Rational Whole { get; }
Property Value
Zero
Zero (0/1).
public static Rational Zero { get; }
Property Value
Methods
CompareTo(Rational)
Compares this value with other, returning a negative number, zero, or a
positive number as this value is less than, equal to, or greater than other.
public int CompareTo(Rational other)
Parameters
otherRational
Returns
ToDouble()
Converts to the nearest double (may lose precision).
public double ToDouble()
Returns
ToString()
Formats as "n" when the denominator is 1, otherwise "n/d".
public override string ToString()
Returns
Operators
operator +(Rational, Rational)
Adds two rational numbers exactly.
public static Rational operator +(Rational a, Rational b)
Parameters
Returns
Exceptions
- OverflowException
The exact sum does not fit in a 64-bit rational.
operator /(Rational, Rational)
Divides a by b exactly.
public static Rational operator /(Rational a, Rational b)
Parameters
Returns
Exceptions
- DivideByZeroException
bis zero.- OverflowException
The exact quotient does not fit in a 64-bit rational.
operator /(Rational, long)
Divides a rational number by an integer exactly.
public static Rational operator /(Rational a, long b)
Parameters
Returns
Exceptions
- DivideByZeroException
bis zero.- OverflowException
The exact quotient does not fit in a 64-bit rational.
operator >(Rational, Rational)
Exact greater-than comparison (128-bit cross-multiplication, no overflow).
public static bool operator >(Rational a, Rational b)
Parameters
Returns
operator >=(Rational, Rational)
Exact greater-than-or-equal comparison (128-bit cross-multiplication, no overflow).
public static bool operator >=(Rational a, Rational b)
Parameters
Returns
operator <(Rational, Rational)
Exact less-than comparison (128-bit cross-multiplication, no overflow).
public static bool operator <(Rational a, Rational b)
Parameters
Returns
operator <=(Rational, Rational)
Exact less-than-or-equal comparison (128-bit cross-multiplication, no overflow).
public static bool operator <=(Rational a, Rational b)
Parameters
Returns
operator *(Rational, Rational)
Multiplies two rational numbers exactly (cross-reducing to avoid overflow).
public static Rational operator *(Rational a, Rational b)
Parameters
Returns
Exceptions
- OverflowException
The exact product does not fit in a 64-bit rational.
operator *(Rational, long)
Multiplies a rational number by an integer exactly.
public static Rational operator *(Rational a, long b)
Parameters
Returns
Exceptions
- OverflowException
The exact product does not fit in a 64-bit rational.
operator -(Rational, Rational)
Subtracts b from a exactly.
public static Rational operator -(Rational a, Rational b)
Parameters
Returns
Exceptions
- OverflowException
The exact difference does not fit in a 64-bit rational.
operator -(Rational)
Returns the negation of a.
public static Rational operator -(Rational a)
Parameters
aRational