Rat

scalation.Rat
See theRat companion object
case class Rat(num: Long, den: Long = ...) extends Fractional[Rat], Ordered[Rat]

The Rat 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: Rat ("2/3"), 'toString'
(a, b) = (2, 3)      via: create ("(2, 3)") 'toString2'

A Rat number can be created without loss of precision using the constructor, apply, create or fromBigDecimal methods. Other methods may lose precision.

Value parameters

den

the denominator (e.g., 3L)

num

the numerator (e.g., 2L)

Attributes

Companion
object
Graph
Supertypes
trait Product
trait Equals
trait Ordered[Rat]
trait Comparable[Rat]
trait Fractional[Rat]
trait Numeric[Rat]
trait Ordering[Rat]
trait PartialOrdering[Rat]
trait Equiv[Rat]
trait Serializable
trait Comparator[Rat]
class Object
trait Matchable
class Any
Show all

Members list

Type members

Inherited classlikes

class FractionalOps(lhs: Fractional.this.T) extends NumericOps

Attributes

Inherited from:
Fractional
Supertypes
class Fractional.this.NumericOps
class Object
trait Matchable
class Any
class NumericOps(lhs: Numeric.this.T)

Attributes

Inherited from:
Numeric
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class Fractional.this.FractionalOps
class OrderingOps(lhs: Ordering.this.T)

This inner class defines comparison operators available for T.

This inner class defines comparison operators available for T.

It can't extend AnyVal because it is not a top-level class or a member of a statically accessible object.

Attributes

Inherited from:
Ordering
Supertypes
class Object
trait Matchable
class Any

Value members

Concrete methods

def *(q: Rat): Rat

Multiply two rational numbers, this * q.

Multiply two rational numbers, this * q.

Value parameters

q

multiply this times rational q

Attributes

def *(d: Double): Rat
def *(l: Long): Rat

Multiply a rational number times a long, this * l.

Multiply a rational number times a long, this * l.

Value parameters

l

multiply this times long l

Attributes

def +(q: Rat): Rat

Add two rational numbers, this + q.

Add two rational numbers, this + q.

Value parameters

q

add rational q to this

Attributes

def +(d: Double): Rat
def +(l: Long): Rat

Add a rational number plus a long, this + l.

Add a rational number plus a long, this + l.

Value parameters

l

add long l to this

Attributes

def -(q: Rat): Rat

Subtract two rational numbers, this - q.

Subtract two rational numbers, this - q.

Value parameters

q

subtract rational q from this

Attributes

def -(d: Double): Rat
def -(l: Long): Rat

Subtract: this rational number minus a long, this - l.

Subtract: this rational number minus a long, this - l.

Value parameters

l

subtract long l from this

Attributes

def /(q: Rat): Rat

Divide two rational numbers, this / q.

Divide two rational numbers, this / q.

Value parameters

q

divide this by rational q

Attributes

def /(d: Double): Rat
def /(l: Long): Rat

Divide a rational number div a long, this / l

Divide a rational number div a long, this / l

Value parameters

l

divide this by long l

Attributes

def =~(q: Rat): Boolean

Return whether two rational numbers are nearly equal.

Return whether two rational numbers are nearly equal.

Value parameters

q

the compare 'this' with q

Attributes

def abs: Rat

Return the absolute value of this rational number.

Return the absolute value of this rational number.

Attributes

def compare(q: Rat, p: Rat): Int

Compare two rational numbers (negative for <, zero for ==, positive for >).

Compare two rational numbers (negative for <, zero for ==, positive for >).

Value parameters

p

the second rational number to compare

q

the first rational number to compare

Attributes

def compare(q: Rat): Int

Compare this rational number with that rational number 'q'.

Compare this rational number with that rational number 'q'.

Value parameters

q

that rational number

Attributes

inline def div(q: Rat, p: Rat): Rat
inline def div(q: Rat, l: Long): Rat
override 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'.

Value parameters

c

the rational number to compare with this

Attributes

Definition Classes
Equals -> Comparator -> Any
def fromBigDecimal(y: BigDecimal): Rat

Create a rational number from a BigDecimal number.

Create a rational number from a BigDecimal number.

Value parameters

y

the BigDecimal used to create the rational number

Attributes

def fromDouble(y: Double): Rat

Create a rational number from a Double.

Create a rational number from a Double.

Value parameters

y

the Double used to create the rational number

Attributes

See also

Rat.double2Rat

