| 
sdr 0.7 | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.lang.Number
net.cscott.sdr.util.Fraction
public class Fraction
Fraction is a Number which implements
 exact arithmetic on fractions.  All computations which do not
 overflow can be done without any accumulated rounding errors.
The implementation of the Fraction class is based on
 the algorithms described in Donald E. Knuth's The Art of Computer
 Programming, volume 2, sections 4.5 through 4.5.2.  In particular,
 fractions are represented as a pair of integers (n/d)
 where n and d are relatively prime to
 each other and d>0.  The number zero is represented
 as (0/1).  Fractions are always maintained in "simplest
 form" and numerically-equal fractions always return true from the
 equals() method.
(Note however that there is a "backwards-compatibility" mode enabled
 if you use the deprecated getFraction() constructor; in this
 case it will appear that unsimplified fractions are maintained, and
 !getFraction(1,2).equals(getFraction(2,4)).  Use of
 getFraction() is not recommended.)
 
This class is immutable, and interoperable with most methods that accept
 a Number.
| Field Summary | |
|---|---|
static Fraction | 
FIVE_EIGHTHS
Fraction representation of 5/8. | 
static Fraction | 
FOUR
Fraction representation of 4. | 
static Fraction | 
FOUR_FIFTHS
Fraction representation of 4/5. | 
static Fraction | 
mFOUR
Fraction representation of -4. | 
static Fraction | 
mONE
Fraction representation of -1. | 
static Fraction | 
mONE_HALF
Fraction representation of -1/2. | 
static Fraction | 
mONE_QUARTER
Fraction representation of -1/4. | 
static Fraction | 
mONE_THIRD
Fraction representation of -1/3. | 
static Fraction | 
mTHREE
Fraction representation of -3. | 
static Fraction | 
mTHREE_HALVES
Fraction representation of -3/2. | 
static Fraction | 
mTHREE_QUARTERS
Fraction representation of -3/4. | 
static Fraction | 
mTWO
Fraction representation of -2. | 
static Fraction | 
mTWO_THIRDS
Fraction representation of -2/3. | 
static Fraction | 
ONE
Fraction representation of 1. | 
static Fraction | 
ONE_EIGHTH
Fraction representation of 1/8. | 
static Fraction | 
ONE_FIFTH
Fraction representation of 1/5. | 
static Fraction | 
ONE_HALF
Fraction representation of 1/2. | 
static Fraction | 
ONE_QUARTER
Fraction representation of 1/4. | 
static Fraction | 
ONE_THIRD
Fraction representation of 1/3. | 
static Fraction | 
SEVEN_EIGHTHS
Fraction representation of 7/8. | 
static Fraction | 
THREE
Fraction representation of 3. | 
static Fraction | 
THREE_EIGHTHS
Fraction representation of 3/8. | 
static Fraction | 
THREE_FIFTHS
Fraction representation of 3/5. | 
static Fraction | 
THREE_HALVES
Fraction representation of 3/2. | 
static Fraction | 
THREE_QUARTERS
Fraction representation of 3/4. | 
static Fraction | 
TWO
Fraction representation of 2. | 
static Fraction | 
TWO_FIFTHS
Fraction representation of 2/5. | 
static Fraction | 
TWO_THIRDS
Fraction representation of 2/3. | 
static Fraction | 
ZERO
Fraction representation of 0. | 
| Method Summary | |
|---|---|
 Fraction | 
abs()
Gets a fraction that is the positive equivalent of this one.  | 
 Fraction | 
add(Fraction fraction)
Adds the value of this fraction to another.  | 
 int | 
compareTo(Fraction other)
Compares this object to another based on size.  | 
 Fraction | 
divide(Fraction fraction)
Divide the value of this fraction by another.  | 
 Fraction | 
divideBy(Fraction fraction)
Deprecated. Renamed to divide()  | 
 double | 
doubleValue()
Gets the fraction as a double. | 
 boolean | 
equals(Object obj)
Compares this fraction to another object to test if they are equal.  | 
 float | 
floatValue()
Gets the fraction as a float. | 
 int | 
floor()
Return the largest integer less than or equal to this fraction.  | 
 int | 
getDenominator()
Gets the denominator part of the fraction.  | 
static Fraction | 
getFraction(double value)
Deprecated. Renamed this method to valueOf() for consistency.  | 
static Fraction | 
getFraction(int numerator,
            int denominator)
Deprecated. For reasons of backwards-compatibility, this method does not simplify fractions. The Fraction objects returned are thus subject to overflow. It should not be used in new code. Use valueOf() instead.  | 
static Fraction | 
getFraction(int whole,
            int numerator,
            int denominator)
