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
intSee numerator.
denominator
intSee denominator.
Remarks
Will simplify the fraction.
Exceptions
- ArgumentOutOfRangeException
numerator
ordenominator
are MinValue.
Fields
Epsilon
The smallest positive value that can be represented.
public static readonly Rational Epsilon
Field Value
Half
The value 1/2.
public static readonly Rational Half
Field Value
MaxValue
The most positive value that can be represented.
public static readonly Rational MaxValue
Field Value
- See Also
MinValue
The most negative value that can be represented.
public static readonly Rational MinValue
Field Value
- See Also
Undefined
Represents any value with denominator 0, but is stored as 0/0.
public static readonly Rational Undefined
Field Value
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
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
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
isUndefined
public bool isUndefined { get; }
Property Value
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
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
Remarks
Equals(Rational)
public bool Equals(Rational other)
Parameters
other
Rational
Returns
Equals(int)
public bool Equals(int other)
Parameters
other
int
Returns
Equals(object)
See Equals(Rational).
public override bool Equals(object obj)
Parameters
obj
object
Returns
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
Returns
Remarks
operator /(Rational, Rational)
public static Rational operator /(Rational a, Rational b)
Parameters
Returns
Remarks
operator ==(Rational, Rational)
Whether the two Rationals represent the same fraction.
public static bool operator ==(Rational a, Rational b)
Parameters
Returns
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
Exceptions
- DivideByZeroException
rational
is Undefined.
operator >(Rational, Rational)
Whether a
> b
.
public static bool operator >(Rational a, Rational b)
Parameters
Returns
Remarks
operator >=(Rational, Rational)
Whether a
>= b
.
public static bool operator >=(Rational a, Rational b)
Parameters
Returns
Remarks
implicit operator float(Rational)
public static implicit operator float(Rational rational)
Parameters
rational
Rational
Returns
Remarks
implicit operator Rational(int)
Converts the integer into a Rational with denominator 1.
public static implicit operator Rational(int integer)
Parameters
integer
int
Returns
operator !=(Rational, Rational)
public static bool operator !=(Rational a, Rational b)
Parameters
Returns
operator <(Rational, Rational)
Whether a
< b
.
public static bool operator <(Rational a, Rational b)
Parameters
Returns
Remarks
operator <=(Rational, Rational)
Whether a
<= b
.
public static bool operator <=(Rational a, Rational b)
Parameters
Returns
Remarks
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
Returns
Remarks
operator *(Rational, Rational)
public static Rational operator *(Rational a, Rational b)
Parameters
Returns
Remarks
operator -(Rational, Rational)
public static Rational operator -(Rational a, Rational b)
Parameters
Returns
Remarks
operator -(Rational)
public static Rational operator -(Rational a)
Parameters
a
Rational