LongRational
Represents a rational number with finite precision, using Int64 for both numerator and denominator.
Adapted from BigRational with the aim of reducing perfomance impact from the use of BigInteger.
public struct LongRational
Inheritance object → ValueType → LongRational
Implements IComparable, IComparable<LongRational>, IEquatable<LongRational>, IToCodeString, IStableHashCode
Properties
Zero
A value representing the number 0.
public static LongRational Zero { get; }
Property Value
One
A value representing the number 1.
public static LongRational One { get; }
Property Value
MinusOne
A value representing the number -1.
public static LongRational MinusOne { get; }
Property Value
PlusInfinity
A value representing the number .
public static LongRational PlusInfinity { get; }
Property Value
MinusInfinity
A value representing the number .
public static LongRational MinusInfinity { get; }
Property Value
Sign
Returns an integer that indicates the sign of the rational
public int Sign { get; }
Property Value
Numerator
The numerator of the rational.
public long Numerator { get; private set; }
Property Value
Denominator
The denominator of the rational.
public long Denominator { get; private set; }
Property Value
IsFinite
True if the number is finite.
public bool IsFinite { get; }
Property Value
IsInfinite
True if the number is infinite.
public bool IsInfinite { get; }
Property Value
IsPlusInfinite
True if the number is .
public bool IsPlusInfinite { get; }
Property Value
IsMinusInfinite
True if the number is .
public bool IsMinusInfinite { get; }
Property Value
IsZero
True if the number is 0.
public bool IsZero { get; }
Property Value
IsOne
True if the number is 1.
public bool IsOne { get; }
Property Value
IsPositive
True if the number is .
public bool IsPositive { get; }
Property Value
IsNegative
True if the number is .
public bool IsNegative { get; }
Property Value
IsInteger
True if the number is an integer.
public bool IsInteger { get; }
Property Value
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.
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
GetFractionPart()
Divides the rational into its integer and fractional parts. Returns the fractional part.
LongRational GetFractionPart()
Returns
Floor()
Returns the largest integer less than or equal to the current rational.
long Floor()
Returns
Ceil()
Returns the smallest integer greater than or equal to the current rational.
long Ceil()
Returns
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
GetHashCode()
Returns a hash code for this rational number.
int GetHashCode()
Returns
GetStableHashCode()
A stable hashcode.
int GetStableHashCode()
Returns
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
ToString()
Converts the value of this instance to its equivalent string representation.
string ToString()
Returns
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
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
Abs(LongRational)
The absolute value of the number.
LongRational Abs(LongRational r)
Parameters
Returns
Negate(LongRational)
The opposite of the number.
LongRational Negate(LongRational r)
Parameters
Returns
Invert(LongRational)
The inverse of the number.
LongRational Invert(LongRational r)
Parameters
Returns
Add(LongRational, LongRational)
The sum of the two numbers.
LongRational Add(LongRational x, LongRational y)
Parameters
Returns
Subtract(LongRational, LongRational)
The difference of the two numbers.
LongRational Subtract(LongRational x, LongRational y)
Parameters
Returns
Multiply(LongRational, LongRational)
The product of the two numbers.
LongRational Multiply(LongRational x, LongRational y)
Parameters
Returns
Divide(LongRational, LongRational)
The division of the two numbers.
LongRational Divide(LongRational dividend, LongRational divisor)
Parameters
dividend LongRational
divisor LongRational
Returns
Remainder(LongRational, LongRational)
The remainder of the two numbers.
LongRational Remainder(LongRational dividend, LongRational divisor)
Parameters
dividend LongRational
divisor LongRational
Returns
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
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 is 4 because the least common multiple of 2 and 4 is 4. Likewise, the LCD of is 6.
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
Returns
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
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
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
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
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
Max(params LongRational[])
Max of a set of numbers.
LongRational Max(params LongRational[] values)
Parameters
values LongRational[]
Returns
Max(IEnumerable<LongRational>)
Max of a set of numbers.
LongRational Max(IEnumerable<LongRational> values)
Parameters
values IEnumerable<LongRational>
Returns
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
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
Min(params LongRational[])
Min of a set of numbers.
LongRational Min(params LongRational[] values)
Parameters
values LongRational[]
Returns
Min(IEnumerable<LongRational>)
Min of a set of numbers.
LongRational Min(IEnumerable<LongRational> values)
Parameters
values IEnumerable<LongRational>