Rat class

A rational number represented as a fraction of two BigInts.

For efficiency reasons the fraction is not normalized. This means that two Rats with equivalent fractions may not be equal. Use normalize to get a normalized Rat.

Annotations
  • @immutable

Constructors

Rat(BigInt n, [BigInt? m])
Creates a new Rat with the given numerator and denominator.
factory

Properties

d BigInt
The denominator of the fraction.
final
hashCode int
The hash code for this object.
no setteroverride
isInteger bool
Whether this Rat is an integer.
no setter
isNegative bool
Whether this Rat is negative.
no setter
isZero bool
Whether this Rat is zero.
no setter
n BigInt
The numerator of the fraction.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
sign int
Returns the sign of this Rat.
no setter

Methods

abs() Rat
Returns the absolute value of this Rat.
ceil() BigInt
Returns the smallest integer that is greater or equal to this Rat.
floor() BigInt
Returns the biggest integer that is less or equal to this Rat.
normalize() Rat
Returns a new Rat with the smallest denominator for the fraction.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
remainder(Rat other) Rat
Returns the remainder of this Rat divided by other.
round() BigInt
Returns the integer closest to this Rat.
toBigInt() BigInt
Returns this Rat as a BigInt.
toDouble() double
Returns this Rat as a double.
toInt() int
Returns this Rat as an int.
toString() String
A string representation of this object.
override
truncate() BigInt
Discards the fractional part of this Rat.

Operators

operator %(Rat other) Rat
Returns the modulo of this Rat and other.
operator *(Rat other) Rat
Multiplies this Rat by other.
operator +(Rat other) Rat
Adds this Rat to other.
operator -(Rat other) Rat
Subtracts other from this Rat.
operator /(Rat other) Rat
Divides this Rat by other.
operator <(Rat other) bool
Whether this Rat is less than other.
operator <=(Rat other) bool
Whether this Rat is less or equal to other.
operator ==(Object other) bool
The equality operator.
override
operator >(Rat other) bool
Whether this Rat is greater than other.
operator >=(Rat other) bool
Whether this Rat is greater or equal to other.
operator unary-() Rat
Returns the negation of this Rat.
operator ~/(Rat other) BigInt
Returns the truncating division of this Rat by other.

Static Properties

half Rat
The half Rat.
final
one Rat
The one Rat.
final
two Rat
The two Rat.
final
zero Rat
The zero Rat.
final

Static Methods

fromDouble(double n) Rat
Creates a new Rat from a double.
fromInt(int n, [int d = 1]) Rat
Creates a new Rat from two integers (numerator and denominator).
parse(String n) Rat
Parses a Rat from a string.