def fromFloat(y: Float): Rat

Create a rational number from a Float. Float is currently not fully supported.

Create a rational number from a Float. Float is currently not fully supported.

Value parameters

y

the Float used to create the rational number

Attributes

def fromInt(n: Int): Rat

Create a rational number from an Int.

Create a rational number from an Int.

Value parameters

n

the Int used to create the rational number

Attributes

def fromLong(n: Long): Rat

Create a rational number from a Long.

Create a rational number from a Long.

Value parameters

n

the Long used to create the rational number

Attributes

override def hashCode: Int

Must also override hashCode to be be compatible with equals.

Must also override hashCode to be be compatible with equals.

Attributes

Definition Classes
Any
def in(lim: (Rat, Rat)): Boolean

Determine whether 'this' is within the given bounds

Determine whether 'this' is within the given bounds

Value parameters

lim

the given (lower, upper) bounds

Attributes

def in(set: Set[Rat]): Boolean

Determine whether 'this' is in the given set.

Determine whether 'this' is in the given set.

Value parameters

lim

the given set of values

Attributes

def isIntegral: Boolean

Determine whether this rational number is integral.

Determine whether this rational number is integral.

Attributes

def max(q: Rat): Rat

Return the maximum of this and that rational numbers.

Return the maximum of this and that rational numbers.

Value parameters

q

that rational number to compare with this

Attributes

def min(q: Rat): Rat

Return the minimum of this and that rational numbers.

Return the minimum of this and that rational numbers.

Value parameters

q

that rational number to compare with this

Attributes

inline def minus(q: Rat, p: Rat): Rat
inline def minus(q: Rat, l: Long): Rat
inline def near_eq(q: Rat, p: Rat): Boolean
inline def negate(q: Rat): Rat
def not_in(lim: (Rat, Rat)): Boolean

Determine whether 'this' is not within the given bounds

Determine whether 'this' is not within the given bounds

Value parameters

lim

the given (lower, upper) bounds

Attributes

def not_in(set: Set[Rat]): Boolean

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

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

Value parameters

lim

the given set of values

Attributes

def parseString(str: String): Option[Rat]

Parse the string to create a rational number.

Parse the string to create a rational number.

Attributes

inline def plus(q: Rat, p: Rat): Rat
inline def plus(q: Rat, l: Long): Rat
inline def pow(q: Rat, p: Rat): Rat
inline def pow(q: Rat, l: Long): Rat
inline def pow_(q: Rat, p: Rat): Rat
def recip: Rat

Take the reciprocal of this rational number by swapping num and den.

Take the reciprocal of this rational number by swapping num and den.

Attributes

def reduce(): Rat

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

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

Attributes

def root(q: Rat, l: Long): Rat

Take the l-th root of the rational number q.

Take the l-th root of the rational number q.

Value parameters

l

the long root

Attributes

def sqrt: Rat

Return the square root of that rational number.

Return the square root of that rational number.

Value parameters

x

that rational number

Attributes

inline def times(q: Rat, p: Rat): Rat
inline def times(q: Rat, l: Long): Rat
def toBigDecimal(q: Rat): BigDecimal

Convert that/this rational number to a BigDecimal number.

Convert that/this rational number to a BigDecimal number.

Value parameters

q

that rational number to convert

Attributes

def toBigDecimal: BigDecimal
def toDouble(q: Rat): Double

Convert that/this rational number to a Double.

Convert that/this rational number to a Double.

Value parameters

q

that rational number to convert

Attributes

def toDouble: Double
def toFloat(q: Rat): Float

Convert that/this rational number to a Float.

Convert that/this rational number to a Float.

Value parameters

q

that rational number to convert

Attributes

def toFloat: Float
def toInt(q: Rat): Int

Convert that/this rational number to an Int.

Convert that/this rational number to an Int.

Value parameters

q

that rational number to convert

Attributes

def toInt: Int
def toLong(q: Rat): Long

Convert this rational number to a Long.

Convert this rational number to a Long.

Value parameters

q

that rational number to convert

Attributes

def toLong: Long
def toRat(q: Rat): Rat

Convert that/this rational number to a Rat.

Convert that/this rational number to a Rat.

Value parameters

q

that rational number to convert

Attributes

def toRat: Rat
override 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'.

Attributes

Definition Classes
Any
def toString2: 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)'.

Attributes

def unary_-: Rat

Compute the unary minus (-).

