Skip to main content

LongRational

Represents a rational number with finite precision, using Int64 for both numerator and denominator.

note

Adapted from BigRational with the aim of reducing perfomance impact from the use of BigInteger.

public struct LongRational

Inheritance objectValueTypeLongRational
Implements IComparable, IComparable<LongRational>, IEquatable<LongRational>, IToCodeString, IStableHashCode

Properties

Zero

A value representing the number 0.

public static LongRational Zero { get; }

Property Value

LongRational

One

A value representing the number 1.

public static LongRational One { get; }

Property Value

LongRational

MinusOne

A value representing the number -1.

public static LongRational MinusOne { get; }

Property Value

LongRational

PlusInfinity

A value representing the number ++\infty.

public static LongRational PlusInfinity { get; }

Property Value

LongRational

MinusInfinity

A value representing the number -\infty.

public static LongRational MinusInfinity { get; }

Property Value

LongRational

Sign

Returns an integer that indicates the sign of the rational

public int Sign { get; }

Property Value

int

Numerator

The numerator of the rational.

public long Numerator { get; private set; }

Property Value

long

Denominator

The denominator of the rational.

public long Denominator { get; private set; }

Property Value

long

IsFinite

True if the number is finite.

public bool IsFinite { get; }

Property Value

bool

IsInfinite

True if the number is infinite.

public bool IsInfinite { get; }

Property Value

bool

IsPlusInfinite

True if the number is ++\infty.

public bool IsPlusInfinite { get; }

Property Value

bool

IsMinusInfinite

True if the number is -\infty.

public bool IsMinusInfinite { get; }

Property Value

bool

IsZero

True if the number is 0.

public bool IsZero { get; }

Property Value

bool

IsOne

True if the number is 1.

public bool IsOne { get; }

Property Value

bool

IsPositive

True if the number is >0> 0.

public bool IsPositive { get; }

Property Value

bool

IsNegative

True if the number is <0< 0.

public bool IsNegative { get; }

Property Value

bool

IsInteger

True if the number is an integer.

public bool IsInteger { get; }

Property Value

bool

Constructors

LongRational(int, int)

Constructor.

LongRational(int numerator, int denominator = 1)

Parameters

numerator int
The numerator.

denominator int
The denominator.

Exceptions

UndeterminedResultException
Thrown when the operation cannot be completed.

LongRational(long, long)

Constructor.

LongRational(long numerator, long denominator = 1L)

Parameters

numerator long
The numerator.

denominator long
The denominator.

Exceptions

UndeterminedResultException
Thrown when the operation cannot be completed.

LongRational(long)

Constructor.

LongRational(long numerator)

Parameters

numerator long
The numerator.

LongRational(double)

Constructs a rational from the shortest round-trippable decimal representation of the given double.

LongRational(double value)

Parameters

value double

LongRational(float)

Constructs a rational from the shortest round-trippable decimal representation of the given float.

LongRational(float value)

Parameters

value float

LongRational(decimal)

Constructor.

note

The Decimal type represents floating point numbers exactly, with no rounding error. Values such as 0.1 in Decimal are actually representable, and convert cleanly to LongRational as 1/10.

LongRational(decimal value)

Parameters

value decimal

Methods

GetWholePart()

Divides the rational into its integer and fractional parts. Returns the integer part.

long GetWholePart()

Returns

long

GetFractionPart()

Divides the rational into its integer and fractional parts. Returns the fractional part.

LongRational GetFractionPart()

Returns

LongRational

Floor()

Returns the largest integer less than or equal to the current rational.

long Floor()

Returns

long

Ceil()

Returns the smallest integer greater than or equal to the current rational.

long Ceil()

Returns

long

Equals(object)

Indicates whether the current instance equals a specified object.

bool Equals(object obj)

Parameters

obj object
The object to compare with the current instance.

Returns

bool

GetHashCode()

Returns a hash code for this rational number.

int GetHashCode()

Returns

int

GetStableHashCode()

A stable hashcode.

int GetStableHashCode()

Returns

int

CompareTo(LongRational)

Compares this instance to a specified LongRational and returns an indication of their relative values.

int CompareTo(LongRational other)

Parameters

other LongRational
The rational to compare with this instance.

Returns

int

ToString()

Converts the value of this instance to its equivalent string representation.

string ToString()

Returns

string

ToCodeString(bool, int)

Returns a string containing C# code to create this LongRational. Useful to copy and paste from a debugger into another test or notebook for further investigation.

string ToCodeString(bool formatted = false, int indentation = 0)

Parameters

formatted bool

indentation int

Returns

string

Equals(LongRational)

Indicates whether the current instance equals a specified LongRational

bool Equals(LongRational other)

Parameters

other LongRational
The rational to compare with the current instance.

Returns

bool

Abs(LongRational)

The absolute value of the number.

LongRational Abs(LongRational r)

Parameters

r LongRational

Returns

LongRational

Negate(LongRational)

The opposite of the number.

LongRational Negate(LongRational r)

Parameters

r LongRational

Returns

LongRational

Invert(LongRational)

The inverse of the number.

