public final class RatNum extends java.lang.Number implements java.lang.Comparable<RatNum>
The "NaN" element is special in many ways. Any arithmetic operation (such as addition) involving "NaN" will return "NaN". With respect to comparison operations, such as less-than, "NaN" is considered equal to itself, and larger than all other rationals.
Examples of RatNums include "-1/13", "53/7", "4", "NaN", and "0".
Modifier and Type | Field and Description |
---|---|
static RatNum |
NaN
A constant holding a Not-a-Number (NaN) value of type RatNum
|
static RatNum |
ZERO
A constant holding a zero value of type RatNum
|
Modifier and Type | Method and Description |
---|---|
RatNum |
add(RatNum arg)
Addition operation.
|
int |
compareTo(RatNum rn)
Compares two RatNums.
|
RatNum |
div(RatNum arg)
Division operation.
|
double |
doubleValue()
Approximates the value of this rational.
|
boolean |
equals(java.lang.Object obj)
Standard equality operation.
|
float |
floatValue()
Returns a float approximation for this.
|
int |
hashCode()
Standard hashCode function.
|
int |
intValue()
Returns an integer approximation for this.
|
boolean |
isNaN()
Returns true if this is NaN
|
boolean |
isNegative()
Returns true if this is negative.
|
boolean |
isPositive()
Returns true if this is positive.
|
long |
longValue()
Returns a long approximation for this.
|
RatNum |
mul(RatNum arg)
Multiplication operation.
|
RatNum |
negate()
Returns the additive inverse of this RatNum.
|
RatNum |
sub(RatNum arg)
Subtraction operation.
|
java.lang.String |
toString() |
static RatNum |
valueOf(java.lang.String ratStr)
Makes a RatNum from a string describing it.
|
public static final RatNum NaN
public static final RatNum ZERO
public RatNum(int n)
n
- the value of the new RatNumpublic RatNum(int n, int d)
n
- the numerator of the new RatNumd
- the denominator of the new RatNumpublic boolean isNaN()
public boolean isNegative()
this < 0
public boolean isPositive()
this > 0
public int compareTo(RatNum rn)
compareTo
in interface java.lang.Comparable<RatNum>
rn
- the RatNum to be comparedthis < rn
, 0 if this = rn, a positive number if this > rn
public double doubleValue()
doubleValue
in class java.lang.Number
Double.NaN
, and
the Double.NaN
value is treated in a special manner by several arithmetic
operations, such as the comparison and equality operators. See the Java Language
Specification, section 4.2.3, for more details.public int intValue()
intValue
in class java.lang.Number
public float floatValue()
floatValue
in class java.lang.Number
public long longValue()
longValue
in class java.lang.Number
public RatNum negate()
public RatNum add(RatNum arg)
arg
- the other value to be addedpublic RatNum sub(RatNum arg)
arg
- the value to be subtractedpublic RatNum mul(RatNum arg)
arg
- the other value to be multipliedpublic RatNum div(RatNum arg)
arg
- the divisorpublic int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
obj
- the object to be compared for equalitypublic java.lang.String toString()
toString
in class java.lang.Object
public static RatNum valueOf(java.lang.String ratStr)
ratStr
- a string of the format described in the @spec.requires clause.