Table of Contents

Struct Rational

Namespace
PAC.DataStructures

A fraction a / b (where a and b are integers) kept in simplest form.

public readonly struct Rational : IEquatable<Rational>, IEquatable<int>
Implements
Inherited Members
Extension Methods

Remarks

Note that small values can have large numerators and denominators, which could cause overflow in e.g. operator +(Rational, Rational).

Constructors

Rational(int, int)

public Rational(int numerator, int denominator)

Parameters

numerator int

See numerator.

denominator int

See denominator.

Remarks

Will simplify the fraction.

Exceptions

ArgumentOutOfRangeException

numerator or denominator are MinValue.

Fields

Epsilon

The smallest positive value that can be represented.

public static readonly Rational Epsilon

Field Value

Rational

Half

The value 1/2.

public static readonly Rational Half

Field Value

Rational

MaxValue

The most positive value that can be represented.

public static readonly Rational MaxValue

Field Value

Rational
See Also

MinValue

The most negative value that can be represented.

public static readonly Rational MinValue

Field Value

Rational
See Also

Undefined

Represents any value with denominator 0, but is stored as 0/0.

public static readonly Rational Undefined

Field Value

Rational

Remarks

Note that this will not be considered == to itself. Use isUndefined instead to determine if a Rational is Undefined.

denominator

The bottom number of the fraction.

public readonly int denominator

Field Value

int

Remarks

This will always be non-negative and coprime to the numerator.

If the numerator is 0 and this is not 0, this will be 1.

numerator

The top number of the fraction.

public readonly int numerator

Field Value

int

Remarks

This will always be coprime to the denominator and its sign will always be the sign of the Rational.

If the denominator is 0, this will be 0 (and this Rational represents an undefined value).

See Also

Properties

isInteger

Whether the denominator is 1.

public bool isInteger { get; }

Property Value

bool

isUndefined

Whether the Rational is Undefined.

public bool isUndefined { get; }

Property Value

bool

Remarks

Note that two Undefined values are not considered ==, so use this method to check if a Rational is Undefined.

sign

The sign of the Rational.

public int sign { get; }

Property Value

int
  • 1 if the value is positive
  • 0 if the value is zero or Undefined
  • -1 if the value is negative

Remarks

This will match the sign of the numerator.

Methods

Abs(Rational)

Returns the absolute value of a.

public static Rational Abs(Rational a)

Parameters

a Rational

Returns

Rational

Remarks

If a is Undefined, it will return Undefined.

Equals(Rational)

public bool Equals(Rational other)

Parameters

other Rational

Returns

bool

Equals(int)

public bool Equals(int other)

Parameters

other int

Returns

bool

Equals(object)

public override bool Equals(object obj)

Parameters

obj object

Returns

bool

GetHashCode()

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int

A 32-bit signed integer that is the hash code for this instance.

ToString()

Returns the fully qualified type name of this instance.

public override string ToString()

Returns

string

The fully qualified type name.

Operators

operator +(Rational, Rational)

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

Parameters

a Rational
b Rational

Returns

Rational

Remarks

If either of a and b are Undefined, it will return Undefined.

operator /(Rational, Rational)

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

Parameters

a Rational
b Rational

Returns

Rational

Remarks

If either of a and b are Undefined, or if b is 0, it will return Undefined.

operator ==(Rational, Rational)

Whether the two Rationals represent the same fraction.

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

Parameters

a Rational
b Rational

Returns

bool

Remarks

Two Undefined values are not considered ==. Use isUndefined to check if a Rational is Undefined.

explicit operator int(Rational)

Rounds the Rational towards zero.

public static explicit operator int(Rational rational)

Parameters

rational Rational

Returns

int

Exceptions

DivideByZeroException

rational is Undefined.

operator >(Rational, Rational)

Whether a > b.

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

Parameters

a Rational
b Rational

Returns

bool

Remarks

Returns false if either of a or b are Undefined.

operator >=(Rational, Rational)

Whether a >= b.

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

Parameters

a Rational
b Rational

Returns

bool

Remarks

Returns false if either of a or b are Undefined.

implicit operator float(Rational)

Converts the Rational to a float.

public static implicit operator float(Rational rational)

Parameters

rational Rational

Returns

float

Remarks

Undefined will be converted to NaN.

implicit operator Rational(int)

Converts the integer into a Rational with denominator 1.

public static implicit operator Rational(int integer)

Parameters

integer int

Returns

Rational

operator !=(Rational, Rational)

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

Parameters

a Rational
b Rational

Returns

bool

operator <(Rational, Rational)

Whether a < b.

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

Parameters

a Rational
b Rational

Returns

bool

Remarks

Returns false if either of a or b are Undefined.

operator <=(Rational, Rational)

Whether a <= b.

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

Parameters

a Rational
b Rational

Returns

bool

Remarks

Returns false if either of a or b are Undefined.

operator %(Rational, Rational)

Returns the smallest non-negative Rational r such that there is an integer q such that a = q * b + r.

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

Parameters

a Rational
b Rational

Returns

Rational

Remarks

If either of a and b are Undefined, it will return Undefined.

operator *(Rational, Rational)

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

Parameters

a Rational
b Rational

Returns

Rational

Remarks

If either of a and b are Undefined, it will return Undefined.

operator -(Rational, Rational)

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

Parameters

a Rational
b Rational

Returns

Rational

Remarks

If either of a and b are Undefined, it will return Undefined.

operator -(Rational)

public static Rational operator -(Rational a)

Parameters

a Rational

Returns

Rational

Remarks

If a is Undefined, it will return Undefined.