Deprecated. For reasons of backwards-compatibility, this method does not simplify fractions. The Fraction objects returned are thus subject to overflow. It should not be used in new code. Use valueOf() instead.  | 
static Fraction | 
getFraction(String str)
Deprecated. For reasons of backwards-compatibility, this method does not simplify fractions. The Fraction objects returned are thus subject to overflow. It should not be used in new code. Use valueOf() instead.  | 
 int | 
getNumerator()
Gets the numerator part of the fraction.  | 
 int | 
getProperNumerator()
Gets the proper numerator, always positive.  | 
 int | 
getProperWhole()
Deprecated. Returns unexpected values for numbers between -1 and 0  | 
static Fraction | 
getReducedFraction(int numerator,
                   int denominator)
Deprecated. All fractions are now stored as reduced fractions; use the valueOf() method instead.  | 
 int | 
hashCode()
Gets a hashCode for the fraction.  | 
 int | 
intValue()
Gets the fraction as an int. | 
 Fraction | 
invert()
Gets a fraction that is the inverse (1/fraction) of this one.  | 
 long | 
longValue()
Gets the fraction as a long. | 
 Fraction | 
multiply(Fraction fraction)
Multiplies the value of this fraction by another.  | 
 Fraction | 
multiplyBy(Fraction fraction)
Deprecated. Renamed to multiply()  | 
 Fraction | 
negate()
Gets a fraction that is the negative (-fraction) of this one.  | 
 Fraction | 
pow(int power)
Gets a fraction that is raised to the passed in power.  | 
 Fraction | 
quantize(int nDenom)
Quantize the given fraction to the nearest x/nDenom.  | 
 Fraction | 
reduce()
Deprecated. Unless you are using deprecated methods this operation is a no-op.  | 
 String | 
repr()
Returns an executable representation of this Fraction.  | 
 Fraction | 
subtract(Fraction fraction)
Subtracts the value of another fraction from the value of this one.  | 
 String | 
toProperString()
Gets the fraction as a proper String in the format X Y/Z. | 
 String | 
toString()
Gets the fraction as a String. | 
static Fraction | 
valueOf(double value)
Creates a Fraction instance from a double value. | 
static Fraction | 
valueOf(int value)
Creates a Fraction instance from a int value. | 
static Fraction | 
valueOf(int numerator,
        int denominator)
Creates a Fraction instance with the 2 parts
 of a fraction Y/Z. | 
static Fraction | 
valueOf(int whole,
        int numerator,
        int denominator)
Creates a Fraction instance with the 3 parts
 of a fraction X Y/Z. | 
static Fraction | 
valueOf(String str)
Creates a Fraction from a String. | 
| Methods inherited from class java.lang.Number | 
|---|
byteValue, shortValue | 
| Methods inherited from class java.lang.Object | 
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait | 
| Field Detail | 
|---|
public static final Fraction ZERO
Fraction representation of 0.
public static final Fraction mONE
Fraction representation of -1.
public static final Fraction ONE
Fraction representation of 1.
public static final Fraction mTWO
Fraction representation of -2.
public static final Fraction TWO
Fraction representation of 2.
public static final Fraction mTHREE
Fraction representation of -3.
public static final Fraction THREE
Fraction representation of 3.
public static final Fraction mFOUR
Fraction representation of -4.
public static final Fraction FOUR
Fraction representation of 4.
public static final Fraction mONE_HALF
Fraction representation of -1/2.
public static final Fraction ONE_HALF
Fraction representation of 1/2.
public static final Fraction mTHREE_HALVES
Fraction representation of -3/2.
public static final Fraction THREE_HALVES
Fraction representation of 3/2.
public static final Fraction mONE_THIRD
Fraction representation of -1/3.
public static final Fraction ONE_THIRD
Fraction representation of 1/3.
public static final Fraction mTWO_THIRDS
Fraction representation of -2/3.
public static final Fraction TWO_THIRDS
Fraction representation of 2/3.
public static final Fraction mONE_QUARTER
Fraction representation of -1/4.
public static final Fraction ONE_QUARTER
Fraction representation of 1/4.
public static final Fraction mTHREE_QUARTERS
Fraction representation of -3/4.
public static final Fraction THREE_QUARTERS
Fraction representation of 3/4.
public static final Fraction ONE_FIFTH
Fraction representation of 1/5.
public static final Fraction TWO_FIFTHS
Fraction representation of 2/5.
public static final Fraction THREE_FIFTHS
Fraction representation of 3/5.
public static final Fraction FOUR_FIFTHS
Fraction representation of 4/5.
public static final Fraction ONE_EIGHTH
Fraction representation of 1/8.
public static final Fraction THREE_EIGHTHS
Fraction representation of 3/8.
public static final Fraction FIVE_EIGHTHS
Fraction representation of 5/8.
public static final Fraction SEVEN_EIGHTHS
Fraction representation of 7/8.
| Method Detail | 
|---|
public static Fraction getFraction(int numerator,
                                   int denominator)
