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

Int32

Numerator

The numerator of the rational.

public long Numerator { get; private set; }

Property Value

Int64

Denominator

The denominator of the rational.

public long Denominator { get; private set; }

Property Value

Int64

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

LongRational(Int64, Int64)

Constructor.

LongRational(long numerator, long denominator)

Parameters

numerator Int64

denominator Int64

Exceptions

UndeterminedResultException

LongRational(Int64)

Constructor.

LongRational(long numerator)

Parameters

numerator Int64

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

LongRational(decimal value)

Parameters

value Decimal

Methods

GetWholePart()

long GetWholePart()

Returns

Int64

GetFractionPart()

LongRational GetFractionPart()

Returns

LongRational

Floor()

long Floor()

Returns

Int64

Ceil()

long Ceil()

Returns

Int64

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(LongRational)

int CompareTo(LongRational other)

Parameters

other LongRational

Returns

Int32

ToString()

string ToString()

Returns

String

ToCodeString(Boolean, Int32)

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, int indentation)

Parameters

formatted Boolean

indentation Int32

Returns

String

Equals(LongRational)

bool Equals(LongRational other)

Parameters

other LongRational

Returns

Boolean

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, out LongRational)

Performs a division with reminder.

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

Parameters

dividend LongRational

divisor LongRational

remainder out LongRational
The reminder resulting from the division.

Returns

LongRational
The integer result of the division.

Pow(LongRational, Int64)

Computes the power baseValue^exponent

LongRational Pow(LongRational baseValue, long exponent)

Parameters

baseValue LongRational

exponent Int64

Returns

LongRational

Exceptions

ArgumentException

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

Int64

GreatestCommonDivisor(LongRational, LongRational)

Greatest Common Divisor of the two numbers.

LongRational GreatestCommonDivisor(LongRational a, LongRational b)

Parameters

a LongRational

b LongRational

Returns

LongRational

GreatestCommonDivisor(Int64, Int64)

Greatest Common Divisor of the two numbers.

long GreatestCommonDivisor(long a, long b)

Parameters

a Int64

b Int64

Returns

Int64

LeastCommonMultiple(LongRational, LongRational)

Least Common Multiple of the two numbers.

LongRational LeastCommonMultiple(LongRational a, LongRational b)

Parameters

a LongRational

b LongRational

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 r1, LongRational r2)

Parameters

r1 LongRational
The first value to compare.

r2 LongRational
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(LongRational, LongRational)

Max of the two numbers.

LongRational Max(LongRational a, LongRational b)

Parameters

a LongRational

b LongRational

Returns

LongRational

Max(LongRational, LongRational, LongRational)

Max of the three numbers.

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

Parameters

a LongRational

b LongRational

c LongRational

Returns

LongRational

Min(LongRational, LongRational)

Min of the two numbers.

LongRational Min(LongRational a, LongRational b)

Parameters

a LongRational

b LongRational

Returns

LongRational

Min(LongRational, LongRational, LongRational)

Min of the three numbers.

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

Parameters

a LongRational

b LongRational

c LongRational

Returns

LongRational