public class RationalNumber extends Number implements Arithmetic<RationalNumber>
Modifier and Type | Field and Description |
---|---|
private BigInteger |
denominator
Holding the denominator of the RationalNumber.
|
private BigInteger |
numerator
Holding the numerator of the RationalNumber.
|
static RationalNumber |
ONE
The canonical representation of 1 as RationalNumber.
|
private static long |
serialVersionUID
Generated serial version UID.
|
static RationalNumber |
ZERO
The canonical representation of zero as RationalNumber.
|
Constructor and Description |
---|
RationalNumber(BigInteger numerator,
BigInteger denominator)
Constructs a RationalNumber for a given numerator and denominator.
|
RationalNumber(double number)
Constructs a RationalNumber out of the given double number.
|
RationalNumber(Double number)
Constructs a RationalNumber out of the given double number.
|
RationalNumber(long numerator,
long denominator)
Constructs a RationalNumber for a given numerator and denominator.
|
RationalNumber(String doubleString)
Constructs a RationalNumber for a given String representing a double.
|
Modifier and Type | Method and Description |
---|---|
RationalNumber |
absValue()
Returns the absolute value of this rational number.
|
RationalNumber |
additiveInverse()
Returns the additive inverse of this RationalNumber.
|
byte |
byteValue()
Returns the byte value of
this.doubleValue() . |
int |
compareTo(RationalNumber o)
Compares two RationalNumbers a/b and c/d.
|
RationalNumber |
copy()
Provides a deep copy of this RationalNumber.
|
RationalNumber |
divided(RationalNumber number)
Divides this number by the given number.
|
double |
doubleValue()
Returns the double value representation of this RationalNumber.
|
boolean |
equals(Object obj)
Two RationalNumbers are considered to be equal if both denominators and
numerators are equal, respectively.
|
float |
floatValue()
Returns the float value of
this.doubleValue() . |
int |
hashCode() |
int |
intValue()
Returns the integer value of
this.doubleValue() . |
long |
longValue()
Returns the long value of
this.doubleValue() . |
RationalNumber |
minus(RationalNumber number)
Subtracts the given number from this number.
|
RationalNumber |
multiplicativeInverse()
Returns the multiplicative inverse of this RationalNumber if it exists.
|
protected void |
normalize()
Normalizes the RationalNumber by normalizing the signum and canceling both,
numerator and denominator, by the greatest common divisor.
|
protected void |
normalizeSignum()
Normalizes the signum such that if the RationalNumber is negative, the
numerator will be negative, the denominator positive.
|
RationalNumber |
plus(RationalNumber number)
Adds the given number to this number.
|
short |
shortValue()
Returns the short value of
this.doubleValue() . |
RationalNumber |
times(RationalNumber number)
Multiplies this number with the given number.
|
String |
toString()
Returns a String representation of this RationalNumber.
|
private static final long serialVersionUID
public static final RationalNumber ZERO
public static final RationalNumber ONE
private BigInteger numerator
private BigInteger denominator
public RationalNumber(BigInteger numerator, BigInteger denominator)
numerator
- the numerator of the RationalNumberdenominator
- the denominator of the RationalNumberIllegalArgumentException
- if denominator.equals(
BigInteger.ZERO)
public RationalNumber(long numerator, long denominator) throws IllegalArgumentException
numerator
- the numerator of the RationalNumberdenominator
- the denominator of the RationalNumberIllegalArgumentException
- if denominator.equals(
BigInteger.ZERO)
public RationalNumber(Double number) throws IllegalArgumentException
number
- a double number to be represented as a RationalNumberIllegalArgumentException
- if the given Double is infinit or not a
numberpublic RationalNumber(double number) throws IllegalArgumentException
number
- a double number to be represented as a RationalNumberIllegalArgumentException
- if the given Double is infinit or not a
numberpublic RationalNumber(String doubleString) throws IllegalArgumentException
doubleString
- a String representing a double numberIllegalArgumentException
- if the given String represents a double
number that is infinit or not a numberprotected void normalize()
protected void normalizeSignum()
public int intValue()
this.doubleValue()
.intValue
in class Number
doubleValue()
public long longValue()
this.doubleValue()
.longValue
in class Number
doubleValue()
public float floatValue()
this.doubleValue()
.floatValue
in class Number
doubleValue()
public byte byteValue()
this.doubleValue()
.byteValue
in class Number
doubleValue()
public short shortValue()
this.doubleValue()
.shortValue
in class Number
doubleValue()
public double doubleValue()
numerator.doubleValue() / denominator.doubleValue()
. Note that
the result may not be exact. Thus after
RationalNumber a = new RationalNumber(b.doubleValue())
,
a.equals(b)
is not necessarily true.doubleValue
in class Number
public RationalNumber plus(RationalNumber number)
Arithmetic
plus
in interface Arithmetic<RationalNumber>
number
- the number to add to this number.public RationalNumber times(RationalNumber number)
Arithmetic
times
in interface Arithmetic<RationalNumber>
number
- the number to multiply this number withpublic RationalNumber minus(RationalNumber number)
Arithmetic
minus
in interface Arithmetic<RationalNumber>
number
- the number to subtract from this numberpublic RationalNumber divided(RationalNumber number) throws ArithmeticException
Arithmetic
divided
in interface Arithmetic<RationalNumber>
number
- the number to divide this number byArithmeticException
- if the given divisor is 0public RationalNumber multiplicativeInverse() throws ArithmeticException
ArithmeticException
- if numerator is 0 and hence the multiplicative
inverse of this rational number does not existpublic RationalNumber additiveInverse()
public RationalNumber absValue()
public int compareTo(RationalNumber o)
(a*d).compareTo(c*b)
.compareTo
in interface Comparable<RationalNumber>
public boolean equals(Object obj)
public String toString()
public RationalNumber copy()