Rational class

A number that can be expressed as a fraction of two integers, a numerator and a non-zero denominator.

This fraction is stored in its canonical form. The canonical form is the rational number expressed in a unique way as an irreducible fraction a/b, where a and b are coprime integers and b > 0.

Rational(2, 4) corresponding to 2/4 will be created with its canonical form 1/2. That means Rational(2, 4).numerator will be equal to 1 and Rational(2, 4).denominator equal to 2.

Implemented types

Constructors

Rational(BigInt numerator, [BigInt? denominator])
Create a new rational number from its numerator and a non-zero denominator.
factory
Rational.fromInt(int numerator, [int denominator = 1])
Create a new rational number from its numerator and a non-zero denominator.
factory

Properties

denominator BigInt
The denominator of this rational number.
final
hashCode int
The hash code for this object.
no setteroverride
inverse Rational
Returns the Rational denominator/numerator.
no setter
isInteger bool
Returns true if this is an integer.
no setter
numerator BigInt
The numerator of this rational number.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
signum int
The signum function value of this.
no setter

Methods

abs() Rational
Returns the absolute value of this.
ceil() BigInt
Returns the least BigInt value that is no smaller than this Rational.
clamp(Rational lowerLimit, Rational upperLimit) Rational
Clamps this to be in the range lowerLimit-upperLimit.
compareTo(Rational other) int
Compares this object to another object.
override
floor() BigInt
Returns the greatest BigInt value no greater than this Rational.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pow(int exponent) Rational
Returns this to the power of exponent.
remainder(Rational other) Rational
Returns the remainder from dividing this Rational by other.
round() BigInt
Returns the BigInt value closest to this number.
toBigInt() BigInt
The BigInt obtained by discarding any fractional digits from this.
toDouble() double
Returns this as a double.
toString() String
A string representation of this object.
override
truncate() BigInt
The BigInt obtained by discarding any fractional digits from this.

Operators

operator %(Rational other) Rational
Euclidean modulo operator.
operator *(Rational other) Rational
Multiplication operator.
operator +(Rational other) Rational
Addition operator.
operator -(Rational other) Rational
Subtraction operator.
operator /(Rational other) Rational
Division operator.
operator <(Rational other) bool
Whether this number is numerically smaller than other.
operator <=(Rational other) bool
Whether this number is numerically smaller than or equal to other.
operator ==(Object other) bool
The equality operator.
override
operator >(Rational other) bool
Whether this number is numerically greater than other.
operator >=(Rational other) bool
Whether this number is numerically greater than or equal to other.
operator unary-() Rational
Returns the negative value of this rational.
operator ~/(Rational other) BigInt
Truncating division operator.

Static Properties

one Rational
The rational number corresponding to 1.
final
zero Rational
The rational number corresponding to 0.
final

Static Methods

parse(String source) Rational
Parses source as a decimal literal and returns its value as Rational.
tryParse(String source) Rational?
Parses source as a decimal literal and returns its value as Rational.