Fraction is a Number implementation that stores fractions accurately.
This class is immutable, and interoperable with most methods that accept a Number.
Note that this class is intended for common use cases, it is int based and thus suffers from various overflow issues. For a BigInteger based equivalent, please see the Commons Math BigFraction class.
| Modifier and Type | Field and Description |
|---|---|
| private final int | denominator
The denominator number part of the fraction (the seven in three sevenths). |
| public static final Fraction | FOUR_FIFTHS
|
| private transient int | hashCode
Cached output hashCode (class is immutable). |
| private final int | numerator
The numerator number part of the fraction (the three in three sevenths). |
| public static final Fraction | |
| public static final Fraction | |
| public static final Fraction | |
| public static final Fraction | ONE_QUARTER
|
| public static final Fraction | |
| private static final long | serialVersionUID
Required for serialization support.
|
| public static final Fraction | THREE_FIFTHS
|
| public static final Fraction | THREE_QUARTERS
|
| private transient String | toProperString
Cached output toProperString (class is immutable). |
| private transient String | toString
Cached output toString (class is immutable). |
| public static final Fraction | TWO_FIFTHS
|
| public static final Fraction | TWO_QUARTERS
|
| public static final Fraction | TWO_THIRDS
|
| public static final Fraction |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public Fraction | Returns: this if it is positive, or a new positive fraction instance with the opposite signed numeratorGets a fraction that is the positive equivalent of this one. |
| public Fraction | |
| private static int | Returns: the sumx+yan addend x, final int an addend y)Adds two integers, checking for overflow. |
| private Fraction | Returns: aFraction instance with the resulting valuesthe fraction to subtract, must not be fraction, final boolean nulltrue to add, false to subtract isAdd)Implements add and subtract using the algorithm described in The Art of Computer Programming (TAOCP) 4.5.1 by Donald Knuth. |
| public int | Returns: -1 if this is less, 0 if equal, +1 if greaterthe object to compare to other)Implements java. Compares this object to another based on size. |
| public Fraction | |
| public double | Returns: the fraction as adoubleImplements abstract java. Gets the fraction as a |
| public boolean | Returns: true if this object is equalthe reference object with which to compare obj)Overrides java. Compares this fraction to another object to test if they are equal. |
| public float | Returns: the fraction as afloatImplements abstract java. Gets the fraction as a |
| public int | |
| public static Fraction | Returns: a new fraction instance that is close to the valuethe double value to convert value)Creates a |
| public static Fraction | Returns: a new fraction instancethe numerator, for example the three in 'three sevenths' numerator, int the denominator, for example the seven in 'three sevenths' denominator)Creates a |
| public static Fraction | Returns: a new fraction instancethe whole number, for example the one in 'one and three sevenths' whole, final int the numerator, for example the three in 'one and three sevenths' numerator, final int the denominator, for example the seven in 'one and three sevenths' denominator)Creates a |
| public static Fraction | Returns: the newFraction instancethe string to parse, must not be str)nullCreates a Fraction from a |
| public int | |
| public int | Returns: the numerator fraction part of a proper fraction, always positiveGets the proper numerator, always positive. |
| public int | Returns: the whole fraction part of a proper fraction, that includes the signGets the proper whole part of the fraction. |
| public static Fraction | Returns: a new fraction instance, with the numerator and denominator reducedthe numerator, for example the three in 'three sevenths' numerator, int the denominator, for example the seven in 'three sevenths' denominator)Creates a reduced |
| private static int | Returns: the greatest common divisor, never zeroa non-zero number u, int a non-zero number v)Gets the greatest common divisor of the absolute value of two numbers, using the "binary gcd" method which avoids division and modulo operations. |
| public int | Returns: a hash code value for this objectOverrides java. Gets a hashCode for the fraction. |
| public int | Returns: the whole number fraction partImplements abstract java. Gets the fraction as an |
| public Fraction | Returns: a new fraction instance with the numerator and denominator inverted.Gets a fraction that is the inverse (1/fraction) of this one. |
| public long | Returns: the whole number fraction partImplements abstract java. Gets the fraction as a |
| private static int | Returns: the productx*ya factor x, final int a factor y)Multiplies two integers, checking for overflow. |
| private static int | Returns: the productx*ya non-negative factor x, final int a non-negative factor y)Multiplies two non-negative integers, checking for overflow. |
| public Fraction | Returns: aFraction instance with the resulting valuesthe fraction to multiply by, must not be fraction)nullMultiplies the value of this fraction by another, returning the result in reduced form. |
| public Fraction | Returns: a new fraction instance with the opposite signed numeratorGets a fraction that is the negative (-fraction) of this one. |
| public Fraction | |
| public Fraction | Returns: a new reduced fraction instance, or this if no simplification possibleReduce the fraction to the smallest values for the numerator and denominator, returning the result. |
| private static int | Returns: the differencex-ythe minuend x, final int the subtrahend y)Subtracts two integers, checking for overflow. |
| public Fraction | |
| public String | Returns: aString form of the fractionGets the fraction as a proper |
| public String | Returns: aString form of the fractionOverrides java. Gets the fraction as a |
| denominator | back to summary |
|---|---|
| private final int denominator The denominator number part of the fraction (the seven in three sevenths). | |
| FOUR_FIFTHS | back to summary |
|---|---|
| public static final Fraction FOUR_FIFTHS
| |
| hashCode | back to summary |
|---|---|
| private transient int hashCode Cached output hashCode (class is immutable). | |
| numerator | back to summary |
|---|---|
| private final int numerator The numerator number part of the fraction (the three in three sevenths). | |
| ONE | back to summary |
|---|---|
| public static final Fraction ONE
| |
| ONE_FIFTH | back to summary |
|---|---|
| public static final Fraction ONE_FIFTH
| |
| ONE_HALF | back to summary |
|---|---|
| public static final Fraction ONE_HALF
| |
| ONE_QUARTER | back to summary |
|---|---|
| public static final Fraction ONE_QUARTER
| |
| ONE_THIRD | back to summary |
|---|---|
| public static final Fraction ONE_THIRD
| |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides java. Required for serialization support. Lang version 2.0.
| |
| THREE_FIFTHS | back to summary |
|---|---|
| public static final Fraction THREE_FIFTHS
| |
| THREE_QUARTERS | back to summary |
|---|---|
| public static final Fraction THREE_QUARTERS
| |
| toProperString | back to summary |
|---|---|
| private transient String toProperString Cached output toProperString (class is immutable). | |
| toString | back to summary |
|---|---|
| private transient String toString Cached output toString (class is immutable). | |
| TWO_FIFTHS | back to summary |
|---|---|
| public static final Fraction TWO_FIFTHS
| |
| TWO_QUARTERS | back to summary |
|---|---|
| public static final Fraction TWO_QUARTERS
| |
| TWO_THIRDS | back to summary |
|---|---|
| public static final Fraction TWO_THIRDS
| |
| ZERO | back to summary |
|---|---|
| public static final Fraction ZERO
| |
| Fraction | back to summary |
|---|---|
| private Fraction(final int numerator, final int denominator) Constructs a
| |
| abs | back to summary |
|---|---|
| public Fraction abs() Gets a fraction that is the positive equivalent of this one.
More precisely: The returned fraction is not reduced.
| |
| add | back to summary |
|---|---|
| public Fraction add(final Fraction fraction) Adds the value of this fraction to another, returning the result in reduced form. The algorithm follows Knuth, 4.5.1.
| |
| addAndCheck | back to summary |
|---|---|
| private static int addAndCheck(final int x, final int y) Adds two integers, checking for overflow.
| |
| addSub | back to summary |
|---|---|
| private Fraction addSub(final Fraction fraction, final boolean isAdd) Implements add and subtract using the algorithm described in The Art of Computer Programming (TAOCP) 4.5.1 by Donald Knuth.
| |
| compareTo | back to summary |
|---|---|
| public int compareTo(final Fraction other) Implements java. 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.
| |
| divideBy | back to summary |
|---|---|
| public Fraction divideBy(final Fraction fraction) Divide the value of this fraction by another.
| |
| doubleValue | back to summary |
|---|---|
| public double doubleValue() Implements abstract java. Gets the fraction as a
| |
| equals | back to summary |
|---|---|
| public boolean equals(final Object obj) Overrides java. 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. | |
| floatValue | back to summary |
|---|---|
| public float floatValue() Implements abstract java. Gets the fraction as a
| |
| getDenominator | back to summary |
|---|---|
| public int getDenominator() Gets the denominator part of the fraction.
| |
| getFraction | back to summary |
|---|---|
| public static Fraction getFraction(double value) Creates a This method uses the continued fraction algorithm, computing a maximum of 25 convergents and bounding the denominator by 10,000.
| |
| getFraction | back to summary |
|---|---|
| public static Fraction getFraction(int numerator, int denominator) Creates a Any negative signs are resolved to be on the numerator.
| |
| getFraction | back to summary |
|---|---|
| public static Fraction getFraction(final int whole, final int numerator, final int denominator) Creates a The negative sign must be passed in on the whole number part.
| |
| getFraction | back to summary |
|---|---|
| public static Fraction getFraction(String str) Creates a Fraction from a The formats accepted are:
and a .
| |
| getNumerator | back to summary |
|---|---|
| public 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.
| |
| getProperNumerator | back to summary |
|---|---|
| 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.
| |
| getProperWhole | back to summary |
|---|---|
| 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.
| |
| getReducedFraction | back to summary |
|---|---|
| public static Fraction getReducedFraction(int numerator, int denominator) Creates a reduced 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.
| |
| greatestCommonDivisor | back to summary |
|---|---|
| private static int greatestCommonDivisor(int u, int v) Gets the greatest common divisor of the absolute value of two numbers, using the "binary gcd" method which avoids division and modulo operations. See Knuth 4.5.2 algorithm B. This algorithm is due to Josef Stein (1961).
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Gets a hashCode for the fraction.
| |
| intValue | back to summary |
|---|---|
| public int intValue() Implements abstract java. Gets the fraction as an
| |
| invert | back to summary |
|---|---|
| public Fraction invert() Gets a fraction that is the inverse (1/fraction) of this one. The returned fraction is not reduced.
| |
| longValue | back to summary |
|---|---|
| public long longValue() Implements abstract java. Gets the fraction as a
| |
| mulAndCheck | back to summary |
|---|---|
| private static int mulAndCheck(final int x, final int y) Multiplies two integers, checking for overflow.
| |
| mulPosAndCheck | back to summary |
|---|---|
| private static int mulPosAndCheck(final int x, final int y) Multiplies two non-negative integers, checking for overflow.
| |
| multiplyBy | back to summary |
|---|---|
| public Fraction multiplyBy(final Fraction fraction) Multiplies the value of this fraction by another, returning the result in reduced form.
| |
| negate | back to summary |
|---|---|
| public Fraction negate() Gets a fraction that is the negative (-fraction) of this one. The returned fraction is not reduced.
| |
| pow | back to summary |
|---|---|
| public Fraction pow(final int power) Gets a fraction that is raised to the passed in power. The returned fraction is in reduced form.
| |
| reduce | back to summary |
|---|---|
| public Fraction reduce() 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.
| |
| subAndCheck | back to summary |
|---|---|
| private static int subAndCheck(final int x, final int y) Subtracts two integers, checking for overflow.
| |
| subtract | back to summary |
|---|---|
| public Fraction subtract(final Fraction fraction) Subtracts the value of another fraction from the value of this one, returning the result in reduced form.
| |
| toProperString | back to summary |
|---|---|
| public String toProperString() Gets the fraction as a proper The format used in 'wholeNumber numerator/denominator'. If the whole number is zero it will be omitted. If the numerator is zero, only the whole number is returned. | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Gets the fraction as a The format used is 'numerator/denominator' always. | |