java.lang.Object
java.lang.Number
de.lmu.ifi.dbs.elki.data.RationalNumber
- All Implemented Interfaces:
Arithmetic<RationalNumber>
,Serializable
,Comparable<RationalNumber>
RationalNumber represents rational numbers in arbitrary precision. Note that
the best possible precision is the primary objective of this class. Since
numerator and denominator of the RationalNumber are represented as
BigIntegers, the required space can grow unlimited. Also arithmetic
operations are considerably less efficient compared to the operations with
doubles.
- Author:
- Arthur Zimek
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final RationalNumber
The canonical representation of 1 as RationalNumber.static final RationalNumber
The canonical representation of zero as RationalNumber. -
Constructor Summary
ConstructorDescriptionRationalNumber
(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
(Double number) Constructs a RationalNumber out of the given double number.RationalNumber
(String doubleString) Constructs a RationalNumber for a given String representing a double.RationalNumber
(BigInteger numerator, BigInteger denominator) Constructs a RationalNumber for a given numerator and denominator. -
Method Summary
Modifier and TypeMethodDescriptionabsValue()
Returns the absolute value of this rational number.Returns the additive inverse of this RationalNumber.byte
Returns the byte value ofthis.doubleValue()
.int
Compares two RationalNumbers a/b and c/d.copy()
Provides a deep copy of this RationalNumber.divided
(RationalNumber number) Divides this number by the given number.double
Returns the double value representation of this RationalNumber.boolean
Two RationalNumbers are considered to be equal if both denominators and numerators are equal, respectively.float
Returns the float value ofthis.doubleValue()
.int
hashCode()
int
intValue()
Returns the integer value ofthis.doubleValue()
.long
Returns the long value ofthis.doubleValue()
.minus
(RationalNumber number) Subtracts the given number from this number.Returns the multiplicative inverse of this RationalNumber if it exists.protected void
Normalizes the RationalNumber by normalizing the signum and canceling both, numerator and denominator, by the greatest common divisor.protected void
Normalizes the signum such that if the RationalNumber is negative, the numerator will be negative, the denominator positive.plus
(RationalNumber number) Adds the given number to this number.short
Returns the short value ofthis.doubleValue()
.times
(RationalNumber number) Multiplies this number with the given number.toString()
Returns a String representation of this RationalNumber.
-
Field Details
-
ZERO
The canonical representation of zero as RationalNumber. -
ONE
The canonical representation of 1 as RationalNumber.
-
-
Constructor Details
-
RationalNumber
Constructs a RationalNumber for a given numerator and denominator. The denominator must not be 0.- Parameters:
numerator
- the numerator of the RationalNumberdenominator
- the denominator of the RationalNumber- Throws:
IllegalArgumentException
- ifdenominator.equals(
BigInteger.ZERO)
-
RationalNumber
Constructs a RationalNumber for a given numerator and denominator. The denominator must not be 0.- Parameters:
numerator
- the numerator of the RationalNumberdenominator
- the denominator of the RationalNumber- Throws:
IllegalArgumentException
- ifdenominator.equals(
BigInteger.ZERO)
-
RationalNumber
Constructs a RationalNumber out of the given double number.- Parameters:
number
- a double number to be represented as a RationalNumber- Throws:
IllegalArgumentException
- if the given Double is infinit or not a number
-
RationalNumber
Constructs a RationalNumber out of the given double number.- Parameters:
number
- a double number to be represented as a RationalNumber- Throws:
IllegalArgumentException
- if the given Double is infinit or not a number
-
RationalNumber
Constructs a RationalNumber for a given String representing a double.- Parameters:
doubleString
- a String representing a double number- Throws:
IllegalArgumentException
- if the given String represents a double number that is infinit or not a number
-
-
Method Details
-
normalize
protected void normalize()Normalizes the RationalNumber by normalizing the signum and canceling both, numerator and denominator, by the greatest common divisor. If the numerator is zero, the denominator is always one. -
normalizeSignum
protected void normalizeSignum()Normalizes the signum such that if the RationalNumber is negative, the numerator will be negative, the denominator positive. If the RationalNumber is positive, both, the numerator and the denominator will be positive. -
intValue
public int intValue()Returns the integer value ofthis.doubleValue()
. -
longValue
public long longValue()Returns the long value ofthis.doubleValue()
. -
floatValue
public float floatValue()Returns the float value ofthis.doubleValue()
.- Specified by:
floatValue
in classNumber
- See Also:
-
byteValue
public byte byteValue()Returns the byte value ofthis.doubleValue()
. -
shortValue
public short shortValue()Returns the short value ofthis.doubleValue()
.- Overrides:
shortValue
in classNumber
- See Also:
-
doubleValue
public double doubleValue()Returns the double value representation of this RationalNumber. The result is given by double division asnumerator.doubleValue() / denominator.doubleValue()
. Note that the result may not be exact. Thus afterRationalNumber a = new RationalNumber(b.doubleValue())
,a.equals(b)
is not necessarily true.- Specified by:
doubleValue
in classNumber
-
plus
Description copied from interface:Arithmetic
Adds the given number to this number.- Specified by:
plus
in interfaceArithmetic<RationalNumber>
- Parameters:
number
- the number to add to this number.- Returns:
- the result of arithmetic addition of this Number with the given number
-
times
Description copied from interface:Arithmetic
Multiplies this number with the given number.- Specified by:
times
in interfaceArithmetic<RationalNumber>
- Parameters:
number
- the number to multiply this number with- Returns:
- the result of arithmetic multiplication of this Number with the given number
-
minus
Description copied from interface:Arithmetic
Subtracts the given number from this number.- Specified by:
minus
in interfaceArithmetic<RationalNumber>
- Parameters:
number
- the number to subtract from this number- Returns:
- the result of arithmetic subtraction of the given number from this Number
-
divided
Description copied from interface:Arithmetic
Divides this number by the given number.- Specified by:
divided
in interfaceArithmetic<RationalNumber>
- Parameters:
number
- the number to divide this number by- Returns:
- the result of arithmetic division of this Number by the given number
- Throws:
ArithmeticException
- if the given divisor is 0
-
multiplicativeInverse
Returns the multiplicative inverse of this RationalNumber if it exists.- Returns:
- the multiplicative inverse of this rational number
- Throws:
ArithmeticException
- if numerator is 0 and hence the multiplicative inverse of this rational number does not exist
-
additiveInverse
Returns the additive inverse of this RationalNumber.- Returns:
- the additive inverse of this RationalNumber
-
absValue
Returns the absolute value of this rational number.- Returns:
- the absolute value of this rational number
-
compareTo
Compares two RationalNumbers a/b and c/d. Result is the same as(a*d).compareTo(c*b)
.- Specified by:
compareTo
in interfaceComparable<RationalNumber>
-
equals
Two RationalNumbers are considered to be equal if both denominators and numerators are equal, respectively. -
hashCode
public int hashCode() -
toString
Returns a String representation of this RationalNumber. The representation consists of the numerator, a separating " / ", and the denominator of the RationalNumber. -
copy
Provides a deep copy of this RationalNumber.- Returns:
- a deep copy of this RationalNumber
-