@Status(stage=PRODUCTION, unitTests=COMPLETE) @Review(by="Kees Schotanus", at="2009-09-28") @ThreadSafety(level=IMMUTABLE) public class CRational extends Object implements Serializable
Modifier and Type | Field and Description |
---|---|
static CRational |
ZERO
A rational value of zero.
|
Constructor and Description |
---|
CRational(long numerator)
Constructs a rational number from the supplied numerator and an implied
denominator of one.
|
CRational(long numerator,
long denominator)
Constructs a rational number from the supplied numerator and denominator.
|
Modifier and Type | Method and Description |
---|---|
CRational |
add(CRational rational)
Adds the supplied rational number to this rational number.
|
static CRational |
add(CRational left,
CRational right)
Adds the right rational number to the left rational number.
|
int |
compareTo(CRational rational)
Compares the supplied rational with this CRational.
|
CRational |
divide(CRational rational)
Divides this rational number by the supplied rational number.
|
static CRational |
divide(CRational left,
CRational right)
Divides the left rational number by the right rational number.
|
double |
doubleValue()
Gets the value of this CRational as a double.
|
boolean |
equals(Object object)
Determines whether the supplied object is equal to this CRational.
|
long |
getDenominator()
Gets the denominator.
|
long |
getNumerator()
Gets the numerator.
|
int |
hashCode()
Computes a hash code for this object.
|
CRational |
multiply(CRational rational)
Multiplies this rational number by the supplied rational number.
|
static CRational |
multiply(CRational left,
CRational right)
Multiplies the left rational number by the right rational number.
|
CRational |
subtract(CRational rational)
Subtracts the supplied rational number from this rational number.
|
static CRational |
subtract(CRational left,
CRational right)
Subtracts the right rational number from the left rational number.
|
String |
toString()
Creates a String representation of this CRational.
|
static CRational |
valueOf(double value)
Converts the supplied value to a CRational.
|
public static final CRational ZERO
public CRational(long numerator)
numerator
- The numerator.public CRational(long numerator, long denominator)
Supplied numerator |
Supplied denominator |
Stored numerator |
Stored denominator |
---|---|---|---|
1 | 2 | 1 | 2 |
2 | 4 | 1 | 2 |
6 | 8 | 3 | 4 |
-6 | -8 | 3 | 4 |
6 | -8 | -6 | 8 |
6 | 6 | 1 | 1 |
numerator
- The numerator.denominator
- The non zero denominator.ArithmeticException
- When the supplied denominator is zero.public long getNumerator()
public long getDenominator()
public CRational add(CRational rational)
rational
- The rational number to add to this rational number.NullPointerException
- When the supplied rational number is null.public static CRational add(CRational left, CRational right)
left
- The left rational number.right
- The right rational number.NullPointerException
- When either the supplied left or right
rational number is null.public CRational subtract(CRational rational)
rational
- The rational number to subtract from this rational
number.NullPointerException
- When the supplied rational number is null.public static CRational subtract(CRational left, CRational right)
left
- The left rational number.right
- The right rational number.NullPointerException
- When either the supplied left or right
rational number is null.public CRational multiply(CRational rational)
rational
- The rational number to multiply by.NullPointerException
- When the supplied rational number is null.public static CRational multiply(CRational left, CRational right)
left
- The left rational number.right
- The right rational number.NullPointerException
- When either the supplied left or right
rational number is null.public CRational divide(CRational rational)
rational
- The rational number to divide by.NullPointerException
- When the supplied rational number is null.public static CRational divide(CRational left, CRational right)
left
- The left rational number.right
- The right rational number.ArithmeticException
- When the numerator of the right operand is 0.NullPointerException
- When either the supplied left or right
rational number is null.public boolean equals(Object object)
public int hashCode()
public int compareTo(CRational rational)
rational
- The CRational to compare with this CRational.NullPointerException
- When the supplied rational is null.Comparable
public String toString()
public double doubleValue()
public static CRational valueOf(double value)
value
- The value to be converted.IllegalArgumentException
- When the supplied value is infinite or
Not a Number.Copyright © 2008–2018. All rights reserved.