org.openmali.number
Class Rational

java.lang.Object
  extended by org.openmali.number.Rational
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<Rational>

public class Rational
extends java.lang.Object
implements java.lang.Comparable<Rational>, java.io.Serializable

Represents a rational number. i.e. an integer over an integer. In order to represent a wide range of values, the data is stored as x + a/b instead of just a/b. So the resultant range of values is slightly wider than the normal integer range. addition, subtraction,multiplication and division are defined.

See Also:
Serialized Form

Field Summary
static int APPROXIMATION_THRESHOLD
           
static boolean AUTO_SIMPLIFY
          should the rational numbers be simplified after every operation?
static Rational ONE
           
static Rational ZERO
           
 
Constructor Summary
Rational()
           
Rational(float val)
          initializes the rational to an APPROXIMATION of the value of the float parameter, note you should avoid using this method where possible
Rational(int val)
           
Rational(int num, int den)
           
Rational(int whole, int num, int den)
          this = whole + num/den
Rational(Rational val)
           
 
Method Summary
static Rational add(Rational a, Rational b, Rational passback)
          result = a+b this operation can be performed in place i.e. the passback can be the same object as a or b
 Rational clone()
          
 int compareTo(Rational o)
           
static Rational div(Rational a, Rational b, Rational passback)
          result = a/b this operation can be performed in place i.e. the passback can be the same object as a or b
 boolean equals(java.lang.Object o)
          
 float floatValue()
           
static int gcd(int a, int b)
          finds the greatest common divisor of two integers
 int getDenominator()
           
 int getNumerator()
           
 int getWhole()
           
 int hashCode()
          
static Rational mul(Rational a, Rational b, Rational passback)
          result = a*b this operation can be performed in place i.e. the passback can be the same object as a or b
 void negate()
           
static Rational parseRational(java.lang.String string)
          Reads a single rational number from a string
 void set(float value)
          initializes the rational to an APPROXIMATION of the value of the float parameter, note you should avoid using this method where possible
 Rational set(int whole, int num, int den)
           
 void set(Rational radical)
           
 void simplify()
          attepts to simplify the rational number by dividing by the greatest common devisor, note this is done automatically if AUTO_SIMPLIFY is set to true
static Rational sub(Rational a, Rational b, Rational passback)
          result = a-b this operation can be performed in place i.e. the passback can be the same object as a or b
 java.lang.String toString()
          returns a string representation of the number, note: this is compatable with parseRational and the Parser class
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

AUTO_SIMPLIFY

public static boolean AUTO_SIMPLIFY
should the rational numbers be simplified after every operation?


APPROXIMATION_THRESHOLD

public static int APPROXIMATION_THRESHOLD

ZERO

public static final Rational ZERO

ONE

public static final Rational ONE
Constructor Detail

Rational

public Rational(int num,
                int den)

Rational

public Rational()

Rational

public Rational(int whole,
                int num,
                int den)
this = whole + num/den


Rational

public Rational(float val)
initializes the rational to an APPROXIMATION of the value of the float parameter, note you should avoid using this method where possible

Parameters:
val -

Rational

public Rational(int val)

Rational

public Rational(Rational val)
Method Detail

set

public final Rational set(int whole,
                          int num,
                          int den)

set

public final void set(float value)
initializes the rational to an APPROXIMATION of the value of the float parameter, note you should avoid using this method where possible


set

public final void set(Rational radical)

negate

public final void negate()

floatValue

public final float floatValue()

getDenominator

public final int getDenominator()

getWhole

public final int getWhole()

getNumerator

public final int getNumerator()

simplify

public void simplify()
attepts to simplify the rational number by dividing by the greatest common devisor, note this is done automatically if AUTO_SIMPLIFY is set to true


add

public static Rational add(Rational a,
                           Rational b,
                           Rational passback)
result = a+b this operation can be performed in place i.e. the passback can be the same object as a or b


sub

public static Rational sub(Rational a,
                           Rational b,
                           Rational passback)
result = a-b this operation can be performed in place i.e. the passback can be the same object as a or b


mul

public static Rational mul(Rational a,
                           Rational b,
                           Rational passback)
result = a*b this operation can be performed in place i.e. the passback can be the same object as a or b


div

public static Rational div(Rational a,
                           Rational b,
                           Rational passback)
result = a/b this operation can be performed in place i.e. the passback can be the same object as a or b


gcd

public static int gcd(int a,
                      int b)
finds the greatest common divisor of two integers

Returns:
greatest common divosor of the two arguments

clone

public Rational clone()

Overrides:
clone in class java.lang.Object

toString

public java.lang.String toString()
returns a string representation of the number, note: this is compatable with parseRational and the Parser class

Overrides:
toString in class java.lang.Object
Returns:
the string representation

equals

public boolean equals(java.lang.Object o)

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()

Overrides:
hashCode in class java.lang.Object

compareTo

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

parseRational

public static Rational parseRational(java.lang.String string)
Reads a single rational number from a string

Returns:
the parsed Rational