Table of Contents

Struct Fraction

Namespace
LfrlAnvil.Numerics
Assembly
LfrlAnvil.Core.dll

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 long

Fraction's numerator.

denominator ulong

Fraction'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

Fraction

MaxValue

Represents the maximum fraction value equivalent to MaxValue.

public static readonly Fraction MaxValue

Field Value

Fraction

MinValue

Represents the minimum fraction value equivalent to MinValue.

public static readonly Fraction MinValue

Field Value

Fraction

One

Represents 1/1 fraction.

public static readonly Fraction One

Field Value

Fraction

Zero

Represents 0/1 fraction.

public static readonly Fraction Zero

Field Value

Fraction

Properties

Denominator

Fraction's denominator.

public ulong Denominator { get; }

Property Value

ulong

Numerator

Fraction's numerator.

public long Numerator { get; }

Property Value

long

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 Fraction

Other instance to add.

Returns

Fraction

New Fraction instance.

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 Fraction

An 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 object

An 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)

Creates a new Fraction instance from the provided Fixed.

[Pure]
public static Fraction Create(Fixed value)

Parameters

value Fixed

Value to convert to a fraction.

Returns

Fraction

New Fraction instance.

Remarks

Resulting Denominator will be equal to 10^p, where p represents the Precision of the provided value.

Create(decimal, ulong, MidpointRounding)

Creates a new Fraction instance from the provided decimal and denominator.

[Pure]
public static Fraction Create(decimal value, ulong denominator, MidpointRounding rounding = MidpointRounding.AwayFromZero)

Parameters

value decimal

Value to convert to a fraction.

denominator ulong

Fraction's denominator.

rounding MidpointRounding

Optional value rounding strategy. Equal to AwayFromZero by default.

Returns

Fraction

New Fraction instance.

Exceptions

ArgumentOutOfRangeException

When denominator is equal to 0.

Create(double, ulong, MidpointRounding)

Creates a new Fraction instance from the provided double and denominator.

[Pure]
public static Fraction Create(double value, ulong denominator, MidpointRounding rounding = MidpointRounding.AwayFromZero)

Parameters

value double

Value to convert to a fraction.

denominator ulong

Fraction's denominator.

rounding MidpointRounding

Optional value rounding strategy. Equal to AwayFromZero by default.

Returns

Fraction

New Fraction instance.

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 Fraction

Other instance to divide by.

Returns

Fraction

New Fraction instance.

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 Fraction

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

[Pure]
public override bool Equals(object? obj)

Parameters

obj object

The 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 Fraction

Other instance to divide by.

Returns

Fraction

New Fraction instance.

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 Fraction

Other instance to multiply by.

Returns

Fraction

New Fraction instance.

Multiply(Percent)

Creates a new Fraction instance by multiplying percent and this instance together.

[Pure]
public Fraction Multiply(Percent percent)

Parameters

percent Percent

Percent to multiply by.

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

Fraction

New Fraction instance.

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 ulong

Fraction's denominator.

rounding MidpointRounding

Optional numerator rounding strategy. Equal to AwayFromZero by default.

Returns

Fraction

New Fraction instance.

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 ulong

Fraction's denominator.

Returns

Fraction

New Fraction instance with unchanged Numerator.

Exceptions

ArgumentOutOfRangeException

When value is equal to 0.

SetNumerator(long)

Creates a new Fraction instance with changed Numerator.

[Pure]
public Fraction SetNumerator(long value)

Parameters

value long

Fraction'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

Fraction

New Fraction instance.

Subtract(Fraction)

Creates a new Fraction instance by subtracting other from this instance.

[Pure]
public Fraction Subtract(Fraction other)

Parameters

other Fraction

Other instance to subtract.

Returns

Fraction

New Fraction instance.

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

a Fraction

First operand.

b Fraction

Second operand.

Returns

Fraction

New Fraction instance.

operator --(Fraction)

Creates a new Fraction instance by decrementing the provided f.

[Pure]
public static Fraction operator --(Fraction f)

Parameters

f Fraction

Operand.

Returns

Fraction

New Fraction instance.

operator /(Fraction, Fraction)

Creates a new Fraction instance by dividing a by b.

[Pure]
public static Fraction operator /(Fraction a, Fraction b)

Parameters

a Fraction

First operand.

b Fraction

Second operand.

Returns

Fraction

New Fraction instance.

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

a Fraction

First operand.

b Fraction

Second operand.

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 Fraction

Fraction to convert.

Returns

decimal

New decimal instance.

explicit operator double(Fraction)

Converts the provided f to double.

[Pure]
public static explicit operator double(Fraction f)

Parameters

f Fraction

Fraction to convert.

Returns

double

New double instance.

operator >(Fraction, Fraction)

Checks if a is greater than b.

[Pure]
public static bool operator >(Fraction a, Fraction b)

Parameters

a Fraction

First operand.

b Fraction

Second operand.

Returns

bool

true when a is greater than b, 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

a Fraction

First operand.

b Fraction

Second operand.

Returns

bool

true when a is greater than or equal to b, otherwise false.

implicit operator Fraction(Fixed)

Converts the provided f to Fraction.

[Pure]
public static implicit operator Fraction(Fixed f)

Parameters

f Fixed

Fixed to convert.

Returns

Fraction

New Fraction instance.

operator ++(Fraction)

Creates a new Fraction instance by incrementing the provided f.

[Pure]
public static Fraction operator ++(Fraction f)

Parameters

f Fraction

Operand.

Returns

Fraction

New Fraction instance.

operator !=(Fraction, Fraction)

Checks if a is not equal to b.

[Pure]
public static bool operator !=(Fraction a, Fraction b)

Parameters

a Fraction

First operand.

b Fraction

Second operand.

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

a Fraction

First operand.

b Fraction

Second operand.

Returns

bool

true when a is less than b, 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

a Fraction

First operand.

b Fraction

Second operand.

Returns

bool

true when a is less than or equal to b, 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

a Fraction

First operand.

b Fraction

Second operand.

Returns

Fraction

New Fraction instance.

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

a Fraction

First operand.

b Fraction

Second operand.

Returns

Fraction

New Fraction instance.

operator *(Fraction, Percent)

Creates a new Fraction instance by multiplying a and b together.

[Pure]
public static Fraction operator *(Fraction a, Percent b)

Parameters

a Fraction

First operand.

b Percent

Second operand.

Returns

Fraction

New Fraction instance.

operator *(Percent, Fraction)

Creates a new Fraction instance by multiplying a and b together.

[Pure]
public static Fraction operator *(Percent a, Fraction b)

Parameters

a Percent

First operand.

b Fraction

Second operand.

Returns

Fraction

New Fraction instance.

operator -(Fraction, Fraction)

Creates a new Fraction instance by subtracting b from a.

[Pure]
public static Fraction operator -(Fraction a, Fraction b)

Parameters

a Fraction

First operand.

b Fraction

Second operand.

Returns

Fraction

New Fraction instance.

operator -(Fraction)

Creates a new Fraction instance by negating the provided f.

[Pure]
public static Fraction operator -(Fraction f)

Parameters

f Fraction

Operand.

Returns

Fraction

New Fraction instance.