Skip to main content

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, IStableHashCode

Properties

Zero

A value representing the number 0.

public static BigRational Zero { get; }

Property Value

BigRational

One

A value representing the number 1.

public static BigRational One { get; }

Property Value

BigRational

MinusOne

A value representing the number -1.

public static BigRational MinusOne { get; }

Property Value

BigRational

PlusInfinity

A value representing the number ++\infty.

public static BigRational PlusInfinity { get; }

Property Value

BigRational

MinusInfinity

A value representing the number -\infty.

public static BigRational MinusInfinity { get; }

Property Value

BigRational

Sign

Returns an integer that indicates the sign of the rational

public int Sign { get; }

Property Value

Int32

Numerator

The numerator of the rational.

public BigInteger Numerator { get; private set; }

Property Value

BigInteger

Denominator

The denominator of the rational.

public BigInteger Denominator { get; private set; }

Property Value

BigInteger

IsFinite

True of the number is finite.

public bool IsFinite { get; }

Property Value

Boolean

IsInfinite

True of the number is infinite.

public bool IsInfinite { get; }

Property Value

Boolean

IsPlusInfinite

True of the number is ++\infty.

public bool IsPlusInfinite { get; }

Property Value

Boolean

IsMinusInfinite

True of the number is -\infty.

public bool IsMinusInfinite { get; }

Property Value

Boolean

IsZero

True of the number is 0.

public bool IsZero { get; }

Property Value

Boolean

IsPositive

True of the number is >0> 0.

public bool IsPositive { get; }

Property Value

Boolean

IsNegative

True of the number is <0< 0.

public bool IsNegative { get; }

Property Value

Boolean

Constructors

BigRational(Int32, Int32)

Constructor.

BigRational(int numerator, int denominator)

Parameters

numerator Int32

denominator Int32

Exceptions

UndeterminedResultException

BigRational(Int64, Int64)

Constructor.

BigRational(long numerator, long denominator)

Parameters

numerator Int64

denominator Int64

Exceptions

UndeterminedResultException

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

Exceptions

UndeterminedResultException

BigRational(BigInteger, BigInteger, BigInteger)

Constructor.

BigRational(BigInteger whole, BigInteger numerator, BigInteger denominator)

Parameters

whole BigInteger

numerator BigInteger

denominator BigInteger

Exceptions

DivideByZeroException

Methods

GetWholePart()

BigInteger GetWholePart()

Returns

BigInteger

GetFractionPart()

BigRational GetFractionPart()

Returns

BigRational

Equals(Object)

bool Equals(object obj)

Parameters

obj Object

Returns

Boolean

GetHashCode()

int GetHashCode()

Returns

Int32

GetStableHashCode()

A stable hashcode.

int GetStableHashCode()

Returns

Int32

CompareTo(BigRational)

int CompareTo(BigRational other)

Parameters

other BigRational

Returns

Int32

ToString()

string ToString()

Returns

String

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

String

Equals(BigRational)

bool Equals(BigRational other)

Parameters

other BigRational

Returns

Boolean

Abs(BigRational)

The absolute value of the number.

BigRational Abs(BigRational r)

Parameters

r BigRational

Returns

BigRational

Negate(BigRational)

The opposite of the number.

BigRational Negate(BigRational r)

Parameters

r BigRational

Returns

BigRational

Invert(BigRational)

The inverse of the number.

BigRational Invert(BigRational r)

Parameters

r BigRational

Returns

BigRational

Add(BigRational, BigRational)

The sum of the two numbers.

BigRational Add(BigRational x, BigRational y)

Parameters

x BigRational

y BigRational

Returns

BigRational

Subtract(BigRational, BigRational)

The difference of the two numbers.

BigRational Subtract(BigRational x, BigRational y)

Parameters

x BigRational

y BigRational

Returns

BigRational

Multiply(BigRational, BigRational)

The product of the two numbers.

BigRational Multiply(BigRational x, BigRational y)

Parameters

x BigRational

y BigRational

Returns

BigRational

Divide(BigRational, BigRational)

The division of the two numbers.

BigRational Divide(BigRational dividend, BigRational divisor)

Parameters

dividend BigRational

divisor BigRational

Returns

BigRational

Remainder(BigRational, BigRational)

The remainder of the two numbers.

BigRational Remainder(BigRational dividend, BigRational divisor)

Parameters

dividend BigRational

divisor BigRational

Returns

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

Returns

BigRational

Exceptions

ArgumentException

LeastCommonDenominator(BigRational, BigRational)

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

BigInteger LeastCommonDenominator(BigRational x, BigRational y)

Parameters

x BigRational

y BigRational

Returns

BigInteger

GreatestCommonDivisor(BigRational, BigRational)

Greatest Common Divisor of the two numbers.

BigRational GreatestCommonDivisor(BigRational a, BigRational b)

Parameters

a BigRational

b BigRational

Returns

BigRational

LeastCommonMultiple(BigRational, BigRational)

Least Common Multiple of the two numbers.

BigRational LeastCommonMultiple(BigRational a, BigRational b)

Parameters

a BigRational

b BigRational

Returns

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

Returns

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

Returns

BigRational

Min(BigRational, BigRational)

Min of the two numbers.

BigRational Min(BigRational a, BigRational b)

Parameters

a BigRational

b BigRational

Returns

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

Returns

BigRational