BigRational

Represents a rational number with infinite precision, using BigInteger for both numerator and denominator.

note

Adapted from Microsoft's BigRational, open sourced as MIT on GitHub.

public struct BigRational

Inheritance ObjectValueTypeBigRational
Implements IComparable, IComparable<BigRational>, IEquatable<BigRational>, IToCodeString

Properties​

Zero​

A value representing the number 0.

public static BigRational Zero { get; }

One​

A value representing the number 1.

public static BigRational One { get; }

MinusOne​

A value representing the number -1.

public static BigRational MinusOne { get; }

PlusInfinity​

A value representing the number $+\infty$.

public static BigRational PlusInfinity { get; }

MinusInfinity​

A value representing the number $-\infty$.

public static BigRational MinusInfinity { get; }

Sign​

Returns an integer that indicates the sign of the rational

public int Sign { get; }

Numerator​

The numerator of the rational.

public BigInteger Numerator { get; private set; }

Denominator​

The denominator of the rational.

public BigInteger Denominator { get; private set; }

IsFinite​

True of the number is finite.

public bool IsFinite { get; }

IsInfinite​

True of the number is infinite.

public bool IsInfinite { get; }

IsPlusInfinite​

True of the number is $+\infty$.

public bool IsPlusInfinite { get; }

IsMinusInfinite​

True of the number is $-\infty$.

public bool IsMinusInfinite { get; }

IsZero​

True of the number is 0.

public bool IsZero { get; }

IsPositive​

True of the number is $> 0$.

public bool IsPositive { get; }

IsNegative​

True of the number is $< 0$.

public bool IsNegative { get; }

Constructors​

BigRational(Int32, Int32)​

Constructor.

BigRational(int numerator, int denominator)

Parameters​

numerator Int32

denominator Int32

BigRational(BigInteger)​

Constructor.

BigRational(BigInteger numerator)

Parameters​

numerator BigInteger

BigRational(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 BigRational as 1/10.

BigRational(decimal value)

Parameters​

value Decimal

BigRational(BigInteger, BigInteger)​

Constructor.

BigRational(BigInteger numerator, BigInteger denominator)

Parameters​

numerator BigInteger

denominator BigInteger

BigRational(BigInteger, BigInteger, BigInteger)​

Constructor.

BigRational(BigInteger whole, BigInteger numerator, BigInteger denominator)

Parameters​

whole BigInteger

numerator BigInteger

denominator BigInteger

Methods​

GetWholePart()​

BigInteger GetWholePart()

GetFractionPart()​

BigRational GetFractionPart()

Equals(Object)​

bool Equals(object obj)

Parameters​

obj Object

GetHashCode()​

int GetHashCode()

CompareTo(BigRational)​

int CompareTo(BigRational other)

Parameters​

other BigRational

ToString()​

string ToString()

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

Equals(BigRational)​

bool Equals(BigRational other)

Parameters​

other BigRational

Abs(BigRational)​

The absolute value of the number.

BigRational Abs(BigRational r)

Parameters​

r BigRational

Negate(BigRational)​

The opposite of the number.

BigRational Negate(BigRational r)

Parameters​

r BigRational

Invert(BigRational)​

The inverse of the number.

BigRational Invert(BigRational r)

Parameters​

r BigRational

Returns​

The sum of the two numbers.

BigRational Add(BigRational x, BigRational y)

Parameters​

x BigRational

y BigRational

Subtract(BigRational, BigRational)​

The difference of the two numbers.

BigRational Subtract(BigRational x, BigRational y)

Parameters​

x BigRational

y BigRational

Multiply(BigRational, BigRational)​

The product of the two numbers.

BigRational Multiply(BigRational x, BigRational y)

Parameters​

x BigRational

y BigRational

Divide(BigRational, BigRational)​

The division of the two numbers.

BigRational Divide(BigRational dividend, BigRational divisor)

Parameters​

dividend BigRational

divisor BigRational

Remainder(BigRational, BigRational)​

The remainder of the two numbers.

BigRational Remainder(BigRational dividend, BigRational divisor)

Parameters​

dividend BigRational

divisor BigRational

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

LeastCommonDenominator(BigRational, BigRational)​

The LCD is the least common multiple of the two denominators. For instance, the LCD of $\frac{1}{2}, \frac{1}{4}$ is 4 because the least common multiple of 2 and 4 is 4. Likewise, the LCD of $\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

BigInteger LeastCommonDenominator(BigRational x, BigRational y)

Parameters​

x BigRational

y BigRational

GreatestCommonDivisor(BigRational, BigRational)​

Greatest Common Divisor of the two numbers.

BigRational GreatestCommonDivisor(BigRational a, BigRational b)

Parameters​

a BigRational

b BigRational

LeastCommonMultiple(BigRational, BigRational)​

Least Common Multiple of the two numbers.

BigRational LeastCommonMultiple(BigRational a, BigRational b)

Parameters​

a BigRational

b BigRational

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​

a BigRational

b BigRational

Max(BigRational, BigRational, BigRational)​

Max of the three numbers.

BigRational Max(BigRational a, BigRational b, BigRational c)

Parameters​

a BigRational

b BigRational

c BigRational

Min(BigRational, BigRational)​

Min of the two numbers.

BigRational Min(BigRational a, BigRational b)

Parameters​

a BigRational

b BigRational

Min(BigRational, BigRational, BigRational)​

Min of the three numbers.

BigRational Min(BigRational a, BigRational b, BigRational c)

Parameters​

a BigRational

b BigRational

c BigRational