Struct Fraction
Represents a lightweight fraction, that is a number with separate Numerator and Denominator.
public readonly struct Fraction : IEquatable<Fraction>, IComparable<Fraction>, IComparable
- Implements
- Inherited Members
- Extension Methods
Constructors
Fraction(long, ulong)
Creates a new Fraction instance.
public Fraction(long numerator, ulong denominator = 1)
Parameters
numeratorlongFraction's numerator.
denominatorulongFraction's denominator. Equal to 1 by default.
Exceptions
- ArgumentOutOfRangeException
When
denominatoris equal to 0.
Fields
Epsilon
Represents smallest possible fraction greater than 0, with Numerator equal to 1 and Denominator equal to MaxValue.
public static readonly Fraction Epsilon
Field Value
MaxValue
Represents the maximum fraction value equivalent to MaxValue.
public static readonly Fraction MaxValue
Field Value
MinValue
Represents the minimum fraction value equivalent to MinValue.
public static readonly Fraction MinValue
Field Value
One
Represents 1/1 fraction.
public static readonly Fraction One
Field Value
Zero
Represents 0/1 fraction.
public static readonly Fraction Zero
Field Value
Properties
Denominator
Fraction's denominator.
public ulong Denominator { get; }
Property Value
Numerator
Fraction's numerator.
public long Numerator { get; }
Property Value
Methods
Abs()
Creates a new Fraction instance by calculating an absolute value from this instance.
[Pure]
public Fraction Abs()
Returns
- Fraction
New Fraction instance with unchanged Denominator.
Add(Fraction)
Creates a new Fraction instance by adding other to this instance.
[Pure]
public Fraction Add(Fraction other)
Parameters
otherFractionOther instance to add.
Returns
Ceiling()
Creates a new Fraction instance by calculating the ceiling of this instance.
[Pure]
public Fraction Ceiling()
Returns
- Fraction
New Fraction instance with Denominator equal to 1.
CompareTo(Fraction)
Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
[Pure]
public int CompareTo(Fraction other)
Parameters
otherFractionAn object to compare with this instance.
Returns
- int
A value that indicates the relative order of the objects being compared. The return value has these meanings:
Value Meaning Less than zero This instance precedes otherin the sort order.Zero This instance occurs in the same position in the sort order as other.Greater than zero This instance follows otherin the sort order.
CompareTo(object?)
Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
[Pure]
public int CompareTo(object? obj)
Parameters
objobjectAn object to compare with this instance.
Returns
- int
A value that indicates the relative order of the objects being compared. The return value has these meanings:
Value Meaning Less than zero This instance precedes objin the sort order.Zero This instance occurs in the same position in the sort order as obj.Greater than zero This instance follows objin the sort order.
Exceptions
- ArgumentException
objis not the same type as this instance.
Create(Fixed)
[Pure]
public static Fraction Create(Fixed value)
Parameters
valueFixedValue to convert to a fraction.
Returns
Remarks
Resulting Denominator will be equal to 10^p,
where p represents the Precision of the provided value.
Create(decimal, ulong, MidpointRounding)
[Pure]
public static Fraction Create(decimal value, ulong denominator, MidpointRounding rounding = MidpointRounding.AwayFromZero)
Parameters
valuedecimalValue to convert to a fraction.
denominatorulongFraction's denominator.
roundingMidpointRoundingOptional
valuerounding strategy. Equal to AwayFromZero by default.
Returns
Exceptions
- ArgumentOutOfRangeException
When
denominatoris equal to 0.
Create(double, ulong, MidpointRounding)
[Pure]
public static Fraction Create(double value, ulong denominator, MidpointRounding rounding = MidpointRounding.AwayFromZero)
Parameters
valuedoubleValue to convert to a fraction.
denominatorulongFraction's denominator.
roundingMidpointRoundingOptional
valuerounding strategy. Equal to AwayFromZero by default.
Returns
Exceptions
- ArgumentOutOfRangeException
When
denominatoris equal to 0.
Decrement()
Creates a new Fraction instance by subtracting 1 from this instance.
[Pure]
public Fraction Decrement()
Returns
- Fraction
New Fraction instance with unchanged Denominator.
Divide(Fraction)
Creates a new Fraction instance by dividing this instance by other.
[Pure]
public Fraction Divide(Fraction other)
Parameters
otherFractionOther instance to divide by.
Returns
Exceptions
- DivideByZeroException
When
otheris equal to 0.
Equals(Fraction)
Indicates whether the current object is equal to another object of the same type.
[Pure]
public bool Equals(Fraction other)
Parameters
otherFractionAn object to compare with this object.
Returns
Equals(object?)
Indicates whether this instance and a specified object are equal.
[Pure]
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current instance.
Returns
- bool
true if
objand this instance are the same type and represent the same value; otherwise, false.
Floor()
Creates a new Fraction instance by calculating the floor of this instance.
[Pure]
public Fraction Floor()
Returns
- Fraction
New Fraction instance with Denominator equal to 1.
GetHashCode()
Returns the hash code for this instance.
[Pure]
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
Increment()
Creates a new Fraction instance by adding 1 to this instance.
[Pure]
public Fraction Increment()
Returns
- Fraction
New Fraction instance with unchanged Denominator.
Modulo(Fraction)
Creates a new Fraction instance by calculating the remainder of division of this instance by other.
[Pure]
public Fraction Modulo(Fraction other)
Parameters
otherFractionOther instance to divide by.
Returns
Exceptions
- DivideByZeroException
When
otheris equal to 0.
Multiply(Fraction)
Creates a new Fraction instance by multiplying other and this instance together.
[Pure]
public Fraction Multiply(Fraction other)
Parameters
otherFractionOther instance to multiply by.
Returns
Multiply(Percent)
Creates a new Fraction instance by multiplying percent and this instance together.
[Pure]
public Fraction Multiply(Percent percent)
Parameters
Returns
- Fraction
New Fraction instance with unchanged Denominator.
Negate()
Creates a new Fraction instance by negating this instance.
[Pure]
public Fraction Negate()
Returns
- Fraction
New Fraction instance with unchanged Denominator.
Reciprocal()
Creates a new Fraction instance that represents a reciprocal of this instance.
[Pure]
public Fraction Reciprocal()
Returns
Round(ulong, MidpointRounding)
Creates a new Fraction instance by rounding this instance to the provided denominator.
[Pure]
public Fraction Round(ulong denominator, MidpointRounding rounding = MidpointRounding.AwayFromZero)
Parameters
denominatorulongFraction's denominator.
roundingMidpointRoundingOptional numerator rounding strategy. Equal to AwayFromZero by default.
Returns
Remarks
See Round(decimal, MidpointRounding) for more information.
Exceptions
- ArgumentOutOfRangeException
When
denominatoris equal to 0.
SetDenominator(ulong)
Creates a new Fraction instance with changed Denominator.
[Pure]
public Fraction SetDenominator(ulong value)
Parameters
valueulongFraction's denominator.
Returns
Exceptions
- ArgumentOutOfRangeException
When
valueis equal to 0.
SetNumerator(long)
[Pure]
public Fraction SetNumerator(long value)
Parameters
valuelongFraction's numerator.
Returns
- Fraction
New Fraction instance with unchanged Denominator.
Simplify()
Creates a new Fraction instance equivalent to this instance, with its Numerator and Denominator divided by their GCD (greatest common divisor).
[Pure]
public Fraction Simplify()
Returns
Subtract(Fraction)
Creates a new Fraction instance by subtracting other from this instance.
[Pure]
public Fraction Subtract(Fraction other)
Parameters
otherFractionOther instance to subtract.
Returns
ToString()
Returns a string representation of this Fraction instance.
[Pure]
public override string ToString()
Returns
- string
String representation.
Truncate()
Creates a new Fraction instance by truncating this instance.
[Pure]
public Fraction Truncate()
Returns
- Fraction
New Fraction instance with Denominator equal to 1.
Operators
operator +(Fraction, Fraction)
Creates a new Fraction instance by adding a and b together.
[Pure]
public static Fraction operator +(Fraction a, Fraction b)
Parameters
Returns
operator --(Fraction)
Creates a new Fraction instance by decrementing the provided f.
[Pure]
public static Fraction operator --(Fraction f)
Parameters
fFractionOperand.
Returns
operator /(Fraction, Fraction)
Creates a new Fraction instance by dividing a by b.
[Pure]
public static Fraction operator /(Fraction a, Fraction b)
Parameters
Returns
Exceptions
- DivideByZeroException
When
bis equal to 0.
operator ==(Fraction, Fraction)
Checks if a is equal to b.
[Pure]
public static bool operator ==(Fraction a, Fraction b)
Parameters
Returns
- bool
true when operands are equal, otherwise false.
explicit operator decimal(Fraction)
Converts the provided f to decimal.
[Pure]
public static explicit operator decimal(Fraction f)
Parameters
fFractionFraction to convert.
Returns
explicit operator double(Fraction)
Converts the provided f to double.
[Pure]
public static explicit operator double(Fraction f)
Parameters
fFractionFraction to convert.
Returns
operator >(Fraction, Fraction)
Checks if a is greater than b.
[Pure]
public static bool operator >(Fraction a, Fraction b)
Parameters
Returns
- bool
true when
ais greater thanb, otherwise false.
operator >=(Fraction, Fraction)
Checks if a is greater than or equal to b.
[Pure]
public static bool operator >=(Fraction a, Fraction b)
Parameters
Returns
- bool
true when
ais greater than or equal tob, otherwise false.
implicit operator Fraction(Fixed)
Converts the provided f to Fraction.
[Pure]
public static implicit operator Fraction(Fixed f)
Parameters
Returns
operator ++(Fraction)
Creates a new Fraction instance by incrementing the provided f.
[Pure]
public static Fraction operator ++(Fraction f)
Parameters
fFractionOperand.
Returns
operator !=(Fraction, Fraction)
Checks if a is not equal to b.
[Pure]
public static bool operator !=(Fraction a, Fraction b)
Parameters
Returns
- bool
true when operands are not equal, otherwise false.
operator <(Fraction, Fraction)
Checks if a is less than b.
[Pure]
public static bool operator <(Fraction a, Fraction b)
Parameters
Returns
- bool
true when
ais less thanb, otherwise false.
operator <=(Fraction, Fraction)
Checks if a is less than or equal to b.
[Pure]
public static bool operator <=(Fraction a, Fraction b)
Parameters
Returns
- bool
true when
ais less than or equal tob, otherwise false.
operator %(Fraction, Fraction)
Creates a new Fraction instance by calculating the remainder of division of
a by b.
[Pure]
public static Fraction operator %(Fraction a, Fraction b)
Parameters
Returns
Exceptions
- DivideByZeroException
When
bis equal to 0.
operator *(Fraction, Fraction)
Creates a new Fraction instance by multiplying a and b together.
[Pure]
public static Fraction operator *(Fraction a, Fraction b)
Parameters
Returns
operator *(Fraction, Percent)
Creates a new Fraction instance by multiplying a and b together.
[Pure]
public static Fraction operator *(Fraction a, Percent b)
Parameters
Returns
operator *(Percent, Fraction)
Creates a new Fraction instance by multiplying a and b together.
[Pure]
public static Fraction operator *(Percent a, Fraction b)
Parameters
Returns
operator -(Fraction, Fraction)
Creates a new Fraction instance by subtracting b from a.
[Pure]
public static Fraction operator -(Fraction a, Fraction b)
Parameters
Returns
operator -(Fraction)
Creates a new Fraction instance by negating the provided f.
[Pure]
public static Fraction operator -(Fraction f)
Parameters
fFractionOperand.