Class Rational
Representation of a fraction
public class Rational
- Inheritance
-
Rational
- Inherited Members
Constructors
Rational()
Constructs new fraction
public Rational()
Rational(double)
Constructs a new fraction by approximating a double value.
public Rational(double a)
Parameters
adoubleThe double value to approximate as a fraction.
Rational(int)
Constructs a new fraction from an integer value with denominator 1.
public Rational(int num)
Parameters
numintThe integer value to represent as a fraction.
Rational(int, int)
Constructs new fraction with specified numerator and denominator
public Rational(int num, int den)
Parameters
Rational(long, long)
Constructs new fraction with specified numerator and denominator
public Rational(long num, long den)
Parameters
Properties
Den
Gets or sets the denominator of the fraction.
public int Den { get; set; }
Property Value
Num
Gets or sets the numerator of the fraction.
public int Num { get; set; }
Property Value
Methods
Clone()
Creates a copy of this fraction.
public Rational Clone()
Returns
- Rational
A new Rational instance with the same values.
Equals(object)
Determines whether this fraction equals another object.
public override bool Equals(object obj)
Parameters
objobjectThe object to compare with.
Returns
- bool
True if the objects are equal; otherwise, false.
Equals(Rational, double)
Determines whether this fraction equals another within a specified precision.
public bool Equals(Rational right, double precision)
Parameters
Returns
- bool
True if the difference is within precision; otherwise, false.
GCD(int, int)
Calculates the greatest common divisor of two integers.
public static int GCD(int a, int b)
Parameters
Returns
- int
The greatest common divisor.
GCD(long, long)
Calculates the greatest common divisor of two long integers.
public static long GCD(long a, long b)
Parameters
Returns
- long
The greatest common divisor.
GetHashCode()
Returns a hash code for this fraction.
public override int GetHashCode()
Returns
- int
A hash code for this instance.
IsNull()
Determines whether this fraction represents a null or zero value.
public bool IsNull()
Returns
- bool
True if numerator or denominator is zero; otherwise, false.
Parse(string)
Parses a string representation of a fraction.
public static Rational Parse(string value)
Parameters
valuestringThe string to parse (e.g., "30/1" or "30000:1001").
Returns
- Rational
A new Rational representing the parsed value.
ToDouble()
Converts this fraction to a double value.
public double ToDouble()
Returns
- double
The fraction as a double, or 0.0 if the denominator is zero.
ToString()
Returns a string representation of this fraction.
public override string ToString()
Returns
- string
A string in the format "numerator/denominator".
ToString(string)
Returns a string representation of this fraction using a custom delimiter.
public string ToString(string delimiter)
Parameters
delimiterstringThe delimiter to use between numerator and denominator.
Returns
- string
A string in the format "numerator[delimiter]denominator".
Operators
operator /(Rational, Rational)
Divides the left fraction by the right fraction.
public static Rational operator /(Rational left, Rational right)
Parameters
Returns
- Rational
The quotient of the two fractions.
operator ==(Rational, Rational)
Determines whether two fractions are equal.
public static bool operator ==(Rational left, Rational right)
Parameters
Returns
- bool
True if the fractions are equal; otherwise, false.
operator >(Rational, Rational)
Determines whether the left fraction is greater than the right fraction.
public static bool operator >(Rational left, Rational right)
Parameters
Returns
- bool
True if left is greater than right; otherwise, false.
operator >=(Rational, Rational)
Determines whether the left fraction is greater than or equal to the right fraction.
public static bool operator >=(Rational left, Rational right)
Parameters
Returns
- bool
True if left is greater than or equal to right; otherwise, false.
operator !=(Rational, Rational)
Determines whether two fractions are not equal.
public static bool operator !=(Rational left, Rational right)
Parameters
Returns
- bool
True if the fractions are not equal; otherwise, false.
operator <(Rational, Rational)
Determines whether the left fraction is less than the right fraction.
public static bool operator <(Rational left, Rational right)
Parameters
Returns
- bool
True if left is less than right; otherwise, false.
operator <=(Rational, Rational)
Determines whether the left fraction is less than or equal to the right fraction.
public static bool operator <=(Rational left, Rational right)
Parameters
Returns
- bool
True if left is less than or equal to right; otherwise, false.
operator !(Rational)
Returns the reciprocal of a fraction (inverts numerator and denominator).
public static Rational operator !(Rational right)
Parameters
rightRationalThe fraction to invert.
Returns
- Rational
The reciprocal of the fraction.
operator *(Rational, Rational)
Multiplies two fractions.
public static Rational operator *(Rational left, Rational right)
Parameters
Returns
- Rational
The product of the two fractions.