Module math

Class RationalNumber

java.lang.Object
java.lang.Number
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.
Author:
Arthur Zimek
See Also:
  • Field Details

    • 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.
  • Constructor Details

    • RationalNumber

      public RationalNumber(BigInteger numerator, BigInteger denominator)
      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(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 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 of this.doubleValue().
      Specified by:
      intValue in class Number
      See Also:
    • longValue

      public long longValue()
      Returns the long value of this.doubleValue().
      Specified by:
      longValue in class Number
      See Also:
    • floatValue

      public float floatValue()
      Returns the float value of this.doubleValue().
      Specified by:
      floatValue in class Number
      See Also:
    • byteValue

      public byte byteValue()
      Returns the byte value of this.doubleValue().
      Overrides:
      byteValue in class Number
      See Also:
    • shortValue

      public short shortValue()
      Returns the short value of this.doubleValue().
      Overrides:
      shortValue in class Number
      See Also:
    • 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