Compute the unary minus (-).

Attributes

def ~^(q: Rat): Rat

Raise a rational number to the q-th power.

Raise a rational number to the q-th power.

Value parameters

q

the rational power/exponent

Attributes

def ~^(l: Long): Rat

Raise a rational number to the l-th power.

Raise a rational number to the l-th power.

Value parameters

l

the long power/exponent

Attributes

def ÷(num: Long, den: Long): Rat
def ↑(q: Rat): Rat

Raise a rational number to the q-th power. Extended to handle a negative base.

Raise a rational number to the q-th power. Extended to handle a negative base.

Value parameters

q

the rational power/exponent

Attributes

See also

pow_ in CommonFunctions.

def ∈(lim: (Rat, Rat)): Boolean
def ∈(set: Set[Rat]): Boolean
def ∉(lim: (Rat, Rat)): Boolean
def ∉(set: Set[Rat]): Boolean
inline def ≈(q: Rat): Boolean
def ≠(q: Rat): Boolean

Compare 'this' rational number with that rational number 'q' for inequality.

Compare 'this' rational number with that rational number 'q' for inequality.

Value parameters

q

that rational number

Attributes

def ≤(q: Rat): 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.

Value parameters

q

that rational number

Attributes

def ≥(q: Rat): 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.

Value parameters

q

that rational number

Attributes

Inherited methods

def <(that: Rat): Boolean

Returns true if this is less than that

Returns true if this is less than that

Attributes

Inherited from:
Ordered
def <=(that: Rat): Boolean

Returns true if this is less than or equal to that.

Returns true if this is less than or equal to that.

Attributes

Inherited from:
Ordered
def >(that: Rat): Boolean

Returns true if this is greater than that.

Returns true if this is greater than that.

Attributes

Inherited from:
Ordered
def >=(that: Rat): Boolean

Returns true if this is greater than or equal to that.

Returns true if this is greater than or equal to that.

Attributes

Inherited from:
Ordered
def abs(x: Rat): Rat

Attributes

Inherited from:
Numeric
def compareTo(that: Rat): Int

Result of comparing this with operand that.

Result of comparing this with operand that.

Attributes

Inherited from:
Ordered
override def equiv(x: Rat, y: Rat): Boolean

Returns true if x == y in the ordering.

Returns true if x == y in the ordering.

Attributes

Definition Classes
Ordering -> PartialOrdering -> Equiv
Inherited from:
Ordering
override def gt(x: Rat, y: Rat): Boolean

Returns true if x > y in the ordering.

Returns true if x > y in the ordering.

Attributes

Definition Classes
Ordering -> PartialOrdering
Inherited from:
Ordering
override def gteq(x: Rat, y: Rat): Boolean

Returns true if x >= y in the ordering.

Returns true if x >= y in the ordering.

Attributes

Definition Classes
Ordering -> PartialOrdering
Inherited from:
Ordering
def isReverseOf(other: Ordering[_]): Boolean

Returns whether or not the other ordering is the opposite ordering of this one.

Returns whether or not the other ordering is the opposite ordering of this one.

Equivalent to other == this.reverse.

Implementations should only override this method if they are overriding reverse as well.

Attributes

Inherited from:
Ordering
override def lt(x: Rat, y: Rat): Boolean

Returns true if x < y in the ordering.

Returns true if x < y in the ordering.

Attributes

Definition Classes
Ordering -> PartialOrdering
Inherited from:
Ordering
override def lteq(x: Rat, y: Rat): Boolean

Returns true if x <= y in the ordering.

Returns true if x <= y in the ordering.

Attributes

Definition Classes
Ordering -> PartialOrdering
Inherited from:
Ordering
def max[U <: Rat](x: U, y: U): U

Returns x if x >= y, otherwise y.

Returns x if x >= y, otherwise y.

Attributes

Inherited from:
Ordering
def min[U <: Rat](x: U, y: U): U

Returns x if x <= y, otherwise y.

Returns x if x <= y, otherwise y.

Attributes

Inherited from:
Ordering
def on[U](f: U => Rat): Ordering[U]

Given f, a function from U into T, creates an Ordering[U] whose compare function is equivalent to:

Given f, a function from U into T, creates an Ordering[U] whose compare function is equivalent to:

def compare(x:U, y:U) = Ordering[T].compare(f(x), f(y))

Attributes

Inherited from:
Ordering
def one: Rat

Attributes

