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
numerator
longFraction's numerator.
denominator
ulongFraction's denominator. Equal to 1 by default.
Exceptions
- ArgumentOutOfRangeException
When
denominator
is 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
other
FractionOther 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
other
FractionAn 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 other
in the sort order.Zero This instance occurs in the same position in the sort order as other
.Greater than zero This instance follows other
in 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
obj
objectAn 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 obj
in the sort order.Zero This instance occurs in the same position in the sort order as obj
.Greater than zero This instance follows obj
in the sort order.
Exceptions
- ArgumentException
obj
is not the same type as this instance.
Create(Fixed)
[Pure]
public static Fraction Create(Fixed value)
Parameters
value
FixedValue 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
value
decimalValue to convert to a fraction.
denominator
ulongFraction's denominator.
rounding
MidpointRoundingOptional
value
rounding strategy. Equal to AwayFromZero by default.
Returns
Exceptions
- ArgumentOutOfRangeException
When
denominator
is equal to 0.
Create(double, ulong, MidpointRounding)
[Pure]
public static Fraction Create(double value, ulong denominator, MidpointRounding rounding = MidpointRounding.AwayFromZero)
Parameters
value
doubleValue to convert to a fraction.
denominator
ulongFraction's denominator.
rounding
MidpointRoundingOptional
value
rounding strategy. Equal to AwayFromZero by default.
Returns
Exceptions
- ArgumentOutOfRangeException
When
denominator
is 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
other
FractionOther instance to divide by.
Returns
Exceptions
- DivideByZeroException
When
other
is 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
other
FractionAn 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
obj
objectThe object to compare with the current instance.
Returns
- bool
true if
obj
and 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
other
FractionOther instance to divide by.
Returns
Exceptions
- DivideByZeroException
When
other
is equal to 0.
Multiply(Fraction)
Creates a new Fraction instance by multiplying other
and this instance together.
[Pure]
public Fraction Multiply(Fraction other)
Parameters
other
FractionOther 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
denominator
ulongFraction's denominator.
rounding
MidpointRoundingOptional numerator rounding strategy. Equal to AwayFromZero by default.
Returns
Remarks
See Round(decimal, MidpointRounding) for more information.
Exceptions
- ArgumentOutOfRangeException
When
denominator
is equal to 0.
SetDenominator(ulong)
Creates a new Fraction instance with changed Denominator.
[Pure]
public Fraction SetDenominator(ulong value)
Parameters
value
ulongFraction's denominator.
Returns
Exceptions
- ArgumentOutOfRangeException
When
value
is equal to 0.
SetNumerator(long)
[Pure]
public Fraction SetNumerator(long value)
Parameters
value
longFraction'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
other
FractionOther 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
f
FractionOperand.
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
b
is 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
f
FractionFraction to convert.
Returns
explicit operator double(Fraction)
Converts the provided f
to double.
[Pure]
public static explicit operator double(Fraction f)
Parameters
f
FractionFraction 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
a
is 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
a
is 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
f
FractionOperand.
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
a
is 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
a
is 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
b
is 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
f
FractionOperand.