case class Rational(num: Long, den: Long = 1L) extends Fractional[Rational] with Ordered[Rational] with Product with Serializable
The Rational
class is used to represent and operate on rational numbers.
Internally, a rational number is represented as two long integers.
Externally, two forms are supported:
a/b = 2/3 via: Rational ("2/3"), 'toString' (a, b) = (2, 3) via: create ("(2, 3)") 'toString2'
Rational number can be created without loss of precision using the constructor, 'apply', 'create' or 'fromBigDecimal' methods. Other methods may lose precision.
- num
the numerator (e.g., 2)
- den
the denominator (e.g., 3)
- Alphabetic
- By Inheritance
- Rational
- Product
- Equals
- Ordered
- Comparable
- Fractional
- Numeric
- Ordering
- PartialOrdering
- Equiv
- Serializable
- Comparator
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Instance Constructors
- new Rational(num: Long, den: Long = 1L)
- num
the numerator (e.g., 2)
- den
the denominator (e.g., 3)
Type Members
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def !=~(q: Rational): Boolean
Return whether two rational numbers are not nearly equal.
Return whether two rational numbers are not nearly equal.
- q
the compare 'this' with q
- final def ##: Int
- Definition Classes
- AnyRef → Any
- def *(l: Long): Rational
Multiply a rational number times a long.
Multiply a rational number times a long.
- l
multiply this times long l
- def *(q: Rational): Rational
Multiply two rational numbers 'this * q'.
Multiply two rational numbers 'this * q'.
- q
multiply this times rational q
- def +(l: Long): Rational
Add a rational number plus a long.
Add a rational number plus a long.
- l
add long l to this
- def +(q: Rational): Rational
Add two rational numbers 'this + q'.
Add two rational numbers 'this + q'.
- q
add rational q to this
- def -(l: Long): Rational
Subtract: a rational number minus a long.
Subtract: a rational number minus a long.
- l
subtract long l from this
- def -(q: Rational): Rational
Subtract two rational numbers 'this - q'.
Subtract two rational numbers 'this - q'.
- q
subtract rational q from this
- def /(l: Long): Rational
Divide a rational number div a long.
Divide a rational number div a long.
- l
divide this by long l
- def /(q: Rational): Rational
Divide two rational numbers 'this / q'.
Divide two rational numbers 'this / q'.
- q
divide this by rational q
- def <(that: Rational): Boolean
- Definition Classes
- Ordered
- def <=(that: Rational): Boolean
- Definition Classes
- Ordered
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def =~(q: Rational): Boolean
Return whether two rational numbers are nearly equal.
Return whether two rational numbers are nearly equal.
- q
the compare 'this' with q
- def >(that: Rational): Boolean
- Definition Classes
- Ordered
- def >=(that: Rational): Boolean
- Definition Classes
- Ordered
- def abs: Rational
Return the absolute value of this rational number.
- def abs(x: Rational): Rational
- Definition Classes
- Numeric
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
- def compare(q: Rational): Int
Compare this rational number with that rational number 'q'.
Compare this rational number with that rational number 'q'.
- q
that rational number
- Definition Classes
- Rational → Ordered
- def compare(q: Rational, p: Rational): Int
Compare two rational numbers (negative for <, zero for ==, positive for >).
Compare two rational numbers (negative for <, zero for ==, positive for >).
- q
the first rational number to compare
- p
the second rational number to compare
- Definition Classes
- Rational → Ordering → Comparator
- def compareTo(that: Rational): Int
- Definition Classes
- Ordered → Comparable
- val den: Long
- def div(q: Rational, l: Long): Rational
- def div(q: Rational, p: Rational): Rational
- Definition Classes
- Rational → Fractional
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(c: Any): Boolean
Override equals to determine whether this rational number equals rational 'c'.
Override equals to determine whether this rational number equals rational 'c'.
- c
the rational number to compare with this
- Definition Classes
- Rational → Equals → Comparator → AnyRef → Any
- def equiv(x: Rational, y: Rational): Boolean
- Definition Classes
- Ordering → PartialOrdering → Equiv
- def fromBigDecimal(y: BigDecimal): Rational
Create a rational number from a
BigDecimal
number.Create a rational number from a
BigDecimal
number.- y
the
BigDecimal
used to create the rational number
- def fromInt(n: Int): Rational
Create a rational number from an
Int
.Create a rational number from an
Int
.- n
the
Int
used to create the rational number
- Definition Classes
- Rational → Numeric
- def fromLong(n: Long): Rational
Create a rational number from a
Long
.Create a rational number from a
Long
.- n
the
Long
used to create the rational number
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def gt(x: Rational, y: Rational): Boolean
- Definition Classes
- Ordering → PartialOrdering
- def gteq(x: Rational, y: Rational): Boolean
- Definition Classes
- Ordering → PartialOrdering
- def hashCode(): Int
Must also override hashCode to be be compatible with equals.
Must also override hashCode to be be compatible with equals.
- Definition Classes
- Rational → AnyRef → Any
- def in(set: Set[Rational]): Boolean
Determine whether 'this' is in the given set.
- def in(lim: (Rational, Rational)): Boolean
Determine whether 'this' is within the given bounds
Determine whether 'this' is within the given bounds
- lim
the given (lower, upper) bounds
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def isIntegral: Boolean
Determine whether this rational number is integral.
- def isReverseOf(other: Ordering[_]): Boolean
- Definition Classes
- Ordering
- def lt(x: Rational, y: Rational): Boolean
- Definition Classes
- Ordering → PartialOrdering
- def lteq(x: Rational, y: Rational): Boolean
- Definition Classes
- Ordering → PartialOrdering
- def max(q: Rational): Rational
Return the maximum of this and that rational numbers.
Return the maximum of this and that rational numbers.
- q
that rational number to compare with this
- def max[U <: Rational](x: U, y: U): U
- Definition Classes
- Ordering
- def min(q: Rational): Rational
Return the minimum of this and that rational numbers.
Return the minimum of this and that rational numbers.
- q
that rational number to compare with this
- def min[U <: Rational](x: U, y: U): U
- Definition Classes
- Ordering
- def minus(q: Rational, l: Long): Rational
- def minus(q: Rational, p: Rational): Rational
- Definition Classes
- Rational → Numeric
- implicit def mkNumericOps(lhs: Rational): FractionalOps
- Definition Classes
- Fractional → Numeric
- implicit def mkOrderingOps(lhs: Rational): OrderingOps
- Definition Classes
- Ordering
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def near_eq(q: Rational, p: Rational): Boolean
- def negate(q: Rational): Rational
- Definition Classes
- Rational → Numeric
- def not_in(set: Set[Rational]): Boolean
Determine whether 'this' is not in the given set.
- def not_in(lim: (Rational, Rational)): Boolean
Determine whether 'this' is not within the given bounds
Determine whether 'this' is not within the given bounds
- lim
the given (lower, upper) bounds
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- val num: Long
- def on[U](f: (U) => Rational): Ordering[U]
- Definition Classes
- Ordering
- def one: Rational
- Definition Classes
- Numeric
- def orElse(other: Ordering[Rational]): Ordering[Rational]
- Definition Classes
- Ordering
- def orElseBy[S](f: (Rational) => S)(implicit ord: Ordering[S]): Ordering[Rational]
- Definition Classes
- Ordering
- def parseString(str: String): Option[Rational]
Parse the string to create a rational number.
Parse the string to create a rational number.
- Definition Classes
- Rational → Numeric
- def plus(q: Rational, l: Long): Rational
- def plus(q: Rational, p: Rational): Rational
- Definition Classes
- Rational → Numeric
- def pow(q: Rational, l: Long): Rational
- def pow(q: Rational, p: Rational): Rational
- def productElementNames: Iterator[String]
- Definition Classes
- Product
- def reduce(): Rational
Reduce the magnitude of the numerator and denonimator by dividing both by their Greatest Common Divisor (GCD).
- def reverse: Ordering[Rational]
- Definition Classes
- Ordering → PartialOrdering
- def reversed(): Comparator[Rational]
- Definition Classes
- Comparator
- def root(q: Rational, l: Long): Rational
Take the 'l'-th root of the rational number 'q'.
Take the 'l'-th root of the rational number 'q'.
- l
the long root
- def sign(x: Rational): Rational
- Definition Classes
- Numeric
- def sqrt: Rational
Return the square root of that rational number.
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def thenComparing[U <: Comparable[_ >: U <: AnyRef]](arg0: Function[_ >: Rational <: AnyRef, _ <: U]): Comparator[Rational]
- Definition Classes
- Comparator
- def thenComparing[U <: AnyRef](arg0: Function[_ >: Rational <: AnyRef, _ <: U], arg1: Comparator[_ >: U <: AnyRef]): Comparator[Rational]
- Definition Classes
- Comparator
- def thenComparing(arg0: Comparator[_ >: Rational <: AnyRef]): Comparator[Rational]
- Definition Classes
- Comparator
- def thenComparingDouble(arg0: ToDoubleFunction[_ >: Rational <: AnyRef]): Comparator[Rational]
- Definition Classes
- Comparator
- def thenComparingInt(arg0: ToIntFunction[_ >: Rational <: AnyRef]): Comparator[Rational]
- Definition Classes
- Comparator
- def thenComparingLong(arg0: ToLongFunction[_ >: Rational <: AnyRef]): Comparator[Rational]
- Definition Classes
- Comparator
- def times(q: Rational, l: Long): Rational
- def times(q: Rational, p: Rational): Rational
- Definition Classes
- Rational → Numeric
- def toBigDecimal: BigDecimal
- def toBigDecimal(q: Rational): BigDecimal
Convert that/this rational number to a
BigDecimal
number.Convert that/this rational number to a
BigDecimal
number.- q
that rational number to convert
- def toDouble: Double
- def toDouble(q: Rational): Double
Convert that/this rational number to a
Double
.Convert that/this rational number to a
Double
.- q
that rational number to convert
- Definition Classes
- Rational → Numeric
- def toFloat: Float
- def toFloat(q: Rational): Float
Convert that/this rational number to a
Float
.Convert that/this rational number to a
Float
.- q
that rational number to convert
- Definition Classes
- Rational → Numeric
- def toInt: Int
- def toInt(q: Rational): Int
Convert that/this rational number to an
Int
.Convert that/this rational number to an
Int
.- q
that rational number to convert
- Definition Classes
- Rational → Numeric
- def toLong: Long
- def toLong(q: Rational): Long
Convert this rational number to a
Long
.Convert this rational number to a
Long
.- q
that rational number to convert
- Definition Classes
- Rational → Numeric
- def toRational: Rational
- def toRational(q: Rational): Rational
Convert that/this rational number to a Rational.
Convert that/this rational number to a Rational.
- q
that rational number to convert
- def toString(): String
Convert this rational number to a String of the form 'a/b'.
Convert this rational number to a String of the form 'a/b'.
- Definition Classes
- Rational → AnyRef → Any
- def toString2: String
Convert this rational number to a String of the form '(a, b)'.
- def tryCompare(x: Rational, y: Rational): Some[Int]
- Definition Classes
- Ordering → PartialOrdering
- def unary_-: Rational
Compute the unary minus (-).
- val val1: Long
General alias for the parts of a complex number
- val val2: Long
General alias for the parts of a complex number
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- def zero: Rational
- Definition Classes
- Numeric
- def ~^(l: Long): Rational
Raise a rational number to the 'l'-th power.
Raise a rational number to the 'l'-th power.
- l
the long power/exponent
- def ~^(q: Rational): Rational
Raise a rational number to the 'q'-th power.
Raise a rational number to the 'q'-th power.
- q
the rational power/exponent
- def ↑(l: Long): Rational
- def ∈(set: Set[Rational]): Boolean
- def ∈(lim: (Rational, Rational)): Boolean
- def ∉(set: Set[Rational]): Boolean
- def ∉(lim: (Rational, Rational)): Boolean
- def ≈(q: Rational): Boolean
- def ≉(q: Rational): Boolean
- def ≠(q: Rational): Boolean
Compare 'this' rational number with that rational number 'q' for inequality.
Compare 'this' rational number with that rational number 'q' for inequality.
- q
that rational number
- def ≤(q: Rational): Boolean
Compare 'this' rational number with that rational number 'q' for less than or equal to.
Compare 'this' rational number with that rational number 'q' for less than or equal to.
- q
that rational number
- def ≥(q: Rational): Boolean
Compare 'this' rational number with that rational number 'q' for greater than or equal to.
Compare 'this' rational number with that rational number 'q' for greater than or equal to.
- q
that rational number