|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.lang.Number de.lmu.ifi.dbs.elki.data.RationalNumber
public class 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.
Field Summary | |
---|---|
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 Summary | |
---|---|
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(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. |
Method Summary | |
---|---|
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() . |
static void |
main(String[] args)
Calls test for a given number of numbers and a optionally given target file. |
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() . |
static void |
test(int n,
PrintStream out)
Compares doubles and RationalNumbers wrt efficiency and accuracy. |
RationalNumber |
times(RationalNumber number)
Multiplies this number with the given number. |
String |
toString()
Returns a String representation of this RationalNumber. |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
private static final long serialVersionUID
public static final RationalNumber ZERO
public static final RationalNumber ONE
private BigInteger numerator
private BigInteger denominator
Constructor Detail |
---|
public RationalNumber(BigInteger numerator, BigInteger denominator) throws IllegalArgumentException
numerator
- the numerator of the RationalNumberdenominator
- the denominator of the RationalNumber
IllegalArgumentException
- if denominator.equals(
BigInteger.ZERO)
public RationalNumber(long numerator, long denominator) throws IllegalArgumentException
numerator
- the numerator of the RationalNumberdenominator
- the denominator of the RationalNumber
IllegalArgumentException
- if denominator.equals(
BigInteger.ZERO)
public RationalNumber(Double number) throws IllegalArgumentException
number
- a double number to be represented as a RationalNumber
IllegalArgumentException
- if the given Double is infinit or not a
numberpublic RationalNumber(String doubleString) throws IllegalArgumentException
doubleString
- a String representing a double number
IllegalArgumentException
- if the given String represents a double
number that is infinit or not a numberMethod Detail |
---|
protected 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 with
public RationalNumber minus(RationalNumber number)
Arithmetic
minus
in interface Arithmetic<RationalNumber>
number
- the number to subtract from this number
public RationalNumber divided(RationalNumber number) throws ArithmeticException
Arithmetic
divided
in interface Arithmetic<RationalNumber>
number
- the number to divide this number by
ArithmeticException
- 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)
equals
in class Object
public int hashCode()
hashCode
in class Object
public String toString()
toString
in class Object
public RationalNumber copy()
public static void test(int n, PrintStream out)
n
- the number of random numbersout
- target to print results topublic static void main(String[] args)
args
- <int> [<filename>]
|
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |