public final class Rational extends Number implements Comparable<Rational>
An immutable data type representation a rational number.
Contains a pair of int
s representing the numerator and denominator of a
Rational number.
Modifier and Type | Field and Description |
---|---|
static Rational |
NaN
Constant for the Not-a-Number (NaN) value of the
Rational type. |
static Rational |
NEGATIVE_INFINITY
Constant for the negative infinity value of the
Rational type. |
static Rational |
POSITIVE_INFINITY
Constant for the positive infinity value of the
Rational type. |
static Rational |
ZERO
Constant for the zero value of the
Rational type. |
Constructor and Description |
---|
Rational(int numerator,
int denominator)
Create a
Rational with a given numerator and denominator. |
Modifier and Type | Method and Description |
---|---|
int |
compareTo(Rational another)
Compare this rational to the specified rational to determine their natural order.
|
double |
doubleValue()
Returns the value of the specified number as a
double . |
boolean |
equals(Object obj)
Compare this Rational to another object and see if they are equal.
|
float |
floatValue()
Returns the value of the specified number as a
float . |
static int |
gcd(int numerator,
int denominator)
Calculates the greatest common divisor using Euclid's algorithm.
|
int |
getDenominator()
Gets the denominator of the rational
The denominator may return
0 , in which case the rational may represent
positive infinity (if the numerator was positive), negative infinity (if the numerator
was negative), or NaN (if the numerator was 0 ). |
int |
getNumerator()
Gets the numerator of the rational.
|
int |
hashCode()
Returns a hash code value for the object.
|
int |
intValue()
Returns the value of the specified number as a
int . |
boolean |
isFinite()
Indicates whether this rational represents a finite value.
|
boolean |
isInfinite()
Indicates whether this rational represents an infinite value.
|
boolean |
isNaN()
Indicates whether this rational is a Not-a-Number (NaN) value.
|
boolean |
isZero()
Indicates whether this rational represents a zero value.
|
long |
longValue()
Returns the value of the specified number as a
long . |
static Rational |
parseRational(String string)
Parses the specified string as a rational value.
|
short |
shortValue()
Returns the value of the specified number as a
short . |
float |
toFloat()
Convert to a floating point representation.
|
String |
toString()
Return a string representation of this rational, e.g.
|
public static final Rational NaN
Rational
type.
A NaN
value is considered to be equal to itself (that is NaN.equals(NaN)
will return true
; it is always greater than any non-NaN
value (that is
NaN.compareTo(notNaN)
will return a number greater than 0
).
Equivalent to constructing a new rational with both the numerator and denominator
equal to 0
.
public static final Rational POSITIVE_INFINITY
Rational
type.
Equivalent to constructing a new rational with a positive numerator and a denominator
equal to 0
.
public static final Rational NEGATIVE_INFINITY
Rational
type.
Equivalent to constructing a new rational with a negative numerator and a denominator
equal to 0
.
public static final Rational ZERO
Rational
type.
Equivalent to constructing a new rational with a numerator equal to 0
and
any non-zero denominator.
public Rational(int numerator, int denominator)
Create a Rational
with a given numerator and denominator.
The signs of the numerator and the denominator may be flipped such that the denominator
is always positive. Both the numerator and denominator will be converted to their reduced
forms (see equals(java.lang.Object)
for more details).
For example,
2/4
will be reduced to 1/2
.
1/-1
will be flipped to -1/1
5/0
will be reduced to 1/0
0/5
will be reduced to 0/1
numerator
- the numerator of the rationaldenominator
- the denominator of the rationalequals(java.lang.Object)
public int getNumerator()
The numerator will always return 1
if this rational represents
infinity (that is, the denominator is 0
).
public int getDenominator()
The denominator may return 0
, in which case the rational may represent
positive infinity (if the numerator was positive), negative infinity (if the numerator
was negative), or NaN
(if the numerator was 0
).
The denominator will always return 1
if the numerator is 0
.
public boolean isNaN()
A NaN
value occurs when both the numerator and the denominator are 0
.
true
if this rational is a Not-a-Number (NaN) value;
false
if this is a (potentially infinite) number valuepublic boolean isInfinite()
An infinite value occurs when the denominator is 0
(but the numerator is not).
true
if this rational is a (positive or negative) infinite value;
false
if this is a finite number value (or NaN
)public boolean isFinite()
A finite value occurs when the denominator is not 0
; in other words
the rational is neither infinity or NaN
.
true
if this rational is a (positive or negative) infinite value;
false
if this is a finite number value (or NaN
)public boolean isZero()
A zero value is a finite
rational with a numerator of 0
.
true
if this rational is finite zero value;
false
otherwisepublic boolean equals(Object obj)
Compare this Rational to another object and see if they are equal.
A Rational object can only be equal to another Rational object (comparing against any
other type will return false
).
A Rational object is considered equal to another Rational object if and only if one of the following holds:
NaN
A reduced form of a Rational is calculated by dividing both the numerator and the denominator by their greatest common divisor.
(new Rational(1, 2)).equals(new Rational(1, 2)) == true // trivially true
(new Rational(2, 3)).equals(new Rational(1, 2)) == false // trivially false
(new Rational(1, 2)).equals(new Rational(2, 4)) == true // true after reduction
(new Rational(0, 0)).equals(new Rational(0, 0)) == true // NaN.equals(NaN)
(new Rational(1, 0)).equals(new Rational(5, 0)) == true // both are +infinity
(new Rational(1, 0)).equals(new Rational(-1, 0)) == false // +infinity != -infinity
equals
in class Object
obj
- a reference to another objectObject.hashCode()
,
HashMap
public String toString()
"1/2"
.
The following rules of conversion apply:
NaN
values will return "NaN"
"Infinity"
"-Infinity"
"numerator/denominator"
where numerator
and denominator
are substituted with the appropriate numerator and denominator
values.
public float toFloat()
Convert to a floating point representation.
public int hashCode()
HashMap
.
The general contract of hashCode
is:
hashCode
method
must consistently return the same integer, provided no information
used in equals
comparisons on the object is modified.
This integer need not remain consistent from one execution of an
application to another execution of the same application.
equals(Object)
method, then calling the hashCode
method on each of
the two objects must produce the same integer result.
Object.equals(java.lang.Object)
method, then calling the hashCode
method on each of the
two objects must produce distinct integer results. However, the
programmer should be aware that producing distinct integer results
for unequal objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by
class Object
does return distinct integers for distinct
objects. (This is typically implemented by converting the internal
address of the object into an integer, but this implementation
technique is not required by the
JavaTM programming language.)
hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public static int gcd(int numerator, int denominator)
Visible for testing only.
numerator
- the numerator in a fractiondenominator
- the denominator in a fractionpublic double doubleValue()
double
.
The double
is calculated by converting both the numerator and denominator
to a double
; then returning the result of dividing the numerator by the
denominator.
doubleValue
in class Number
double
.public float floatValue()
float
.
The float
is calculated by converting both the numerator and denominator
to a float
; then returning the result of dividing the numerator by the
denominator.
floatValue
in class Number
float
.public int intValue()
int
.
Finite
rationals are converted to an int
value
by dividing the numerator by the denominator; conversion for non-finite values happens
identically to casting a floating point value to an int
, in particular:
Integer.MAX_VALUE
Integer.MIN_VALUE
0
.public long longValue()
long
.
Finite
rationals are converted to an long
value
by dividing the numerator by the denominator; conversion for non-finite values happens
identically to casting a floating point value to a long
, in particular:
Long.MAX_VALUE
Long.MIN_VALUE
0
.public short shortValue()
short
.
Finite
rationals are converted to a short
value
identically to intValue()
; the int
result is then truncated to a
short
before returning the value.
shortValue
in class Number
short
.public int compareTo(Rational another)
NaN
is considered to be equal to itself and greater than all other
Rational
values. Otherwise, if the objects are not equal
, then
the following rules apply:
compareTo
in interface Comparable<Rational>
another
- the rational to be comparedNullPointerException
- if another
was null
public static Rational parseRational(String string) throws NumberFormatException
The ASCII characters \
u003a
(':') and
\
u002f
('/') are recognized as separators between
the numerator and denumerator.
For any Rational r
: Rational.parseRational(r.toString()).equals(r)
.
However, the method also handles rational numbers expressed in the
following forms:
"num/
den" or
"num:
den" => new Rational(num, den);
,
where num and den are string integers potentially
containing a sign, such as "-10", "+7" or "5".
Rational.parseRational("3:+6").equals(new Rational(1, 2)) == true
Rational.parseRational("-3/-6").equals(new Rational(1, 2)) == true
Rational.parseRational("4.56") => throws NumberFormatException
string
- the string representation of a rational value.string
.NumberFormatException
- if string
cannot be parsed
as a rational value.NullPointerException
- if string
was null