gishur.core.math
Class RationalNumber

java.lang.Object
  |
  +--java.lang.Number
        |
        +--gishur.core.math.MathNumber
              |
              +--gishur.core.math.RationalNumber
All Implemented Interfaces:
java.io.Serializable

public class RationalNumber
extends MathNumber

A rational Number based upon MathNumber. With rational numbers one can perform exact arithmetic - to the cost of space and time. A rational numbers stores a numerator() and a denominator(). All arithmetic methods are supported - with the exception of MathNumber.sqrt().

Version:
1.0
Author:
Thomas Wolf
See Also:
Serialized Form

Fields inherited from class gishur.core.math.MathNumber
MINUS_ONE, ONE, ZERO
 
Constructor Summary
RationalNumber(java.math.BigInteger n, java.math.BigInteger d)
           
RationalNumber(long n)
           
RationalNumber(long n, long d)
           
 
Method Summary
 MathNumber abs()
          Computes |this|.
 MathNumber add(MathNumber n)
          Computes this+n.
 java.math.BigInteger denominator()
          Returns the denominator of this rational number.
 MathNumber divide(MathNumber n)
          Computes this/n.
 double doubleValue()
          Returns the value of the number as a double.
 MathNumber invert()
          Computes 1/this.
 MathNumber multiply(MathNumber n)
          Computes this*n.
 MathNumber negate()
          Computes -this.
 void normalize()
          Normalizes this fraction.
 java.math.BigInteger numerator()
          Returns the numerator of this rational number.
 MathNumber pow(long n)
          Computes this^n.
 int sign()
          Determines the sign of this number.
 MathNumber subtract(MathNumber n)
          Computes this-n.
 
Methods inherited from class gishur.core.math.MathNumber
byteValue, compareTo, floatValue, intValue, log, longValue, max, min, shortValue, sqrt, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

RationalNumber

public RationalNumber(long n)

RationalNumber

public RationalNumber(long n,
                      long d)

RationalNumber

public RationalNumber(java.math.BigInteger n,
                      java.math.BigInteger d)
Method Detail

normalize

public final void normalize()
Normalizes this fraction.
Throws:
ArithmeticException - if the value of the denominator is zero.
MathNumberException - if either or both of the arguments are null.

numerator

public java.math.BigInteger numerator()
Returns the numerator of this rational number.
Returns:
numerator

denominator

public java.math.BigInteger denominator()
Returns the denominator of this rational number.
Returns:
denominator

sign

public int sign()
Determines the sign of this number. This method returns -1, 0, or 1 as this number is negative, zero, or positive.
Overrides:
sign in class MathNumber
Returns:
the sign of this number.
See Also:
RationalNumber

abs

public MathNumber abs()
Computes |this|.
Overrides:
abs in class MathNumber
Returns:
the absolute of this.

negate

public MathNumber negate()
Computes -this.
Overrides:
negate in class MathNumber
Returns:
the negative of this.

invert

public MathNumber invert()
Computes 1/this.
Overrides:
invert in class MathNumber
Returns:
result.
Throws:
ArithmeticException - if the value of this number is zero.

add

public MathNumber add(MathNumber n)
Computes this+n.
Overrides:
add in class MathNumber
Parameters:
n - the number to add to this.
Returns:
the sum of this and n.
Throws:
MathNumberException - if the operation does not support the type of n.

subtract

public MathNumber subtract(MathNumber n)
Computes this-n.
Overrides:
subtract in class MathNumber
Parameters:
n - the number to subtract from this.
Returns:
this minus n.
Throws:
MathNumberException - if the operation does not support the type of n.

multiply

public MathNumber multiply(MathNumber n)
Computes this*n.
Overrides:
multiply in class MathNumber
Parameters:
n - the number to multiply this by.
Returns:
the product of this and n.
Throws:
MathNumberException - if the operation does not support the type of n.

divide

public MathNumber divide(MathNumber n)
Computes this/n.
Overrides:
divide in class MathNumber
Parameters:
n - the number to divide this by.
Returns:
this divided by n.
Throws:
MathNumberException - if the operation does not support the type of n.
ArithmeticException - if the argument is zero.

pow

public MathNumber pow(long n)
Computes this^n.
Overrides:
pow in class MathNumber
Parameters:
n - the exponent.
Returns:
this raised to the power of n.
Throws:
MathNumberException - if this Method is not supported or if the argument is null.

doubleValue

public double doubleValue()
Returns the value of the number as a double.
Overrides:
doubleValue in class MathNumber
Returns:
number as double