Packages

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)

Linear Supertypes
Product, Equals, Ordered[Rational], Comparable[Rational], Fractional[Rational], Numeric[Rational], Ordering[Rational], PartialOrdering[Rational], Equiv[Rational], Serializable, Comparator[Rational], AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Rational
  2. Product
  3. Equals
  4. Ordered
  5. Comparable
  6. Fractional
  7. Numeric
  8. Ordering
  9. PartialOrdering
  10. Equiv
  11. Serializable
  12. Comparator
  13. AnyRef
  14. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Instance Constructors

  1. new Rational(num: Long, den: Long = 1L)

    num

    the numerator (e.g., 2)

    den

    the denominator (e.g., 3)

Type Members

  1. class FractionalOps extends scala.math.Fractional.NumericOps
    Definition Classes
    Fractional
  2. class NumericOps extends AnyRef
    Definition Classes
    Numeric
  3. class OrderingOps extends AnyRef
    Definition Classes
    Ordering

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. 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

  3. final def ##: Int
    Definition Classes
    AnyRef → Any
  4. def *(l: Long): Rational

    Multiply a rational number times a long.

    Multiply a rational number times a long.

    l

    multiply this times long l

  5. def *(q: Rational): Rational

    Multiply two rational numbers 'this * q'.

    Multiply two rational numbers 'this * q'.

    q

    multiply this times rational q

  6. def +(l: Long): Rational

    Add a rational number plus a long.

    Add a rational number plus a long.

    l

    add long l to this

  7. def +(q: Rational): Rational

    Add two rational numbers 'this + q'.

    Add two rational numbers 'this + q'.

    q

    add rational q to this

  8. def -(l: Long): Rational

    Subtract: a rational number minus a long.

    Subtract: a rational number minus a long.

    l

    subtract long l from this

  9. def -(q: Rational): Rational

    Subtract two rational numbers 'this - q'.

    Subtract two rational numbers 'this - q'.

    q

    subtract rational q from this

  10. def /(l: Long): Rational

    Divide a rational number div a long.

    Divide a rational number div a long.

    l

    divide this by long l

  11. def /(q: Rational): Rational

    Divide two rational numbers 'this / q'.

    Divide two rational numbers 'this / q'.

    q

    divide this by rational q

  12. def <(that: Rational): Boolean
    Definition Classes
    Ordered
  13. def <=(that: Rational): Boolean
    Definition Classes
    Ordered
  14. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  15. 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

  16. def >(that: Rational): Boolean
    Definition Classes
    Ordered
  17. def >=(that: Rational): Boolean
    Definition Classes
    Ordered
  18. def abs: Rational

    Return the absolute value of this rational number.

  19. def abs(x: Rational): Rational
    Definition Classes
    Numeric
  20. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  21. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
  22. 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
  23. 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
  24. def compareTo(that: Rational): Int
    Definition Classes
    Ordered → Comparable
  25. val den: Long
  26. def div(q: Rational, l: Long): Rational
  27. def div(q: Rational, p: Rational): Rational
    Definition Classes
    Rational → Fractional
  28. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  29. 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
  30. def equiv(x: Rational, y: Rational): Boolean
    Definition Classes
    Ordering → PartialOrdering → Equiv
  31. 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

  32. 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
  33. 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

  34. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  35. def gt(x: Rational, y: Rational): Boolean
    Definition Classes
    Ordering → PartialOrdering
  36. def gteq(x: Rational, y: Rational): Boolean
    Definition Classes
    Ordering → PartialOrdering
  37. 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
  38. def in(set: Set[Rational]): Boolean

    Determine whether 'this' is in the given set.

  39. 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

  40. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  41. def isIntegral: Boolean

    Determine whether this rational number is integral.

  42. def isReverseOf(other: Ordering[_]): Boolean
    Definition Classes
    Ordering
  43. def lt(x: Rational, y: Rational): Boolean
    Definition Classes
    Ordering → PartialOrdering
  44. def lteq(x: Rational, y: Rational): Boolean
    Definition Classes
    Ordering → PartialOrdering
  45. 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

  46. def max[U <: Rational](x: U, y: U): U
    Definition Classes
    Ordering
  47. 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

  48. def min[U <: Rational](x: U, y: U): U
    Definition Classes
    Ordering
  49. def minus(q: Rational, l: Long): Rational
  50. def minus(q: Rational, p: Rational): Rational
    Definition Classes
    Rational → Numeric
  51. implicit def mkNumericOps(lhs: Rational): FractionalOps
    Definition Classes
    Fractional → Numeric
  52. implicit def mkOrderingOps(lhs: Rational): OrderingOps
    Definition Classes
    Ordering
  53. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  54. def near_eq(q: Rational, p: Rational): Boolean
  55. def negate(q: Rational): Rational
    Definition Classes
    Rational → Numeric
  56. def not_in(set: Set[Rational]): Boolean

    Determine whether 'this' is not in the given set.

  57. 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

  58. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  59. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @HotSpotIntrinsicCandidate()
  60. val num: Long
  61. def on[U](f: (U) => Rational): Ordering[U]
    Definition Classes
    Ordering
  62. def one: Rational
    Definition Classes
    Numeric
  63. def orElse(other: Ordering[Rational]): Ordering[Rational]
    Definition Classes
    Ordering
  64. def orElseBy[S](f: (Rational) => S)(implicit ord: Ordering[S]): Ordering[Rational]
    Definition Classes
    Ordering
  65. 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
  66. def plus(q: Rational, l: Long): Rational
  67. def plus(q: Rational, p: Rational): Rational
    Definition Classes
    Rational → Numeric
  68. def pow(q: Rational, l: Long): Rational
  69. def pow(q: Rational, p: Rational): Rational
  70. def productElementNames: Iterator[String]
    Definition Classes
    Product
  71. def reduce(): Rational

    Reduce the magnitude of the numerator and denonimator by dividing both by their Greatest Common Divisor (GCD).

  72. def reverse: Ordering[Rational]
    Definition Classes
    Ordering → PartialOrdering
  73. def reversed(): Comparator[Rational]
    Definition Classes
    Comparator
  74. 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

  75. def sign(x: Rational): Rational
    Definition Classes
    Numeric
  76. def sqrt: Rational

    Return the square root of that rational number.

  77. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  78. def thenComparing[U <: Comparable[_ >: U <: AnyRef]](arg0: Function[_ >: Rational <: AnyRef, _ <: U]): Comparator[Rational]
    Definition Classes
    Comparator
  79. def thenComparing[U <: AnyRef](arg0: Function[_ >: Rational <: AnyRef, _ <: U], arg1: Comparator[_ >: U <: AnyRef]): Comparator[Rational]
    Definition Classes
    Comparator
  80. def thenComparing(arg0: Comparator[_ >: Rational <: AnyRef]): Comparator[Rational]
    Definition Classes
    Comparator
  81. def thenComparingDouble(arg0: ToDoubleFunction[_ >: Rational <: AnyRef]): Comparator[Rational]
    Definition Classes
    Comparator
  82. def thenComparingInt(arg0: ToIntFunction[_ >: Rational <: AnyRef]): Comparator[Rational]
    Definition Classes
    Comparator
  83. def thenComparingLong(arg0: ToLongFunction[_ >: Rational <: AnyRef]): Comparator[Rational]
    Definition Classes
    Comparator
  84. def times(q: Rational, l: Long): Rational
  85. def times(q: Rational, p: Rational): Rational
    Definition Classes
    Rational → Numeric
  86. def toBigDecimal: BigDecimal
  87. 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

  88. def toDouble: Double
  89. 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
  90. def toFloat: Float
  91. 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
  92. def toInt: Int
  93. 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
  94. def toLong: Long
  95. 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
  96. def toRational: Rational
  97. 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

  98. 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
  99. def toString2: String

    Convert this rational number to a String of the form '(a, b)'.

  100. def tryCompare(x: Rational, y: Rational): Some[Int]
    Definition Classes
    Ordering → PartialOrdering
  101. def unary_-: Rational

    Compute the unary minus (-).

  102. val val1: Long

    General alias for the parts of a complex number

  103. val val2: Long

    General alias for the parts of a complex number

  104. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  105. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  106. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  107. def zero: Rational
    Definition Classes
    Numeric
  108. 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

  109. 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

  110. def (l: Long): Rational
  111. def (set: Set[Rational]): Boolean
  112. def (lim: (Rational, Rational)): Boolean
  113. def (set: Set[Rational]): Boolean
  114. def (lim: (Rational, Rational)): Boolean
  115. def (q: Rational): Boolean
  116. def (q: Rational): Boolean
  117. 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

  118. 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

  119. 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

Deprecated Value Members

  1. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable]) @Deprecated
    Deprecated
  2. def signum(x: Rational): Int
    Definition Classes
    Numeric
    Annotations
    @deprecated
    Deprecated

    (Since version 2.13.0) use sign method instead

Inherited from Product

Inherited from Equals

Inherited from Ordered[Rational]

Inherited from Comparable[Rational]

Inherited from Fractional[Rational]

Inherited from Numeric[Rational]

Inherited from Ordering[Rational]

Inherited from PartialOrdering[Rational]

Inherited from Equiv[Rational]

Inherited from Serializable

Inherited from Comparator[Rational]

Inherited from AnyRef

Inherited from Any

Ungrouped