com.drew.lang
Class Rational

java.lang.Object
  extended by java.lang.Number
      extended by com.drew.lang.Rational
All Implemented Interfaces:
Serializable

public class Rational
extends Number
implements Serializable

Immutable class for holding a rational number without loss of precision. Provides a familiar representation via toString() in form numerator/denominator.

Author:
Drew Noakes http://drewnoakes.com
See Also:
Serialized Form

Constructor Summary
Rational(long numerator, long denominator)
          Creates a new instance of Rational.
 
Method Summary
 byte byteValue()
          Returns the value of the specified number as a byte.
 double doubleValue()
          Returns the value of the specified number as a double.
 boolean equals(Object obj)
          Compares two Rational instances, returning true if they are mathematically equivalent.
 float floatValue()
          Returns the value of the specified number as a float.
 long getDenominator()
          Returns the denominator.
 long getNumerator()
          Returns the numerator.
 Rational getReciprocal()
          Returns the reciprocal value of this object as a new Rational.
 Rational getSimplifiedInstance()
           Simplifies the Rational number.
 int hashCode()
           
 int intValue()
          Returns the value of the specified number as an int.
 boolean isInteger()
          Checks if this rational number is an Integer, either positive or negative.
 long longValue()
          Returns the value of the specified number as a long.
 short shortValue()
          Returns the value of the specified number as a short.
 String toSimpleString(boolean allowDecimal)
          Returns the simplest representation of this Rational's value possible.
 String toString()
          Returns a string representation of the object of form numerator/denominator.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Rational

public Rational(long numerator,
                long denominator)
Creates a new instance of Rational. Rational objects are immutable, so once you've set your numerator and denominator values here, you're stuck with them!

Method Detail

doubleValue

public double doubleValue()
Returns the value of the specified number as a double. This may involve rounding.

Specified by:
doubleValue in class Number
Returns:
the numeric value represented by this object after conversion to type double.

floatValue

public float floatValue()
Returns the value of the specified number as a float. This may involve rounding.

Specified by:
floatValue in class Number
Returns:
the numeric value represented by this object after conversion to type float.

byteValue

public final byte byteValue()
Returns the value of the specified number as a byte. This may involve rounding or truncation. This implementation simply casts the result of doubleValue() to byte.

Overrides:
byteValue in class Number
Returns:
the numeric value represented by this object after conversion to type byte.

intValue

public final int intValue()
Returns the value of the specified number as an int. This may involve rounding or truncation. This implementation simply casts the result of doubleValue() to int.

Specified by:
intValue in class Number
Returns:
the numeric value represented by this object after conversion to type int.

longValue

public final long longValue()
Returns the value of the specified number as a long. This may involve rounding or truncation. This implementation simply casts the result of doubleValue() to long.

Specified by:
longValue in class Number
Returns:
the numeric value represented by this object after conversion to type long.

shortValue

public final short shortValue()
Returns the value of the specified number as a short. This may involve rounding or truncation. This implementation simply casts the result of doubleValue() to short.

Overrides:
shortValue in class Number
Returns:
the numeric value represented by this object after conversion to type short.

getDenominator

public final long getDenominator()
Returns the denominator.


getNumerator

public final long getNumerator()
Returns the numerator.


getReciprocal

public Rational getReciprocal()
Returns the reciprocal value of this object as a new Rational.

Returns:
the reciprocal in a new object

isInteger

public boolean isInteger()
Checks if this rational number is an Integer, either positive or negative.


toString

public String toString()
Returns a string representation of the object of form numerator/denominator.

Overrides:
toString in class Object
Returns:
a string representation of the object.

toSimpleString

public String toSimpleString(boolean allowDecimal)
Returns the simplest representation of this Rational's value possible.


equals

public boolean equals(Object obj)
Compares two Rational instances, returning true if they are mathematically equivalent.

Overrides:
equals in class Object
Parameters:
obj - the Rational to compare this instance to.
Returns:
true if instances are mathematically equivalent, otherwise false. Will also return false if obj is not an instance of Rational.

hashCode

public int hashCode()
Overrides:
hashCode in class Object

getSimplifiedInstance

public Rational getSimplifiedInstance()

Simplifies the Rational number.

Prime number series: 1, 2, 3, 5, 7, 9, 11, 13, 17

To reduce a rational, need to see if both numerator and denominator are divisible by a common factor. Using the prime number series in ascending order guarantees the minimum number of checks required.

However, generating the prime number series seems to be a hefty task. Perhaps it's simpler to check if both d & n are divisible by all numbers from 2 -> (Math.min(denominator, numerator) / 2). In doing this, one can check for 2 and 5 once, then ignore all even numbers, and all numbers ending in 0 or 5. This leaves four numbers from every ten to check.

Therefore, the max number of pairs of modulus divisions required will be:

    4   Math.min(denominator, numerator) - 1
   -- * ------------------------------------ + 2
   10                    2
 

Math.min(denominator, numerator) - 1 = ------------------------------------ + 2 5

Returns:
a simplified instance, or if the Rational could not be simplified, returns itself (unchanged)


Copyright © 2012. All Rights Reserved.