Class Rat
- All Implemented Interfaces:
Serializable
,Comparable<Rat>
public final class Rat extends Number implements Comparable<Rat>
toString()
, numerator()
, and denominator()
.
Instances of Rat
are acquired from the static rat()
methods.
It is recommended to statically import rat()
for convenience. There
are many overloads of rat()
for acquiring Rat
from different
inputs. The string overload takes an arbitrary arithmetic expression as its
argument, evaluates it, and returns the result as a Rat
. The static
instances ZERO
, ONE
, PI
, and E
are also
available.
Arithmetic methods include negate
, abs
, invert
,
plus
, minus
, times
, dividedBy
, sqrt
,
nthRoot
, pow
, exp
, log
, and ln
.
Whenever possible, these arithmetic methods return an exact result; for example,
If the result is irrational, a very high-precision approximation is returned (at least 100 significant figures in the decimal representation).rat(-8,27).pow(rat(-2,3)) → 9/4
rat(9,4).log(rat(8,27)) → -2/3
Comparison methods equal
, notEqual
, greater
, less
,
greaterOrEqual
, lessOrEqual
, isPositive
, isNegative
,
and isZero
are provided, as well as the method isInteger
.
All method arguments must not be null unless otherwise stated.
Any reasonably sized rational number can be represented exactly, but if a numerator
or denominator approaches the magnitude 2
Integer.MAX_VALUE
,
overflow may occur.
- See Also:
- Serialized Form
-
Field Summary
-
Method Summary
Modifier and Type Method Description Rat
abs()
Returns the absolute value of thisRat
.BigDecimal
bigDecimalValue()
Returns aBigDecimal
representation ofthis
.int
compareTo(Rat other)
Returns a positive result ifthis
is greater thanother
, a negative result ifthis
is less thanother
, and zero ifthis
is numerically equal toother
.BigInteger
denominator()
Returns the denominator ofthis
, in its reduced-form representation.Rat
dividedBy(Rat other)
Returnsthis/other
.double
doubleValue()
Returns adouble
representation ofthis
.boolean
equal(Rat other)
Returns true ifthis
is numerically equal toother
.boolean
equals(Object obj)
Returns true if the given object is a non-nullRat
that is numerically equivalent tothis
.Rat
exp()
Returnse
this
.float
floatValue()
Returns afloat
representation ofthis
.boolean
greater(Rat other)
Returns true ifthis
is greater thanother
.boolean
greaterOrEqual(Rat other)
Returns true ifthis
is greater than or numerically equal toother
.int
hashCode()
Returns a hash code ofthis
.int
intValue()
Returns anint
representation ofthis
, if possible.Rat
invert()
Returns1/this
.boolean
isInteger()
Returns true if thisRat
can be exactly represented with a denominator of one.boolean
isNegative()
Returns true if thisRat
is less than zero.boolean
isPositive()
Returns true if thisRat
is greater than zero.boolean
isZero()
Returns true if thisRat
is equal to zero.boolean
less(Rat other)
Returns true ifthis
is less thanother
.boolean
lessOrEqual(Rat other)
Returns true ifthis
is less than or numerically equal toother
.Rat
ln()
Returns the natural log ofthis
; i.e., the log ofthis
with basee
.Rat
log(Rat base)
Returns the log ofthis
with the given base.long
longValue()
Returns along
representation ofthis
, if possible.Rat
minus(Rat other)
Returnsthis-other
.Rat
negate()
Returns-1*this
.boolean
notEqual(Rat other)
Returns true ifthis
is not numerically equal toother
.Rat
nthRoot(long n)
Returns then
th root ofthis
; i.e.BigInteger
numerator()
Returns the numerator ofthis
, in its reduced-form representation.Rat
plus(Rat other)
Returnsthis+other
.Rat
pow(Rat exponent)
Returnsthis
exponent
.static Rat
rat(double value)
Returns aRat
equal to the given value.static Rat
rat(long value)
Returns aRat
equal tovalue/1
.static Rat
rat(long numerator, long denominator)
Returns aRat
equal tonumerator/denominator
.static Rat
rat(String expression)
Evaluates the given expression and returns the result as aRat
.static Rat
rat(BigDecimal value)
Returns aRat
equal to the given value.static Rat
rat(BigInteger value)
Returns aRat
equal tovalue/1
.static Rat
rat(BigInteger numerator, BigInteger denominator)
Returns aRat
equal tonumerator/denominator
.Rat
reducePrecision(int maxDigits)
If both the numerator and denominator of this Rat have more digits than the given maximum, returns a different Rat where both the numerator and denominator have been divided by a power of 10 such that the shorter of the two has the requested number of digits.Rat
round()
Returns this Rat rounded to the nearest integer value.Rat
sqrt()
Returns the square root ofthis
.Rat
times(Rat other)
Returnsthis*other
.String
toString()
Returns a string representation ofthis
.
-
Field Details
-
Method Details
-
rat
Returns aRat
equal tonumerator/denominator
.denominator
cannot be zero. -
rat
Returns aRat
equal tovalue/1
. -
rat
Returns aRat
equal tovalue/1
. -
rat
Returns aRat
equal tonumerator/denominator
.denominator
cannot be zero. -
rat
Returns aRat
equal to the given value. -
rat
Returns aRat
equal to the given value. -
rat
Evaluates the given expression and returns the result as aRat
. The expression must be a valid arithmetic expression, and may contain whitespace, digits, decimal points, parenthesis, and the operators+ - * / ^
. -
isInteger
public boolean isInteger()Returns true if thisRat
can be exactly represented with a denominator of one.Whether or not it is small enough to be represented as an
int
is irrelevant. -
isPositive
public boolean isPositive()Returns true if thisRat
is greater than zero. -
isNegative
public boolean isNegative()Returns true if thisRat
is less than zero. -
isZero
public boolean isZero()Returns true if thisRat
is equal to zero. -
negate
Returns-1*this
. -
abs
Returns the absolute value of thisRat
. -
invert
Returns1/this
.If
this
is zero, an exception is thrown. -
plus
Returnsthis+other
. -
minus
Returnsthis-other
. -
times
Returnsthis*other
. -
dividedBy
Returnsthis/other
.other
cannot be zero. -
pow
Returnsthis
exponent
.this
andexponent
cannot both be zero. Ifthis
is negative, the deonominator ofexponent
must be odd. -
nthRoot
Returns then
th root ofthis
; i.e.this
1/n
.n
must be greater than zero. Ifthis
is negative,n
must be odd. -
sqrt
Returns the square root ofthis
.this
cannot be negative. -
exp
Returnse
this
. -
log
Returns the log ofthis
with the given base.this
must be greater than zero.base
must be greater than zero and not equal to one. -
ln
Returns the natural log ofthis
; i.e., the log ofthis
with basee
.this
must be greater than zero. -
equal
Returns true ifthis
is numerically equal toother
.The comparison is about numeric equivalence, so, for example,
rat(1,2).equal(rat(2,4)) => true
.This method is equivalent to
equals(Object)
in the case that the given object is a non-nullRat
. -
notEqual
Returns true ifthis
is not numerically equal toother
.The comparison is about numeric equivalence, so, for example,
rat(1,2).notEqual(rat(2,4)) => false
. -
greater
Returns true ifthis
is greater thanother
. -
less
Returns true ifthis
is less thanother
. -
greaterOrEqual
Returns true ifthis
is greater than or numerically equal toother
. -
lessOrEqual
Returns true ifthis
is less than or numerically equal toother
. -
compareTo
Returns a positive result ifthis
is greater thanother
, a negative result ifthis
is less thanother
, and zero ifthis
is numerically equal toother
.- Specified by:
compareTo
in interfaceComparable<Rat>
-
numerator
Returns the numerator ofthis
, in its reduced-form representation. -
denominator
Returns the denominator ofthis
, in its reduced-form representation. -
round
Returns this Rat rounded to the nearest integer value. UsesRoundingMode.HALF_UP
.Examples:
1 → 1 4/5 → 1 1/2 → 1 1/5 → 0 0 → 0 -1/5 → 0 -1/2 → -1 -4/5 → -1 -1 → -1
-
reducePrecision
If both the numerator and denominator of this Rat have more digits than the given maximum, returns a different Rat where both the numerator and denominator have been divided by a power of 10 such that the shorter of the two has the requested number of digits. If rounding is necessary,RoundingMode.HALF_UP
is used.Note that the resulting fraction will be reduced if possible, so may contain different leading digits than this Rat.
If either the numerator or denominator of this Rat does not exceed the requested number of digits, a Rat of the same value is returned.
Examples:
1234/2345, 3 → 123/235 1234/2345, 4 → 1234/2345 1234/2345, 5 → 1234/2345 123/4567, 2 → 12/457 123/4561, 2 → 1/38 (12/456 reduced) 123/4567, 3 → 123/4567 123/4567, 4 → 123/4567
-
bigDecimalValue
Returns aBigDecimal
representation ofthis
. -
doubleValue
public double doubleValue()Returns adouble
representation ofthis
. Precision may be lost becausedouble
has limited precision. If the magnitude ofthis
is too large to be represented as adouble
, an exception is thrown.- Specified by:
doubleValue
in classNumber
-
floatValue
public float floatValue()Returns afloat
representation ofthis
. Precision may be lost becausefloat
has limited precision. If the magnitude ofthis
is too large to be represented as afloat
, an exception is thrown.- Specified by:
floatValue
in classNumber
-
longValue
public long longValue()Returns along
representation ofthis
, if possible. Ifthis
cannot be exactly represented as along
, an exception is thrown. -
intValue
public int intValue()Returns anint
representation ofthis
, if possible. Ifthis
cannot be exactly represented as anint
, an exception is thrown. -
equals
Returns true if the given object is a non-nullRat
that is numerically equivalent tothis
.This method is equivalent to
equal(Rat)
in the case that the given object is a non-nullRat
. -
hashCode
public int hashCode()Returns a hash code ofthis
. -
toString
Returns a string representation ofthis
. The representation will be reduced to lowest terms.
-