|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.lang.Number calculator.RatNum
public final class RatNum
RatNum represents an immutable rational number. It includes all of the elements in the set of rationals, as well as the special "NaN" (not-a-number) element that results from division by zero.
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".
Field Summary | |
---|---|
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 |
Constructor Summary | |
---|---|
RatNum(int n)
|
|
RatNum(int n,
int d)
|
Method Summary | |
---|---|
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. |
Methods inherited from class java.lang.Number |
---|
byteValue, shortValue |
Methods inherited from class java.lang.Object |
---|
getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final RatNum NaN
public static final RatNum ZERO
Constructor Detail |
---|
public RatNum(int n)
public RatNum(int n, int d)
Method Detail |
---|
public boolean isNaN()
public boolean isNegative()
public boolean isPositive()
public int compareTo(RatNum rn)
compareTo
in interface java.lang.Comparable<RatNum>
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)
public RatNum sub(RatNum arg)
public RatNum mul(RatNum arg)
public RatNum div(RatNum arg)
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
public static RatNum valueOf(java.lang.String ratStr)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |