edu.umd.cfar.lamp.viper.geometry
Class Rational

java.lang.Object
  extended byjava.lang.Number
      extended byedu.umd.cfar.lamp.viper.geometry.Rational
All Implemented Interfaces:
java.lang.Comparable, java.io.Serializable

public class Rational
extends java.lang.Number
implements java.lang.Comparable

Represents numbers as a fraction with two BigIntegers. Useful for intersecting polygons and the like.

See Also:
Serialized Form

Constructor Summary
Rational()
          Constructs a new Rational.
Rational(long numerator)
          Constructs a new Rational with the given integer value.
Rational(long numerator, long denominator)
          Constructs a new Rational with the given value.
Rational(Rational old)
          Constructs a new Rational with the given value.
 
Method Summary
 Rational abs()
          Changes this to refer to its abolute value
 byte byteValue()
          Gets the byte approximation of this rational.
 java.math.BigInteger ceiling()
          Compute the ceiling as an integer.
 int compareTo(java.lang.Object o)
          Compares to the other number.
static void divide(Rational numerator, Rational denominator, Rational quotient)
          Divides two Rationals and stores the result in a third.
 double doubleValue()
          Gets the double approximation of this rational.
 boolean equals(int other)
          Tests the equality of this Rational with an int.
 boolean equals(java.lang.Object other)
          Tests the equality of two Rationals or Numbers.
 float floatValue()
          Gets the float approximation of this rational.
 java.math.BigInteger floor()
          Compute the floor as an integer.
 boolean greaterThan(int other)
          Checks to see if this is greater than an int.
 boolean greaterThan(Rational other)
          Checks to see if this is greater than another Rational.
 boolean greaterThanEqualTo(Rational other)
          Checks to see if this is greater than or equal to another Rational.
 int hashCode()
          Get a hashcode for this number.
 int intValue()
          Gets the int approximation of this rational.
 boolean isNegative()
          Tests to see if the number is negative.
 boolean isPositive()
          Tests to see if the number is positive.
 boolean isZero()
          Tests to see if the number is zero.
 boolean lessThan(int other)
          Checks to see if this is less than an int.
 boolean lessThan(Rational other)
          Checks to see if this is less than another Rational.
 boolean lessThanEqualTo(Rational other)
          Checks to see if this is less than or equal to another Rational.
 long longValue()
          Gets the long approximation of this rational.
static void minus(Rational first, Rational second, Rational difference)
          Subtracts one Rational from another and stores the result into a third.
static void multiply(Rational first, long second, Rational result)
          Multiplies a Rational with a long and stores the result into another Rational.
static void multiply(Rational first, Rational second, Rational result)
          Multiplies two Rationals and stores the result into a third.
 Rational negate()
          Turns negates fraction upside-down, by negating its numerator.
static Rational parseRational(java.lang.String valStr)
           
static void plus(Rational first, Rational second, Rational result)
          Subtracts one Rational from another and stores the result into a third.
 Rational reciprocate()
          Turns this fraction upside-down, by turning it to its reciprocal.
 Rational setTo(long numerator)
          Set this to the given value.
 Rational setTo(long numerator, long denominator)
          Set this to the given value.
 Rational setTo(Rational old)
          Set this to the given value.
 short shortValue()
          Gets the short approximation of this rational.
 Rational square()
          Square a Rational.
 java.lang.String toString()
          Gets a string representation.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Rational

public Rational()
Constructs a new Rational. Defaults to NaN.


Rational

public Rational(long numerator)
Constructs a new Rational with the given integer value.

Parameters:
numerator - Integer value to give the Rational.

Rational

public Rational(long numerator,
                long denominator)
Constructs a new Rational with the given value.

Parameters:
numerator - The number on top of the fraction.
denominator - The number on bottom.

Rational

public Rational(Rational old)
Constructs a new Rational with the given value. (Copy Constructor.)

Parameters:
old - Rational value to give the new one.
Method Detail

setTo

public Rational setTo(Rational old)
Set this to the given value.

Parameters:
old - Rational to set this to.
Returns:
this after getting set.

setTo

public Rational setTo(long numerator)
Set this to the given value.

Parameters:
numerator - An integer value to set this to.
Returns:
this after getting set.

setTo

public Rational setTo(long numerator,
                      long denominator)
Set this to the given value.

Parameters:
numerator - The numerator of the fraction.
denominator - The denominator of the fraction.
Returns:
this after getting set.

toString

public java.lang.String toString()
Gets a string representation. Either -inf, +inf, NAN, or the numerator / denominator.

Returns:
The fraction as a String.

hashCode

public int hashCode()
Get a hashcode for this number.

Returns:
The numerator xored with the denominator.

equals

public boolean equals(java.lang.Object other)
Tests the equality of two Rationals or Numbers.

Parameters:
other - The object to compare against this rational number.
Returns:
true if they are equal.

