org.rubato.math.arith
Class Rational

java.lang.Object
  extended by java.lang.Number
      extended by org.rubato.math.arith.Rational
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<Rational>

public final class Rational
extends java.lang.Number
implements java.lang.Comparable<Rational>, java.lang.Cloneable

Rational number arithmetic.

Author:
Gérard Milmeister
See Also:
Serialized Form

Constructor Summary
Rational(double d)
          Creates a new rational from a double d.
Rational(double d, int quant)
          Creates a new rational from a double d.
Rational(int n)
          Creates a new rational number n/1.
Rational(int n, int d)
          Creates a new rational n/d.
Rational(Rational r)
          Creates a new rational from r.
Rational(Rational r, int quant)
          Creates a new rational from another rational using given quantization.
Rational(java.lang.String s)
          Creates a new rational from its string representation s.
 
Method Summary
 Rational abs()
          Returns the absolute value of this rational.
 void add(int n)
          Adds the integer n to this number.
 void add(Rational r)
          Adds r to this number.
 java.lang.Object clone()
           
 int compareTo(Rational r)
           
 Rational difference(int n)
          Returns the difference of this number and the integer n.
 Rational difference(Rational r)
          Returns the difference of this number and r.
 void divide(int x)
          Divides this number by the integer n.
 void divide(Rational r)
          Divides this number by r.
 double doubleValue()
          Converts this rational to a double.
 boolean equals(java.lang.Object object)
           
 boolean equals(Rational r)
           
 float floatValue()
          Converts this rational to a float.
static int getDefaultQuantization()
          Returns the current default quantization;
 int getDenominator()
          Returns the denominator of this rational.
 int getNumerator()
          Returns the numerator of this rational.
static Rational getOne()
          Returns the rational 1/1.
static Rational getZero()
          Returns the rational 0/1.
 int hashCode()
          Returns the hashcode for this rational.
 int intValue()
           
 Rational inverse()
          Returns the inverse of this rational.
 void invert()
          Inverts this rational.
 boolean isIntegral()
           
 boolean isOne()
          Returns true iff this number is 1/1.
 boolean isZero()
          Returns true iff this number is 0/1.
 long longValue()
           
 void multiply(int n)
          Multiplies this number with the integer n.
 void multiply(Rational r)
          Multiplies this number with r.
 void negate()
          Negates this rational.
 Rational negated()
          Returns -this number.
static Rational parseRational(java.lang.String s)
          Returns the rational correspoding to its string representation s.
 Rational product(int n)
          Returns the product of this number and the integer n.
 Rational product(Rational r)
          Returns the product of this number and r.
 Rational quotient(int n)
          Returns the quotient of this number and the integer n.
 Rational quotient(Rational r)
          Returns the quotient of this number and r.
static void resetDefaultQuantization()
          Resets the default quantization to the initial value.
static void setDefaultQuantization(int quant)
          Sets the default quantization to the given value.
 void subtract(int n)
          Subtracts the integer n from this number.
 void subtract(Rational r)
          Subtracts r from this number.
 Rational sum(int n)
          Returns the sum of this number and the integer n.
 Rational sum(Rational r)
          Returns the sum of this number and r.
 java.lang.String toString()
          Returns the string representation of this rational.
 
Methods inherited from class java.lang.Number
byteValue, shortValue
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Rational

public Rational(int n)
Creates a new rational number n/1.


Rational

public Rational(int n,
                int d)
Creates a new rational n/d.

Parameters:
n - is the numerator
d - is the denominator

Rational

public Rational(Rational r)
Creates a new rational from r. Copy constructor.


Rational

public Rational(double d,
                int quant)
Creates a new rational from a double d. Converts d with quantization quant.


Rational

public Rational(double d)
Creates a new rational from a double d. Converts d with default quantization.


Rational

public Rational(Rational r,
                int quant)
Creates a new rational from another rational using given quantization. A new rational x/quant is created such that x/quant <= r < (x+1)/quant.


Rational

public Rational(java.lang.String s)
Creates a new rational from its string representation s.

Parameters:
s - has the format "n/d" where n and d are integers
Method Detail

getZero

public static Rational getZero()
Returns the rational 0/1.


getOne

public static Rational getOne()
Returns the rational 1/1.


equals

public boolean equals(java.lang.Object object)
Overrides:
equals in class java.lang.Object

equals

public boolean equals(Rational r)

isZero

public boolean isZero()
Returns true iff this number is 0/1.


isOne

public boolean isOne()
Returns true iff this number is 1/1.


compareTo

public int compareTo(Rational r)
Specified by:
compareTo in interface java.lang.Comparable<Rational>

sum

public Rational sum(Rational r)
Returns the sum of this number and r.


sum

public Rational sum(int n)
Returns the sum of this number and the integer n.


add

public void add(Rational r)
Adds r to this number.


add

public void add(int n)
Adds the integer n to this number.


difference

public Rational difference(Rational r)
Returns the difference of this number and r.


difference

public Rational difference(int n)
Returns the difference of this number and the integer n.


subtract

public void subtract(Rational r)
Subtracts r from this number.


subtract

public void subtract(int n)
Subtracts the integer n from this number.


product

public Rational product(Rational r)
Returns the product of this number and r.


product

public Rational product(int n)
Returns the product of this number and the integer n.


multiply

public void multiply(Rational r)
Multiplies this number with r.


multiply

public void multiply(int n)
Multiplies this number with the integer n.


quotient

public Rational quotient(Rational r)
Returns the quotient of this number and r.


quotient

public Rational quotient(int n)
Returns the quotient of this number and the integer n.


divide

public void divide(Rational r)
Divides this number by r.


divide

public void divide(int x)
Divides this number by the integer n.


inverse

public Rational inverse()
Returns the inverse of this rational.


invert

public void invert()
Inverts this rational.


negated

public Rational negated()
Returns -this number.


negate

public void negate()
Negates this rational.


getNumerator

public int getNumerator()
Returns the numerator of this rational.


getDenominator

public int getDenominator()
Returns the denominator of this rational.


abs

public Rational abs()
Returns the absolute value of this rational.


doubleValue

public double doubleValue()
Converts this rational to a double.

Specified by:
doubleValue in class java.lang.Number

floatValue

public float floatValue()
Converts this rational to a float.

Specified by:
floatValue in class java.lang.Number

longValue

public long longValue()
Specified by:
longValue in class java.lang.Number

intValue

public int intValue()
Specified by:
intValue in class java.lang.Number

parseRational

public static Rational parseRational(java.lang.String s)
Returns the rational correspoding to its string representation s.


isIntegral

public boolean isIntegral()

toString

public java.lang.String toString()
Returns the string representation of this rational.

Overrides:
toString in class java.lang.Object

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

hashCode

public int hashCode()
Returns the hashcode for this rational.

Overrides:
hashCode in class java.lang.Object

setDefaultQuantization

public static void setDefaultQuantization(int quant)
Sets the default quantization to the given value. The quantization is taken to be the absolute value of quant. If quant is 0, the initial default value is used.


resetDefaultQuantization

public static void resetDefaultQuantization()
Resets the default quantization to the initial value.


getDefaultQuantization

public static int getDefaultQuantization()
Returns the current default quantization;