| java.lang.Object java.lang.Number org.apache.commons.lang.math.Fraction
Fraction | final public class Fraction extends Number implements Comparable(Code) | | Fraction is a Number implementation that
stores fractions accurately.
This class is immutable, and interoperable with most methods that accept
a Number .
author: Travis Reeder author: Stephen Colebourne author: Tim O'Brien author: Pete Gieser author: C. Scott Ananian since: 2.0 version: $Id: Fraction.java 489733 2006-12-22 19:29:53Z bayard $ |
Method Summary | |
public Fraction | abs() | public Fraction | add(Fraction fraction) Adds the value of this fraction to another, returning the result in reduced form. | public int | compareTo(Object object) Compares this object to another based on size.
Note: this class has a natural ordering that is inconsistent
with equals, because, for example, equals treats 1/2 and 2/4 as
different, whereas compareTo treats them as equal. | public Fraction | divideBy(Fraction fraction) | public double | doubleValue() Gets the fraction as a double . | public boolean | equals(Object obj) Compares this fraction to another object to test if they are equal. .
To be equal, both values must be equal. | public float | floatValue() Gets the fraction as a float . | public int | getDenominator() | public static Fraction | getFraction(int numerator, int denominator) | public static Fraction | getFraction(int whole, int numerator, int denominator) | public static Fraction | getFraction(double value) | public static Fraction | getFraction(String str) | public int | getNumerator() | public int | getProperNumerator() Gets the proper numerator, always positive.
An improper fraction 7/4 can be resolved into a proper one, 1 3/4. | 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. | public static Fraction | getReducedFraction(int numerator, int denominator) | public int | hashCode() | public int | intValue() Gets the fraction as an int . | public Fraction | invert() | public long | longValue() Gets the fraction as a long . | public Fraction | multiplyBy(Fraction fraction) | public Fraction | negate() | public Fraction | pow(int power) | public Fraction | reduce() | public Fraction | subtract(Fraction fraction) | public 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. | public String | toString() Gets the fraction as a String .
The format used is 'numerator/denominator' always. |
FOUR_FIFTHS | final public static Fraction FOUR_FIFTHS(Code) | | Fraction representation of 4/5.
|
ONE | final public static Fraction ONE(Code) | | Fraction representation of 1.
|
ONE_FIFTH | final public static Fraction ONE_FIFTH(Code) | | Fraction representation of 1/5.
|
ONE_HALF | final public static Fraction ONE_HALF(Code) | | Fraction representation of 1/2.
|
ONE_QUARTER | final public static Fraction ONE_QUARTER(Code) | | Fraction representation of 1/4.
|
ONE_THIRD | final public static Fraction ONE_THIRD(Code) | | Fraction representation of 1/3.
|
THREE_FIFTHS | final public static Fraction THREE_FIFTHS(Code) | | Fraction representation of 3/5.
|
THREE_QUARTERS | final public static Fraction THREE_QUARTERS(Code) | | Fraction representation of 3/4.
|
TWO_FIFTHS | final public static Fraction TWO_FIFTHS(Code) | | Fraction representation of 2/5.
|
TWO_QUARTERS | final public static Fraction TWO_QUARTERS(Code) | | Fraction representation of 2/4.
|
TWO_THIRDS | final public static Fraction TWO_THIRDS(Code) | | Fraction representation of 2/3.
|
ZERO | final public static Fraction ZERO(Code) | | Fraction representation of 0.
|
abs | public Fraction abs()(Code) | | Gets a fraction that is the positive equivalent of this one.
More precisely: (fraction >= 0 ? this : -fraction)
The returned fraction is not reduced.
this if it is positive, or a new positive fractioninstance with the opposite signed numerator |
add | public Fraction add(Fraction fraction)(Code) | | Adds the value of this fraction to another, returning the result in reduced form.
The algorithm follows Knuth, 4.5.1.
Parameters: fraction - the fraction to add, must not be null a Fraction instance with the resulting values throws: IllegalArgumentException - if the fraction is null throws: ArithmeticException - if the resulting numerator or denominator exceedsInteger.MAX_VALUE |
compareTo | public int compareTo(Object object)(Code) | | Compares this object to another based on size.
Note: this class has a natural ordering that is inconsistent
with equals, because, for example, equals treats 1/2 and 2/4 as
different, whereas compareTo treats them as equal.
Parameters: object - the object to compare to -1 if this is less, 0 if equal, +1 if greater throws: ClassCastException - if the object is not a Fraction throws: NullPointerException - if the object is null |
divideBy | public Fraction divideBy(Fraction fraction)(Code) | | Divide the value of this fraction by another.
Parameters: fraction - the fraction to divide by, must not be null a Fraction instance with the resulting values throws: IllegalArgumentException - if the fraction is null throws: ArithmeticException - if the fraction to divide by is zero throws: ArithmeticException - if the resulting numerator or denominator exceedsInteger.MAX_VALUE |
doubleValue | public double doubleValue()(Code) | | Gets the fraction as a double . This calculates the fraction
as the numerator divided by denominator.
the fraction as a double |
equals | public boolean equals(Object obj)(Code) | | Compares this fraction to another object to test if they are equal. .
To be equal, both values must be equal. Thus 2/4 is not equal to 1/2.
Parameters: obj - the reference object with which to compare true if this object is equal |
floatValue | public float floatValue()(Code) | | Gets the fraction as a float . This calculates the fraction
as the numerator divided by denominator.
the fraction as a float |
getDenominator | public int getDenominator()(Code) | | Gets the denominator part of the fraction.
the denominator fraction part |
getFraction | public static Fraction getFraction(int numerator, int denominator)(Code) | | Creates a Fraction instance with the 2 parts
of a fraction Y/Z.
Any negative signs are resolved to be on the numerator.
Parameters: numerator - the numerator, for example the three in 'three sevenths' Parameters: denominator - the denominator, for example the seven in 'three sevenths' a new fraction instance throws: ArithmeticException - if the denomiator is zero |
getFraction | public static Fraction getFraction(int whole, int numerator, int denominator)(Code) | | 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.
Parameters: whole - the whole number, for example the one in 'one and three sevenths' Parameters: numerator - the numerator, for example the three in 'one and three sevenths' Parameters: denominator - the denominator, for example the seven in 'one and three sevenths' a new fraction instance throws: ArithmeticException - if the denomiator is zero throws: ArithmeticException - if the denominator is negative throws: ArithmeticException - if the numerator is negative throws: ArithmeticException - if the resulting numerator exceeds Integer.MAX_VALUE |
getFraction | public static Fraction getFraction(double value)(Code) | | 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.
Parameters: value - the double value to convert a new fraction instance that is close to the value throws: ArithmeticException - if |value| > Integer.MAX_VALUE or value = NaN throws: ArithmeticException - if the calculated denominator is zero throws: ArithmeticException - if the the algorithm does not converge |
getFraction | public static Fraction getFraction(String str)(Code) | | Creates a Fraction from a String .
The formats accepted are:
double String containing a dot
- 'X Y/Z'
- 'Y/Z'
- 'X' (a simple whole number)
and a .
Parameters: str - the string to parse, must not be null the new Fraction instance throws: IllegalArgumentException - if the string is null throws: NumberFormatException - if the number format is invalid |
getNumerator | public int getNumerator()(Code) | | 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 fraction part |
getProperNumerator | public int getProperNumerator()(Code) | | 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 numerator fraction part of a proper fraction, always positive |
getProperWhole | public int getProperWhole()(Code) | | 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.
the whole fraction part of a proper fraction, that includes the sign |
getReducedFraction | public static Fraction getReducedFraction(int numerator, int denominator)(Code) | | Creates a reduced Fraction instance with the 2 parts
of a fraction Y/Z.
For example, if the input parameters represent 2/4, then the created
fraction will be 1/2.
Any negative signs are resolved to be on the numerator.
Parameters: numerator - the numerator, for example the three in 'three sevenths' Parameters: denominator - the denominator, for example the seven in 'three sevenths' a new fraction instance, with the numerator and denominator reduced throws: ArithmeticException - if the denominator is zero |
hashCode | public int hashCode()(Code) | | Gets a hashCode for the fraction.
a hash code value for this object |
intValue | public int intValue()(Code) | | Gets the fraction as an int . This returns the whole number
part of the fraction.
the whole number fraction part |
invert | public Fraction invert()(Code) | | Gets a fraction that is the inverse (1/fraction) of this one.
The returned fraction is not reduced.
a new fraction instance with the numerator and denominatorinverted. throws: ArithmeticException - if the fraction represents zero. |
longValue | public long longValue()(Code) | | Gets the fraction as a long . This returns the whole number
part of the fraction.
the whole number fraction part |
multiplyBy | public Fraction multiplyBy(Fraction fraction)(Code) | | Multiplies the value of this fraction by another, returning the
result in reduced form.
Parameters: fraction - the fraction to multiply by, must not be null a Fraction instance with the resulting values throws: IllegalArgumentException - if the fraction is null throws: ArithmeticException - if the resulting numerator or denominator exceedsInteger.MAX_VALUE |
negate | public Fraction negate()(Code) | | Gets a fraction that is the negative (-fraction) of this one.
The returned fraction is not reduced.
a new fraction instance with the opposite signed numerator |
pow | public Fraction pow(int power)(Code) | | Gets a fraction that is raised to the passed in power.
The returned fraction is in reduced form.
Parameters: power - the power to raise the fraction to this if the power is one, ONE if the poweris zero (even if the fraction equals ZERO) or a new fraction instance raised to the appropriate power throws: ArithmeticException - if the resulting numerator or denominator exceedsInteger.MAX_VALUE |
reduce | public Fraction reduce()(Code) | | Reduce the fraction to the smallest values for the numerator and
denominator, returning the result.
For example, if this fraction represents 2/4, then the result
will be 1/2.
a new reduced fraction instance, or this if no simplification possible |
subtract | public Fraction subtract(Fraction fraction)(Code) | | Subtracts the value of another fraction from the value of this one,
returning the result in reduced form.
Parameters: fraction - the fraction to subtract, must not be null a Fraction instance with the resulting values throws: IllegalArgumentException - if the fraction is null throws: ArithmeticException - if the resulting numerator or denominatorcannot be represented in an int . |
toProperString | public String toProperString()(Code) | | 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.
a String form of the fraction |
toString | public String toString()(Code) | | Gets the fraction as a String .
The format used is 'numerator/denominator' always.
a String form of the fraction |
|
|