|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.lang.Number
com.drew.lang.Rational
public class Rational
Immutable class for holding a rational number without loss of precision. Provides
a familiar representation via toString() in form numerator/denominator.
| 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 |
|---|
public Rational(long numerator,
long denominator)
| Method Detail |
|---|
public double doubleValue()
double.
This may involve rounding.
doubleValue in class Numberdouble.public float floatValue()
float.
This may involve rounding.
floatValue in class Numberfloat.public final byte byteValue()
byte.
This may involve rounding or truncation. This implementation simply
casts the result of doubleValue() to byte.
byteValue in class Numberbyte.public final int intValue()
int.
This may involve rounding or truncation. This implementation simply
casts the result of doubleValue() to int.
intValue in class Numberint.public final long longValue()
long.
This may involve rounding or truncation. This implementation simply
casts the result of doubleValue() to long.
longValue in class Numberlong.public final short shortValue()
short.
This may involve rounding or truncation. This implementation simply
casts the result of doubleValue() to short.
shortValue in class Numbershort.public final long getDenominator()
public final long getNumerator()
public Rational getReciprocal()
public boolean isInteger()
public String toString()
numerator/denominator.
toString in class Objectpublic String toSimpleString(boolean allowDecimal)
public boolean equals(Object obj)
Rational instances, returning true if they are mathematically
equivalent.
equals in class Objectobj - the Rational to compare this instance to.
obj is not an instance of Rational.public int hashCode()
hashCode in class Objectpublic 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
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||