de.lmu.ifi.dbs.elki.data
Class RationalNumber

java.lang.Object
  extended by java.lang.Number
      extended by de.lmu.ifi.dbs.elki.data.RationalNumber
All Implemented Interfaces:
Arithmetic<RationalNumber>, Serializable, Comparable<RationalNumber>

public class RationalNumber
extends Number
implements Arithmetic<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.

See Also:
Serialized Form

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

serialVersionUID

private static final long serialVersionUID
Generated serial version UID.

See Also:
Constant Field Values

ZERO

public static final RationalNumber ZERO
The canonical representation of zero as RationalNumber.


ONE

public static final RationalNumber ONE
The canonical representation of 1 as RationalNumber.


numerator

private BigInteger numerator
Holding the numerator of the RationalNumber.


denominator

private BigInteger denominator
Holding the denominator of the RationalNumber.

Constructor Detail

RationalNumber

public RationalNumber(BigInteger numerator,
                      BigInteger denominator)
               throws IllegalArgumentException
Constructs a RationalNumber for a given numerator and denominator. The denominator must not be 0.

Parameters:
numerator - the numerator of the RationalNumber
denominator - the denominator of the RationalNumber
Throws:
IllegalArgumentException - if denominator.equals(BigInteger.ZERO)

RationalNumber

public RationalNumber(long numerator,
                      long denominator)
               throws IllegalArgumentException
Constructs a RationalNumber for a given numerator and denominator. The denominator must not be 0.

Parameters:
numerator - the numerator of the RationalNumber
denominator - the denominator of the RationalNumber
Throws:
IllegalArgumentException - if denominator.equals(BigInteger.ZERO)

RationalNumber

public RationalNumber(Double number)
               throws IllegalArgumentException
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

public RationalNumber(String doubleString)
               throws IllegalArgumentException
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 Detail

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 of this.doubleValue().

Specified by:
intValue in class Number
See Also:
doubleValue()

longValue

public long longValue()
Returns the long value of this.doubleValue().

Specified by:
longValue in class Number
See Also:
doubleValue()

floatValue

public float floatValue()
Returns the float value of this.doubleValue().

Specified by:
floatValue in class Number
See Also:
doubleValue()

byteValue

public byte byteValue()
Returns the byte value of this.doubleValue().

Overrides:
byteValue in class Number
See Also:
doubleValue()

shortValue

public short shortValue()
Returns the short value of this.doubleValue().

Overrides:
shortValue in class Number
See Also:
doubleValue()

doubleValue

public double doubleValue()
Returns the double value representation of this RationalNumber.

The result is given by double division as 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.

Specified by:
doubleValue in class Number

plus

public RationalNumber plus(RationalNumber number)
Description copied from interface: Arithmetic
Adds the given number to this number.

Specified by:
plus in interface Arithmetic<RationalNumber>
Parameters:
number - the number to add to this number.
Returns:
the result of arithmetic addition of this Number with the given number

times

public RationalNumber times(RationalNumber number)
Description copied from interface: Arithmetic
Multiplies this number with the given number.

Specified by:
times in interface Arithmetic<RationalNumber>
Parameters:
number - the number to multiply this number with
Returns:
the result of arithmetic multiplication of this Number with the given number

minus

public RationalNumber minus(RationalNumber number)
Description copied from interface: Arithmetic
Subtracts the given number from this number.

Specified by:
minus in interface Arithmetic<RationalNumber>
Parameters:
number - the number to subtract from this number
Returns:
the result of arithmetic subtraction of the given number from this Number

divided

public RationalNumber divided(RationalNumber number)
                       throws ArithmeticException
Description copied from interface: Arithmetic
Divides this number by the given number.

Specified by:
divided in interface Arithmetic<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

public RationalNumber multiplicativeInverse()
                                     throws ArithmeticException
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

public RationalNumber additiveInverse()
Returns the additive inverse of this RationalNumber.

Returns:
the additive inverse of this RationalNumber

absValue

public RationalNumber absValue()
Returns the absolute value of this rational number.

Returns:
the absolute value of this rational number

compareTo

public int compareTo(RationalNumber o)
Compares two RationalNumbers a/b and c/d. Result is the same as (a*d).compareTo(c*b).

Specified by:
compareTo in interface Comparable<RationalNumber>

equals

public boolean equals(Object obj)
Two RationalNumbers are considered to be equal if both denominators and numerators are equal, respectively.

Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Returns a String representation of this RationalNumber.

The representation consists of the numerator, a separating " / ", and the denominator of the RationalNumber.

Overrides:
toString in class Object

copy

public RationalNumber copy()
Provides a deep copy of this RationalNumber.

Returns:
a deep copy of this RationalNumber

test

public static void test(int n,
                        PrintStream out)
Compares doubles and RationalNumbers wrt efficiency and accuracy.

Parameters:
n - the number of random numbers
out - target to print results to

main

public static void main(String[] args)
Calls test for a given number of numbers and a optionally given target file.

Parameters:
args - <int> [<filename>]

Release 0.4.0 (2011-09-20_1324)