Creates a Fraction instance with the 2 parts
 of a fraction Y/Z.
Any negative signs are resolved to be on the numerator. Creates UNREDUCED fractions for backwards-compatibility.
numerator - the numerator, for example the three in 'three sevenths'denominator - the denominator, for example the seven in 'three sevenths'
ArithmeticException - if the denominator is zero
public static Fraction valueOf(int numerator,
                               int denominator)
Creates a Fraction instance with the 2 parts
 of a fraction Y/Z.
Any negative signs are resolved to be on the numerator, zeros are normalized to (0/1), and all fractions are simplified.
numerator - the numerator, for example the three in 'three sevenths'denominator - the denominator, for example the seven in 'three sevenths'
ArithmeticException - if the denominator is zeropublic static Fraction valueOf(int value)
Creates a Fraction instance from a int value.
value - the integer value to convert
public static Fraction getFraction(int whole,
                                   int numerator,
                                   int denominator)
Creates a Fraction instance with the 3 parts
 of a fraction X Y/Z.
 Creates UNREDUCED fractions for backwards-compatibility.
The negative sign must be passed in on the whole number part.
whole - the whole number, for example the one in 'one and three sevenths'numerator - the numerator, for example the three in 'one and three sevenths'denominator - the denominator, for example the seven in 'one and three sevenths'
ArithmeticException - if the denominator is zero
ArithmeticException - if the denominator is negative
ArithmeticException - if the numerator is negative
ArithmeticException - if the resulting numerator exceeds 
  Integer.MAX_VALUE
public static Fraction valueOf(int whole,
                               int numerator,
                               int denominator)
Creates a Fraction instance with the 3 parts
 of a fraction X Y/Z.
The negative sign must be passed in on the whole number part.
whole - the whole number, for example the one in 'one and three sevenths'numerator - the numerator, for example the three in 'one and three sevenths'denominator - the denominator, for example the seven in 'one and three sevenths'
ArithmeticException - if the denominator is zero
ArithmeticException - if the denominator is negative
ArithmeticException - if the numerator is negative
ArithmeticException - if the resulting numerator exceeds 
  Integer.MAX_VALUE
public static Fraction getReducedFraction(int numerator,
                                          int denominator)
Creates a Fraction instance with the 2 parts
 of a fraction Y/Z.
Any negative signs are resolved to be on the numerator.
numerator - the numerator, for example the three in 'three sevenths'denominator - the denominator, for example the seven in 'three sevenths'
ArithmeticException - if the denominator is zeropublic static Fraction getFraction(double value)
Creates a Fraction instance from a double value.
This method uses the continued fraction algorithm, computing a maximum of 25 convergents and bounding the denominator by 10,000.
value - the double value to convert
ArithmeticException - if |value| > Integer.MAX_VALUE 
  or value = NaN
ArithmeticException - if the calculated denominator is zero
ArithmeticException - if the the algorithm does not convergepublic static Fraction valueOf(double value)
Creates a Fraction instance from a double value.
This method uses the continued fraction algorithm, computing a maximum of 25 convergents and bounding the denominator by 10,000.
value - the double value to convert
ArithmeticException - if |value| > Integer.MAX_VALUE 
  or value = NaN
ArithmeticException - if the calculated denominator is zero
ArithmeticException - if the the algorithm does not convergepublic static Fraction valueOf(String str)
Creates a Fraction from a String.
The formats accepted are:
double String containing a dot
str - the string to parse, must not be null
Fraction instance
IllegalArgumentException - if the string is null
NumberFormatException - if the number format is invalidpublic static Fraction getFraction(String str)
Creates a Fraction from a String.
 Creates UNREDUCED fractions for backwards-compatibility.
The formats accepted are:
double String containing a dot
str - the string to parse, must not be null
Fraction instance
IllegalArgumentException - if the string is null
NumberFormatException - if the number format is invalidpublic int getNumerator()
Gets the numerator part of the fraction.
This method may return a value greater than the denominator, an improper fraction, such as the seven in 7/4.
The numerator and denominator will always be relatively prime unless deprecated methods are used.
public int getDenominator()
Gets the denominator part of the fraction.
The numerator and denominator will always be relatively prime unless deprecated methods are used. The denominator will always be greater than zero.
public int getProperNumerator()
Gets the proper numerator, always positive.
An improper fraction 7/4 can be resolved into a proper one, 1 3/4. This method returns the 3 from the proper fraction.
If the fraction is negative such as -7/4, it can be resolved into -1 3/4, so this method returns the positive proper numerator, 3.
The proper numerator will always be relatively prime to the denominator, unless deprecated methods are used.
@Deprecated public int getProperWhole()
Gets the proper whole part of the fraction.
An improper fraction 7/4 can be resolved into a proper one, 1 3/4. This method returns the 1 from the proper fraction.
If the fraction is negative such as -7/4, it can be resolved into -1 3/4, so this method returns the positive whole part -1.
public int floor()
public Fraction quantize(int nDenom)
public int intValue()
Gets the fraction as an int. This returns the whole number
 part of the fraction.
