Rational

com.phasmidsoftware.lascala.values.Rational
See theRational companion object
class Rational[N](numerator: N, denominator: N)(using evidence$1: FiniteIntegral[N]) extends Ordered[Rational[N]]

A simple Rational type for developers not depending on Spire or something like that. This grew out of a class exercise so is not the last word in sophistication.

NOTE that it is not expected that applications will directly reference this case class constructor. If they do, they must ensure that d is positive and that there are no common factors for n and d

NOTE that the underlying type, N, is invariant.

Type parameters

N

the (invariant) underlying integral type

Value parameters

denominator

the denominator of this Rational

numerator

the numerator of this Rational

Attributes

Companion
object
Graph
Supertypes
trait Ordered[Rational[N]]
trait Comparable[Rational[N]]
class Object
trait Matchable
class Any

Members list

Type members

Types

type Builder = (N, N) => Rational[N]

Value members

Concrete methods

def *(multiplicand: Rational[N]): Rational[N]

Operator * to multiply this by multiplicand

Operator * to multiply this by multiplicand

Value parameters

multiplicand

the factor by which this is to be multiplied

Attributes

Returns

this * multiplicand

def +(addend: Rational[N]): Rational[N]

Operator + to add addend to this

Operator + to add addend to this

Value parameters

addend

another Rational[N] -- the addend

Attributes

Returns

the sum of this with addend

def -(subtrahend: Rational[N]): Rational[N]

Operator - to subtract subtrahend from this

Operator - to subtract subtrahend from this

Value parameters

subtrahend

the Rational object to be subtracted from this

Attributes

Returns

the difference of this and subtrahend, i.e. this - subtrahend

def /(divisor: Rational[N]): Rational[N]

Operator / to divide divisor into this

Operator / to divide divisor into this

Value parameters

divisor

the divisor

Attributes

Returns

the quotient of this divided by divisor

def ^(exponent: Int): Rational[N]

Operator &#94 to take this to the power of the given exponent

Operator &#94 to take this to the power of the given exponent

Value parameters

exponent

the power

Attributes

Returns

this * this * ... * this (exponent times)

def compare(other: Rational[N]): Int

Method defined by Ordered

Method defined by Ordered

Value parameters

other

the Rational with which to compare this

Attributes

def d: N
override def equals(obj: Any): Boolean

This should correspond to logic in hashCode (below).

This should correspond to logic in hashCode (below).

Value parameters

obj

the object to be compared with this

Attributes

Returns

true if the objects are considered equal

Definition Classes
Any
def floor: N
override def hashCode(): Int

This should correspond to logic in equals (above).

This should correspond to logic in equals (above).

Attributes

Returns

the hash code for this object.

Definition Classes
Any
def invert(implicit builder: Builder): Rational[N]

Method to invert this

Method to invert this

Attributes

Returns

this but with the numerator and denominator swapped

def isExactDouble: Boolean
def isInfinity: Boolean
def isNaN: Boolean
def isUnity: Boolean
def isWhole: Boolean
def isZero: Boolean
def n: N
def negate: Rational[N]

Method to negate this

Method to negate this

Attributes

Returns

this but with a negative numerator

def power(x: Int): Rational[N]

Raise this to the power of x. A tail-recursive method to perform x multiplications is used

Raise this to the power of x. A tail-recursive method to perform x multiplications is used

Value parameters

x

the required exponent

Attributes

Returns

the result of multiplying x by itself x times

def reciprocal(implicit builder: Builder): Rational[N]

Method to invert this (a synonym for invert)

Method to invert this (a synonym for invert)

Attributes

Returns

this but with the numerator and denominator swapped

def signum: Int

The signum of this Rational[N]

The signum of this Rational[N]

Attributes

Returns

the signum of n (we assume that d is always positive)

def toBigDecimal: Try[BigDecimal]

Method to convert this Rational[N] to a BigDecimal

Method to convert this Rational[N] to a BigDecimal

Attributes

Returns

a BigDecimal corresponding to this Rational[N], or a Failure if this is infinite

def toDouble: Double

Method to convert this Rational[N] to a Double

Method to convert this Rational[N] to a Double

invokes toBigDecimal and converts that to Double

Attributes

Returns

a Double corresponding to this Rational[N] or Double.NaN if that is impossible

def toFloat: Float

Method to convert this Rational[N] to a Float

Method to convert this Rational[N] to a Float

invokes toDouble and converts that to Float

Attributes

Returns

a Float corresponding to this Rational[N]

def toInt: Int

Method to convert this Rational[N] to an Int

Method to convert this Rational[N] to an Int

For details, see Rational[N].toInt

Attributes

Returns

an Int corresponding to this Rational[N], if we're lucky

def toLong: Long

Method to convert this Rational[N] to a Long

Method to convert this Rational[N] to a Long

For details, see Rational[N].toLong

Attributes

Returns

an Int corresponding to this Rational[N], if we're lucky

This method is provided to enable conversion of this Rational[N] into a Rational[M]

This method is provided to enable conversion of this Rational[N] into a Rational[M]

Type parameters

M

the underlying type of the result

Attributes

Returns

a Rational[M] that is equal in value to this

def toRationalString: String
override def toString: String

Returns a string representation of the object.

Returns a string representation of the object.

The default representation is platform dependent.

Attributes

Returns

a string representation of the object.

Definition Classes
Any
def unary_-: Rational[N]

Unary - operator -- to negate this

Unary - operator -- to negate this

Attributes

Returns

the negative of this

Inherited methods

def <(that: Rational[N]): Boolean

Returns true if this is less than that

Returns true if this is less than that

Value parameters

that

the instance to compare against

Attributes

Inherited from:
Ordered
def <=(that: Rational[N]): 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: Rational[N]): Boolean

Returns true if this is greater than that.

Returns true if this is greater than that.

Value parameters

that

the instance to compare against

Attributes

Inherited from:
Ordered
def >=(that: Rational[N]): 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 compareTo(that: Rational[N]): Int

Result of comparing this with operand that.

Result of comparing this with operand that.

Value parameters

that

the instance to compare against

Attributes

Inherited from:
Ordered