Inherited from:
Numeric
def orElse(other: Ordering[Rat]): Ordering[Rat]

Creates an Ordering[T] whose compare function returns the result of this Ordering's compare function, if it is non-zero, or else the result of others compare function.

Creates an Ordering[T] whose compare function returns the result of this Ordering's compare function, if it is non-zero, or else the result of others compare function.

Value parameters

other

an Ordering to use if this Ordering returns zero

Attributes

Example
case class Pair(a: Int, b: Int)
val pairOrdering = Ordering.by[Pair, Int](_.a)
                          .orElse(Ordering.by[Pair, Int](_.b))
Inherited from:
Ordering
def orElseBy[S](f: Rat => S)(implicit ord: Ordering[S]): Ordering[Rat]

Given f, a function from T into S, creates an Ordering[T] whose compare function returns the result of this Ordering's compare function, if it is non-zero, or else a result equivalent to:

Given f, a function from T into S, creates an Ordering[T] whose compare function returns the result of this Ordering's compare function, if it is non-zero, or else a result equivalent to:

Ordering[S].compare(f(x), f(y))

This function is equivalent to passing the result of Ordering.by(f) to orElse.

Attributes

Example
case class Pair(a: Int, b: Int)
val pairOrdering = Ordering.by[Pair, Int](_.a)
                          .orElseBy[Int](_.b)
Inherited from:
Ordering
def productElementNames: Iterator[String]

An iterator over the names of all the elements of this product.

An iterator over the names of all the elements of this product.

Attributes

Inherited from:
Product
def productIterator: Iterator[Any]

An iterator over all the elements of this product.

An iterator over all the elements of this product.

Attributes

Returns

in the default implementation, an Iterator[Any]

Inherited from:
Product
override def reverse: Ordering[Rat]

Returns the opposite ordering of this one.

Returns the opposite ordering of this one.

Implementations overriding this method MUST override isReverseOf as well if they change the behavior at all (for example, caching does not require overriding it).

Attributes

Definition Classes
Ordering -> PartialOrdering
Inherited from:
Ordering
def reversed(): Comparator[Rat]

Attributes

Inherited from:
Comparator
def sign(x: Rat): Rat

Attributes

Inherited from:
Numeric
def thenComparing[U <: Comparable[_ >: U <: <FromJavaObject>]](x$0: Function[_ >: Rat <: <FromJavaObject>, _ <: U]): Comparator[Rat]

Attributes

Inherited from:
Comparator
def thenComparing[U <: <FromJavaObject>](x$0: Function[_ >: Rat <: <FromJavaObject>, _ <: U], x$1: Comparator[_ >: U <: <FromJavaObject>]): Comparator[Rat]

Attributes

Inherited from:
Comparator
def thenComparing(x$0: Comparator[_ >: Rat <: <FromJavaObject>]): Comparator[Rat]

Attributes

Inherited from:
Comparator
def thenComparingDouble(x$0: ToDoubleFunction[_ >: Rat <: <FromJavaObject>]): Comparator[Rat]

Attributes

Inherited from:
Comparator
def thenComparingInt(x$0: ToIntFunction[_ >: Rat <: <FromJavaObject>]): Comparator[Rat]

Attributes

Inherited from:
Comparator
def thenComparingLong(x$0: ToLongFunction[_ >: Rat <: <FromJavaObject>]): Comparator[Rat]

Attributes

Inherited from:
Comparator
def tryCompare(x: Rat, y: Rat): Some[Int]

Returns whether a comparison between x and y is defined, and if so the result of compare(x, y).

Returns whether a comparison between x and y is defined, and if so the result of compare(x, y).

Attributes

Inherited from:
Ordering
def zero: Rat

Attributes

Inherited from:
Numeric

Deprecated and Inherited methods

def signum(x: Rat): Int

Attributes

Deprecated
[Since version 2.13.0] use `sign` method instead
Inherited from:
Numeric

Concrete fields

val val1: Long
val val2: Long

Implicits

Inherited implicits

implicit override def mkNumericOps(lhs: Rat): FractionalOps

Attributes

Definition Classes
Fractional -> Numeric
Inherited from:
Fractional
implicit def mkOrderingOps(lhs: Rat): OrderingOps

This implicit method augments T with the comparison operators defined in scala.math.Ordering.Ops.

This implicit method augments T with the comparison operators defined in scala.math.Ordering.Ops.

Attributes

Inherited from:
Ordering