BigRational
Represents a rational number with infinite precision, using BigInteger for both numerator and denominator.
Adapted from Microsoft's BigRational, open sourced as MIT on GitHub.
public struct BigRational
Inheritance Object → ValueType → BigRational
Implements IComparable, IComparable<BigRational>, IEquatable<BigRational>, IToCodeString
Properties
Zero
A value representing the number 0.
public static BigRational Zero { get; }
Property Value
One
A value representing the number 1.
public static BigRational One { get; }
Property Value
MinusOne
A value representing the number -1.
public static BigRational MinusOne { get; }
Property Value
PlusInfinity
A value representing the number .
public static BigRational PlusInfinity { get; }
Property Value
MinusInfinity
A value representing the number .
public static BigRational 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 BigInteger Numerator { get; private set; }
Property Value
Denominator
The denominator of the rational.
public BigInteger Denominator { get; private set; }
Property Value
IsFinite
True of the number is finite.
public bool IsFinite { get; }
Property Value
IsInfinite
True of the number is infinite.
public bool IsInfinite { get; }
Property Value
IsPlusInfinite
True of the number is .
public bool IsPlusInfinite { get; }
Property Value
IsMinusInfinite
True of the number is .
public bool IsMinusInfinite { get; }
Property Value
IsZero
True of the number is 0.
public bool IsZero { get; }
Property Value
IsPositive
True of the number is .
public bool IsPositive { get; }
Property Value
IsNegative
True of the number is .
public bool IsNegative { get; }
Property Value
Constructors
BigRational(Int32, Int32)
Constructor.
BigRational(int numerator, int denominator)
Parameters
numerator
Int32
denominator
Int32
Exceptions
BigRational(BigInteger)
Constructor.
BigRational(BigInteger numerator)
Parameters
numerator
BigInteger
BigRational(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 BigRational as 1/10.
BigRational(decimal value)
Parameters
value
Decimal
BigRational(BigInteger, BigInteger)
Constructor.
BigRational(BigInteger numerator, BigInteger denominator)
Parameters
numerator
BigInteger
denominator
BigInteger
Exceptions
BigRational(BigInteger, BigInteger, BigInteger)
Constructor.
BigRational(BigInteger whole, BigInteger numerator, BigInteger denominator)
Parameters
whole
BigInteger
numerator
BigInteger
denominator
BigInteger
Exceptions
Methods
GetWholePart()
BigInteger GetWholePart()
Returns
GetFractionPart()
BigRational GetFractionPart()
Returns
Equals(Object)
bool Equals(object obj)
Parameters
obj
Object
Returns
GetHashCode()
int GetHashCode()
Returns
CompareTo(BigRational)
int CompareTo(BigRational other)
Parameters
other
BigRational
Returns
ToString()
string ToString()
Returns
ToCodeString(Boolean, Int32)
Returns a string containing C# code to create this BigRational. Useful to copy and paste from a debugger into another test or notebook for further investigation.
string ToCodeString(bool formatted, int indentation)
Parameters
formatted
Boolean
indentation
Int32
Returns
Equals(BigRational)
bool Equals(BigRational other)
Parameters
other
BigRational
Returns
Abs(BigRational)
The absolute value of the number.
BigRational Abs(BigRational r)
Parameters
Returns
Negate(BigRational)
The opposite of the number.
BigRational Negate(BigRational r)
Parameters
Returns
Invert(BigRational)
The inverse of the number.
BigRational Invert(BigRational r)
Parameters
Returns
Add(BigRational, BigRational)
The sum of the two numbers.
BigRational Add(BigRational x, BigRational y)
Parameters
Returns
Subtract(BigRational, BigRational)
The difference of the two numbers.
BigRational Subtract(BigRational x, BigRational y)
Parameters
Returns
Multiply(BigRational, BigRational)
The product of the two numbers.
BigRational Multiply(BigRational x, BigRational y)
Parameters
Returns
Divide(BigRational, BigRational)
The division of the two numbers.
BigRational Divide(BigRational dividend, BigRational divisor)
Parameters
dividend
BigRational
divisor
BigRational
Returns
Remainder(BigRational, BigRational)
The remainder of the two numbers.
BigRational Remainder(BigRational dividend, BigRational divisor)
Parameters
dividend
BigRational
divisor
BigRational
Returns
DivRem(BigRational, BigRational, out BigRational)
Performs a division with reminder.
BigRational DivRem(BigRational dividend, BigRational divisor, out BigRational remainder)
Parameters
dividend
BigRational
divisor
BigRational
remainder
out BigRational
The reminder resulting from the division.
Returns
BigRational
The integer result of the division.
Pow(BigRational, BigInteger)
Computes the power baseValue
^exponent
BigRational Pow(BigRational baseValue, BigInteger exponent)
Parameters
baseValue
BigRational
exponent
BigInteger
Returns
Exceptions
LeastCommonDenominator(BigRational, BigRational)
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
BigInteger LeastCommonDenominator(BigRational x, BigRational y)
Parameters
Returns
GreatestCommonDivisor(BigRational, BigRational)
Greatest Common Divisor of the two numbers.
BigRational GreatestCommonDivisor(BigRational a, BigRational b)
Parameters
Returns
LeastCommonMultiple(BigRational, BigRational)
Least Common Multiple of the two numbers.
BigRational LeastCommonMultiple(BigRational a, BigRational b)
Parameters
Returns
Compare(BigRational, BigRational)
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(BigRational r1, BigRational r2)
Parameters
r1
BigRational
The first value to compare.
r2
BigRational
The second value to compare.
Returns
Int32
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(BigRational, BigRational)
Max of the two numbers.
BigRational Max(BigRational a, BigRational b)
Parameters
Returns
Max(BigRational, BigRational, BigRational)
Max of the three numbers.
BigRational Max(BigRational a, BigRational b, BigRational c)
Parameters
Returns
Min(BigRational, BigRational)
Min of the two numbers.
BigRational Min(BigRational a, BigRational b)
Parameters
Returns
Min(BigRational, BigRational, BigRational)
Min of the three numbers.
BigRational Min(BigRational a, BigRational b, BigRational c)