intValue in class Numberpublic long longValue()
Gets the fraction as a long. This returns the whole number
 part of the fraction.
longValue in class Numberpublic float floatValue()
Gets the fraction as a float. This calculates the fraction
 as the numerator divided by denominator.
floatValue in class Numberfloatpublic double doubleValue()
Gets the fraction as a double. This calculates the fraction
 as the numerator divided by denominator.
doubleValue in class Numberdoublepublic Fraction reduce()
Reduce the fraction to the smallest values for the numerator and denominator, returning the result.
public Fraction invert()
Gets a fraction that is the inverse (1/fraction) of this one.
ArithmeticException - if the fraction represents zero.public Fraction negate()
Gets a fraction that is the negative (-fraction) of this one.
public Fraction abs()
Gets a fraction that is the positive equivalent of this one.
More precisely: (fraction >= 0 ? this : -fraction)
this if it is positive, or a new positive fraction
  instance with the opposite signed numeratorpublic Fraction pow(int power)
Gets a fraction that is raised to the passed in power.
power - the power to raise the fraction to
this if the power is one, ONE if the power
 is zero (even if the fraction equals ZERO) or a new fraction instance 
 raised to the appropriate power
ArithmeticException - if the resulting numerator or denominator exceeds
  Integer.MAX_VALUEpublic Fraction add(Fraction fraction)
Adds the value of this fraction to another. The algorithm follows Knuth, 4.5.1.
fraction - the fraction to add, must not be null
Fraction instance with the resulting values
IllegalArgumentException - if the fraction is null
ArithmeticException - if the resulting numerator or denominator exceeds
  Integer.MAX_VALUEpublic Fraction subtract(Fraction fraction)
Subtracts the value of another fraction from the value of this one.
fraction - the fraction to subtract, must not be null
Fraction instance with the resulting values
IllegalArgumentException - if the fraction is null
ArithmeticException - if the resulting numerator or denominator
   cannot be represented in an int.public Fraction multiplyBy(Fraction fraction)
Multiplies the value of this fraction by another.
fraction - the fraction to multiply by, must not be null
Fraction instance with the resulting values
IllegalArgumentException - if the fraction is null
ArithmeticException - if the resulting numerator or denominator exceeds
  Integer.MAX_VALUEpublic Fraction multiply(Fraction fraction)
Multiplies the value of this fraction by another.
fraction - the fraction to multiply by, must not be null
Fraction instance with the resulting values
IllegalArgumentException - if the fraction is null
ArithmeticException - if the resulting numerator or denominator exceeds
  Integer.MAX_VALUEpublic Fraction divideBy(Fraction fraction)
Divide the value of this fraction by another.
fraction - the fraction to divide by, must not be null
Fraction instance with the resulting values
IllegalArgumentException - if the fraction is null
ArithmeticException - if the fraction to divide by is zero
ArithmeticException - if the resulting numerator or denominator exceeds
  Integer.MAX_VALUEpublic Fraction divide(Fraction fraction)
Divide the value of this fraction by another.
fraction - the fraction to divide by, must not be null
Fraction instance with the resulting values
IllegalArgumentException - if the fraction is null
ArithmeticException - if the fraction to divide by is zero
ArithmeticException - if the resulting numerator or denominator exceeds
  Integer.MAX_VALUEpublic boolean equals(Object obj)
Compares this fraction to another object to test if they are equal.
.Note that 2/4 is equal to 1/2 (unless you are using deprecated methods).
equals in class Objectobj - the reference object with which to compare
true if this object is equalpublic int hashCode()
Gets a hashCode for the fraction.
hashCode in class Objectpublic int compareTo(Fraction other)
Compares this object to another based on size.
compareTo in interface Comparable<Fraction>other - the object to compare to
ClassCastException - if the object is not a Fraction
NullPointerException - if the object is nullpublic String toString()
Gets the fraction as a String.
The format used is 'numerator/denominator' always.
toString in class ObjectString form of the fractionpublic String toProperString()
Gets the fraction as a proper String in the format X Y/Z.
The format used in 'wholeNumber numerator/denominator'. If the whole number is zero it will be ommitted. If the numerator is zero, only the whole number is returned.
String form of the fractionpublic String repr()
  | 
sdr 0.7 | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||