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
Members list
Value members
Concrete methods
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
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
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
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
Operator ^ to take this to the power of the given exponent
Operator ^ to take this to the power of the given exponent
Value parameters
- exponent
-
the power
Attributes
- Returns
-
this * this * ... * this (exponent times)
Method defined by Ordered
Method defined by Ordered
Value parameters
- other
-
the Rational with which to compare this
Attributes
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
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
Method to invert this
Method to invert this
Attributes
- Returns
-
this but with the numerator and denominator swapped
Method to negate this
Method to negate this
Attributes
- Returns
-
this but with a negative numerator
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
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
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)
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
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
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]
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
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
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
Inherited methods
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
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
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
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