LongRational Invert(LongRational r)

Parameters

r LongRational

Returns

LongRational

Add(LongRational, LongRational)

The sum of the two numbers.

LongRational Add(LongRational x, LongRational y)

Parameters

x LongRational

y LongRational

Returns

LongRational

Subtract(LongRational, LongRational)

The difference of the two numbers.

LongRational Subtract(LongRational x, LongRational y)

Parameters

x LongRational

y LongRational

Returns

LongRational

Multiply(LongRational, LongRational)

The product of the two numbers.

LongRational Multiply(LongRational x, LongRational y)

Parameters

x LongRational

y LongRational

Returns

LongRational

Divide(LongRational, LongRational)

The division of the two numbers.

LongRational Divide(LongRational dividend, LongRational divisor)

Parameters

dividend LongRational

divisor LongRational

Returns

LongRational

Remainder(LongRational, LongRational)

The remainder of the two numbers.

LongRational Remainder(LongRational dividend, LongRational divisor)

Parameters

dividend LongRational

divisor LongRational

Returns

LongRational

DivRem(LongRational, LongRational, LongRational)

Performs a division with remainder.

LongRational DivRem(LongRational dividend, LongRational divisor, LongRational remainder)

Parameters

dividend LongRational
The dividend.

divisor LongRational
The divisor.

remainder LongRational
The remainder resulting from the division.

Returns

LongRational
The integer result of the division.

Pow(LongRational, long)

Computes the power baseValue^exponent

LongRational Pow(LongRational baseValue, long exponent)

Parameters

baseValue LongRational
The base value.

exponent long
The exponent.

Returns

LongRational

Exceptions

ArgumentException
Thrown when the operation cannot be completed.

LeastCommonDenominator(LongRational, LongRational)

The LCD is the least common multiple of the two denominators. For instance, the LCD of 12,14\frac{1}{2}, \frac{1}{4} is 4 because the least common multiple of 2 and 4 is 4. Likewise, the LCD of 12,13\frac{1}{2}, \frac{1}{3} is 6.

note

To find the LCD: Find the Greatest Common Divisor (GCD) of the denominators Multiply the denominators together Divide the product of the denominators by the GCD

long LeastCommonDenominator(LongRational x, LongRational y)

Parameters

x LongRational

y LongRational

Returns

long

GreatestCommonDivisor(LongRational, LongRational)

Greatest Common Divisor of the two numbers.

LongRational GreatestCommonDivisor(LongRational a, LongRational b)

Parameters

a LongRational
The first operand.

b LongRational
The second operand.

Returns

LongRational

GreatestCommonDivisor(long, long)

Greatest Common Divisor of the two numbers.

long GreatestCommonDivisor(long a, long b)

Parameters

a long
The first operand.

b long
The second operand.

Returns

long

LeastCommonMultiple(LongRational, LongRational)

Least Common Multiple of the two numbers.

LongRational LeastCommonMultiple(LongRational a, LongRational b)

Parameters

a LongRational
The first operand.

b LongRational
The second operand.

Returns

LongRational

Compare(LongRational, LongRational)

Compares two values and returns an integer that indicates whether the first value is less than, equal to, or greater than the second value.

int Compare(LongRational left, LongRational right)

Parameters

left LongRational
The first value to compare.

right LongRational
The second value to compare.

Returns

int
A signed integer that indicates the relative values of and , as shown in the following table. ValueConditionLess than zero is less than .Zero equals .Greater than zero is greater than .

Max(LongRational, LongRational)

Max of the two numbers.

LongRational Max(LongRational a, LongRational b)

Parameters

a LongRational
The first operand.

b LongRational
The second operand.

Returns

LongRational

Max(LongRational, LongRational, LongRational)

Max of the three numbers.

LongRational Max(LongRational a, LongRational b, LongRational c)

Parameters

a LongRational
The first operand.

b LongRational
The second operand.

c LongRational
The third operand.

Returns

LongRational

Max(params LongRational[])

Max of a set of numbers.

LongRational Max(params LongRational[] values)

Parameters

values LongRational[]

Returns

LongRational

Max(IEnumerable<LongRational>)

Max of a set of numbers.

LongRational Max(IEnumerable<LongRational> values)

Parameters

values IEnumerable<LongRational>

Returns

LongRational

Min(LongRational, LongRational)

Min of the two numbers.

LongRational Min(LongRational a, LongRational b)

Parameters

a LongRational
The first operand.

b LongRational
The second operand.

Returns

LongRational

Min(LongRational, LongRational, LongRational)

Min of the three numbers.

LongRational Min(LongRational a, LongRational b, LongRational c)

Parameters

a LongRational
The first operand.

b LongRational
The second operand.

c LongRational
The third operand.

Returns

LongRational

Min(params LongRational[])

Min of a set of numbers.

LongRational Min(params LongRational[] values)

Parameters

values LongRational[]

Returns

LongRational

Min(IEnumerable<LongRational>)

Min of a set of numbers.

LongRational Min(IEnumerable<LongRational> values)

Parameters

values IEnumerable<LongRational>

Returns

LongRational