Table of Contents

Struct Rational

Namespace
Celeritas.Core
Assembly
Celeritas.dll

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

numerator long

The numerator (any sign).

denominator long

The denominator; must be non-zero.

Exceptions

ArgumentException

denominator is 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

long

Eighth

An eighth note (1/8 of a whole note).

public static Rational Eighth { get; }

Property Value

Rational

Half

A half note (1/2 of a whole note).

public static Rational Half { get; }

Property Value

Rational

Numerator

The signed numerator, in lowest terms.

public long Numerator { get; }

Property Value

long

Quarter

A quarter note (1/4 of a whole note).

public static Rational Quarter { get; }

Property Value

Rational

Sixteenth

A sixteenth note (1/16 of a whole note).

public static Rational Sixteenth { get; }

Property Value

Rational

Whole

A whole note (1/1), the unit of the whole-note time model.

public static Rational Whole { get; }

Property Value

Rational

Zero

Zero (0/1).

public static Rational Zero { get; }

Property Value

Rational

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

other Rational

Returns

int

ToDouble()

Converts to the nearest double (may lose precision).

public double ToDouble()

Returns

double

ToString()

Formats as "n" when the denominator is 1, otherwise "n/d".

public override string ToString()

Returns

string

Operators

operator +(Rational, Rational)

Adds two rational numbers exactly.

public static Rational operator +(Rational a, Rational b)

Parameters

a Rational
b Rational

Returns

Rational

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

a Rational
b Rational

Returns

Rational

Exceptions

DivideByZeroException

b is 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

a Rational
b long

Returns

Rational

Exceptions

DivideByZeroException

b is 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

a Rational
b Rational

Returns

bool

operator >=(Rational, Rational)

Exact greater-than-or-equal comparison (128-bit cross-multiplication, no overflow).

public static bool operator >=(Rational a, Rational b)

Parameters

a Rational
b Rational

Returns

bool

operator <(Rational, Rational)

Exact less-than comparison (128-bit cross-multiplication, no overflow).

public static bool operator <(Rational a, Rational b)

Parameters

a Rational
b Rational

Returns

bool

operator <=(Rational, Rational)

Exact less-than-or-equal comparison (128-bit cross-multiplication, no overflow).

public static bool operator <=(Rational a, Rational b)

Parameters

a Rational
b Rational

Returns

bool

operator *(Rational, Rational)

Multiplies two rational numbers exactly (cross-reducing to avoid overflow).

public static Rational operator *(Rational a, Rational b)

Parameters

a Rational
b Rational

Returns

Rational

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

a Rational
b long

Returns

Rational

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

a Rational
b Rational

Returns

Rational

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

a Rational

Returns

Rational