equals

public boolean equals(int other)
Tests the equality of this Rational with an int.

Parameters:
other - The int to compare against this rational number.
Returns:
true if they are equal.

isZero

public boolean isZero()
Tests to see if the number is zero.

Returns:
true if the number is zero

isNegative

public boolean isNegative()
Tests to see if the number is negative.

Returns:
true if the number is less than zero

isPositive

public boolean isPositive()
Tests to see if the number is positive.

Returns:
true if the number is greater than zero

lessThan

public boolean lessThan(Rational other)
Checks to see if this is less than another Rational.

Parameters:
other - the Rational to test against.
Returns:
true iff this is less than the other.

lessThan

public boolean lessThan(int other)
Checks to see if this is less than an int.

Parameters:
other - the int to test against.
Returns:
true iff this is less than the other.

greaterThan

public boolean greaterThan(int other)
Checks to see if this is greater than an int.

Parameters:
other - the int to test against.
Returns:
true iff this is greater than the other.

greaterThan

public boolean greaterThan(Rational other)
Checks to see if this is greater than another Rational.

Parameters:
other - the number to test against.
Returns:
true iff this is greater than the other.

lessThanEqualTo

public boolean lessThanEqualTo(Rational other)
Checks to see if this is less than or equal to another Rational.

Parameters:
other - the number to test against.
Returns:
true iff this is less than or equal to the other.

greaterThanEqualTo

public boolean greaterThanEqualTo(Rational other)
Checks to see if this is greater than or equal to another Rational.

Parameters:
other - the number to test against.
Returns:
true iff this is greater than or equal to the other.

compareTo

public int compareTo(java.lang.Object o)
Compares to the other number.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
o - the number to compare against
Returns:
zero if they are equal, negative if o is greater, and positive if this is greater

doubleValue

public double doubleValue()
Gets the double approximation of this rational.

Returns:
a double close to the value of this

floatValue

public float floatValue()
Gets the float approximation of this rational.

Returns:
a float close to the value of this

byteValue

public byte byteValue()
Gets the byte approximation of this rational.

Returns:
the byte closest to the value of this

shortValue

public short shortValue()
Gets the short approximation of this rational.

Returns:
the short closest to the value of this

intValue

public int intValue()
Gets the int approximation of this rational.

Returns:
the int closest to the value of this

floor

public java.math.BigInteger floor()
Compute the floor as an integer.

Returns:
the floor. This is equivalent to num / div, where they are both integers.

ceiling

public java.math.BigInteger ceiling()
Compute the ceiling as an integer.

Returns:
the ceiling

longValue

public long longValue()
Gets the long approximation of this rational.

Returns:
the long closest to the value of this

plus

public static void plus(Rational first,
                        Rational second,
                        Rational result)
Subtracts one Rational from another and stores the result into a third. Does not clobber result, so you can pass it all the same reference and end up with twice the original just fine.

Parameters:
first - A Rational to add to.
second - A Rational to add.
result - Where to store the result.

minus

public static void minus(Rational first,
                         Rational second,
                         Rational difference)
Subtracts one Rational from another and stores the result into a third. Keeps the order, so you can pass it all the same reference and end up with zero just fine.

Parameters:
first - a Rational to subtract something from.
second - a Rational to subtract from the first.
difference - where to store the result.

multiply

public static void multiply(Rational first,
                            Rational second,
                            Rational result)
Multiplies two Rationals and stores the result into a third. Keeps the order, so you can pass it all the same reference and end up with the square just fine.

Parameters:
first - A Rational to multiply.
second - Another Rational to multiply.
result - Where to store the result.

multiply

public static void multiply(Rational first,
                            long second,
                            Rational result)
Multiplies a Rational with a long and stores the result into another Rational. Keeps the order, so you can pass it all the same reference and end up fine.

Parameters:
first - A Rational to multiply.
second - A long to multiply.
result - Where to store the result.

divide

public static void divide(Rational numerator,
                          Rational denominator,
                          Rational quotient)
Divides two Rationals and stores the result in a third. Keeps the order, so you can pass it all the same reference and end up with 1 just fine.

Parameters:
numerator - The thing on top, or divisor.
denominator - The thing on the bottom, or dividend.
quotient - Where to store the answer.

negate

public Rational negate()
Turns negates fraction upside-down, by negating its numerator.

Returns:
Itself, now the opposite of what it was.

reciprocate

public Rational reciprocate()
Turns this fraction upside-down, by turning it to its reciprocal.

Returns:
Itself, now upside-down.

abs

public Rational abs()
Changes this to refer to its abolute value

Returns:
this

square

public Rational square()
Square a Rational. Sets this to its square.

Returns:
this, now squared.

parseRational

public static Rational parseRational(java.lang.String valStr)
Parameters:
valStr -
Returns: