3using System.Reflection;
17 private BigInteger numerator;
18 private BigInteger denominator;
28 throw new DivideByZeroException();
48 this.numerator = Number;
49 this.denominator = BigInteger.One;
57 get => this.numerator;
58 set => this.numerator = value;
66 get => this.denominator;
67 set => this.denominator = value;
99 if (Obj is BigInteger i)
105 if (Obj is Complex z)
117 return ((
double)this.numerator) / ((double)this.denominator);
128 if (
Left.numerator.IsZero ||
Right.numerator.IsZero)
131 BigInteger gcd1 = BigInteger.GreatestCommonDivisor(
Left.numerator,
Right.denominator);
132 BigInteger gcd2 = BigInteger.GreatestCommonDivisor(
Right.numerator,
Left.denominator);
134 BigInteger n = BigInteger.Divide(
Left.numerator, gcd1) * BigInteger.Divide(
Right.numerator, gcd2);
135 BigInteger d = BigInteger.Divide(
Left.denominator, gcd2) * BigInteger.Divide(
Right.denominator, gcd1);
137 return OperationResult(n, d);
155 return new RationalNumber(n, d);
166 BigInteger gcd2 = BigInteger.GreatestCommonDivisor(
Right,
Left.denominator);
168 BigInteger n = Left.numerator * BigInteger.Divide(
Right, gcd2);
169 BigInteger d = BigInteger.Divide(
Left.denominator, gcd2);
171 return OperationResult(n, d);
224 if (this.numerator.IsZero)
225 throw new DivideByZeroException();
227 return OperationResult(this.denominator, this.numerator);
242 if (Obj is BigInteger i)
248 if (Obj is Complex z)
262 BigInteger n = Left.numerator * Right.denominator + Right.numerator *
Left.denominator;
263 BigInteger d = Right.denominator *
Left.denominator;
265 BigInteger gcd = BigInteger.GreatestCommonDivisor(n, d);
267 return OperationResult(BigInteger.Divide(n, gcd), BigInteger.Divide(d, gcd));
278 BigInteger n = Left.numerator +
Right *
Left.denominator;
279 BigInteger gcd = BigInteger.GreatestCommonDivisor(n,
Left.denominator);
281 return OperationResult(BigInteger.Divide(n, gcd), BigInteger.Divide(
Left.denominator, gcd));
345 if (this.numerator.IsZero)
346 return Q.numerator.IsZero;
348 return this.numerator * Q.denominator == Q.numerator * this.denominator;
351 object Obj = E.AssociatedObjectValue;
353 if (Obj is BigInteger i)
354 return this.numerator == i * this.denominator;
359 if (Obj is Complex z)
368 int Result = this.numerator.GetHashCode();
369 Result ^= Result << 5 ^ this.denominator.GetHashCode();
391 if (DesiredType.GetTypeInfo().IsAssignableFrom(typeof(
RationalNumber).GetTypeInfo()))
400 if (DesiredType == typeof(
byte))
402 if (d >=
byte.MinValue && d <=
byte.MaxValue)
408 else if (DesiredType == typeof(decimal))
413 else if (DesiredType == typeof(
double))
418 else if (DesiredType == typeof(
short))
420 if (d >=
short.MinValue && d <=
short.MaxValue)
426 else if (DesiredType == typeof(
int))
428 if (d >=
int.MinValue && d <=
int.MaxValue)
434 else if (DesiredType == typeof(
long))
436 if (d >=
long.MinValue && d <=
long.MaxValue)
442 else if (DesiredType == typeof(sbyte))
444 if (d >= sbyte.MinValue && d <= sbyte.MaxValue)
450 else if (DesiredType == typeof(
float))
455 else if (DesiredType == typeof(ushort))
457 if (d >= ushort.MinValue && d <= ushort.MaxValue)
463 else if (DesiredType == typeof(uint))
465 if (d >= uint.MinValue && d <= uint.MaxValue)
471 else if (DesiredType == typeof(ulong))
473 if (d >= ulong.MinValue && d <= ulong.MaxValue)
Base class for all types of elements.
abstract object AssociatedObjectValue
Associated object value.
Base class for all types of field elements.
Class managing a script expression.
static bool TryConvert(object Value, Type DesiredType, out object Result)
Tries to convert an object Value to an object of type DesiredType .
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
override bool TryConvertTo(Type DesiredType, out object Value)
Converts the value to a .NET type.
override int GetHashCode()
Calculates a hash code of the element. Hash code.
override IRingElement Invert()
Inverts the element, if possible.
static ICommutativeRingWithIdentityElement operator/(RationalNumber Left, RationalNumber Right)
Divides a rational number with another rational number.
override ICommutativeRingWithIdentityElement One
Returns the identity element of the commutative ring with identity.
override IAbelianGroupElement Add(IAbelianGroupElement Element)
Tries to add an element to the current element.
static ICommutativeRingWithIdentityElement operator*(RationalNumber Left, RationalNumber Right)
Multiplies a rational number with another rational number.
BigInteger Numerator
Numerator.
static ICommutativeRingWithIdentityElement operator+(RationalNumber Left, RationalNumber Right)
Adds a rational number with another rational number.
override IField AssociatedField
Associated Field.
RationalNumber(BigInteger Number)
RationalNumber-valued number.
override bool Equals(object obj)
Compares the element to another. If elements are equal.
override object AssociatedObjectValue
Associated object value.
override ICommutativeRingElement Multiply(ICommutativeRingElement Element)
Tries to multiply an element to the current element.
override string ToString()
BigInteger Denominator
Denominator
override IAbelianGroupElement Zero
Returns the zero element of the group.
double ToDouble()
Converts rational number to a double-precision floating-point number.
RationalNumber(BigInteger Numerator, BigInteger Denominator)
Rational Number.
override IGroupElement Negate()
Negates the element.
static ICommutativeRingWithIdentityElement operator-(RationalNumber Left, RationalNumber Right)
Subtracts a rational number with another rational number.
Field of rational numbers.
Basic interface for all types of abelian group elements.
Basic interface for all types of commutative ring elements.
Basic interface for all types of commutative ring with identity elements.
Basic interface for all types of elements.
Basic interface for all types of group elements.
Basic interface for all types of ring elements.
Basic interface for all types of fields.