public final class RatNum extends Number implements 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(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.
|
String |
toString() |
static RatNum |
valueOf(String ratStr)
Makes a RatNum from a string describing it.
|
byteValue, shortValue
public RatNum(int n)
n
- The value of the new RatNum.public RatNum(int n, int d)
n
- The numerator of the new RatNum.d
- The denominator of the new RatNum.public boolean isNaN()
public boolean isNegative()
public boolean isPositive()
public int compareTo(RatNum rn)
compareTo
in interface Comparable<RatNum>
rn
- The RatNum to be compared.public double doubleValue()
doubleValue
in class 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()
public float floatValue()
floatValue
in class Number
public long longValue()
public RatNum negate()
public RatNum add(RatNum arg)
arg
- The other value to be added.public RatNum sub(RatNum arg)
arg
- The value to be subtracted.public RatNum mul(RatNum arg)
arg
- The other value to be multiplied.public RatNum div(RatNum arg)
arg
- The divisor.public int hashCode()
public static RatNum valueOf(String ratStr)
ratStr
- A string of the format described in the @requires clause.