Rational Class Reference

Inherits java::io::Serializable.

Collaboration diagram for Rational:

Collaboration graph
[legend]

List of all members.

Public Member Functions

final byte byteValue ()
double doubleValue ()
boolean equals (Object obj)
float floatValue ()
final int getDenominator ()
final int getNumerator ()
Rational getReciprocal ()
Rational getSimplifiedInstance ()
int hashCode ()
final int intValue ()
boolean isInteger ()
final long longValue ()
 Rational (int numerator, int denominator)
final short shortValue ()
String toSimpleString (boolean allowDecimal)
String toString ()

Private Member Functions

boolean tooComplexForSimplification ()

Private Attributes

final int denominator
int maxSimplificationCalculations = 1000
final int numerator


Detailed Description

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

Constructor & Destructor Documentation

Rational ( int  numerator,
int  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!


Member Function Documentation

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.

Returns:
the numeric value represented by this object after conversion to type byte.

double doubleValue (  ) 

Returns the value of the specified number as a double. This may involve rounding.

Returns:
the numeric value represented by this object after conversion to type double.

boolean equals ( Object  obj  ) 

Compares two Rational instances, returning true if they are mathematically equivalent.

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.

float floatValue (  ) 

Returns the value of the specified number as a float. This may involve rounding.

Returns:
the numeric value represented by this object after conversion to type float.

final int getDenominator (  ) 

Returns the denominator.

final int getNumerator (  ) 

Returns the numerator.

Rational getReciprocal (  ) 

Returns the reciprocal value of this obejct as a new Rational.

Returns:
the reciprocal in a new object

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 minimun 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 simpliffied, returns itself (unchanged)

int hashCode (  ) 

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.

Returns:
the numeric value represented by this object after conversion to type int.

boolean isInteger (  ) 

Checks if this rational number is an Integer, either positive or negative.

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.

Returns:
the numeric value represented by this object after conversion to type long.

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.

Returns:
the numeric value represented by this object after conversion to type short.

boolean tooComplexForSimplification (  )  [private]

Decides whether a brute-force simplification calculation should be avoided by comparing the maximum number of possible calculations with some threshold.

Returns:
true if the simplification should be performed, otherwise false

String toSimpleString ( boolean  allowDecimal  ) 

Returns the simplest represenation of this Rational's value possible.

String toString (  ) 

Returns a string representation of the object of form numerator/denominator.

Returns:
a string representation of the object.


Member Data Documentation

final int denominator [private]

Holds the denominator.

int maxSimplificationCalculations = 1000 [private]

final int numerator [private]

Holds the numerator.


The documentation for this class was generated from the following file:

Generated on Sat May 26 06:42:29 2012 for Project Wonder by  doxygen 1.5.8