Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.apache.commons.math.fraction
Class Fraction

    java.lang.Object
      java.lang.Number
          org.apache.commons.math.fraction.Fraction

All Implemented Interfaces:
  Comparable, Serializable

public class Fraction
extends Number
implements Comparable

Representation of a rational number.

Since:
  1.1
Version:
  $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
See Also:
  Serialized Form

Field Summary
`static Fraction` | `ONE` A fraction representing "1 / 1".
`static Fraction` | `ZERO` A fraction representing "0 / 1".

Constructor Summary
`Fraction(double value)` Create a fraction given the double value.
`Fraction(double value, double epsilon, int maxIterations)` Create a fraction given the double value.
`Fraction(int num, int den)` Create a fraction given the numerator and denominator.

Method Summary
`Fraction` | `abs()` Returns the absolute value of this fraction.
`Fraction` | `add(Fraction fraction)` Adds the value of this fraction to another, returning the result in reduced form.
`int` | `compareTo(Object object)` Compares this object to another based on size.
`Fraction` | `divide(Fraction fraction)` Divide the value of this fraction by another.
`double` | `doubleValue()` Gets the fraction as a `double`.
`boolean` | `equals(Object other)` Test for the equality of two fractions.
`float` | `floatValue()` Gets the fraction as a `float`.
`int` | `getDenominator()` Access the denominator.
`int` | `getNumerator()` Access the numerator.
`static Fraction` | `getReducedFraction(int numerator, int denominator)` Creates a `Fraction` instance with the 2 parts of a fraction Y/Z.
`int` | `hashCode()` Gets a hashCode for the fraction.
`int` | `intValue()` Gets the fraction as an `int`.
`long` | `longValue()` Gets the fraction as a `long`.
`Fraction` | `multiply(Fraction fraction)` Multiplies the value of this fraction by another, returning the result in reduced form.
`Fraction` | `negate()` Return the additive inverse of this fraction.
`Fraction` | `reciprocal()` Return the multiplicative inverse of this fraction.
`Fraction` | `subtract(Fraction fraction)` Subtracts the value of another fraction from the value of this one, returning the result in reduced form.

Methods inherited from class java.lang.Number
`byteValue, shortValue`

Methods inherited from class java.lang.Object
`clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### ONE

    public static final Fraction ONE

  A fraction representing "1 / 1".

### ZERO

    public static final Fraction ZERO

  A fraction representing "0 / 1".

Constructor Detail

### Fraction

    public Fraction(double value)
             throws ConvergenceException

  Create a fraction given the double value.

Parameters:
  `value` - the double value to convert to a fraction.
Throws:
  `ConvergenceException` - if the continued fraction failed to converge.

### Fraction

    public Fraction(double value,
                    double epsilon,
                    int maxIterations)
             throws ConvergenceException

  Create a fraction given the double value.

  References:

  - Continued Fraction equations (11) and (22)-(26)

Parameters:
  `value` - the double value to convert to a fraction.
  `epsilon` - maximum error allowed. The resulting fraction is within `epsilon` of `value`, in absolute terms.
  `maxIterations` - maximum number of convergents
Throws:
  `ConvergenceException` - if the continued fraction failed to converge.

### Fraction

    public Fraction(int num,
                    int den)

  Create a fraction given the numerator and denominator. The fraction is reduced to lowest terms.

Parameters:
  `num` - the numerator.
  `den` - the denominator.
Throws:
  `ArithmeticException` - if the denomiator is `zero`

Method Detail

### abs

    public Fraction abs()

  Returns the absolute value of this fraction.

  Returns:
    the absolute value.

### compareTo

    public int compareTo(Object object)

  Compares this object to another based on size.

  Specified by:
    `compareTo` in interface `Comparable`
  Parameters:
    `object` - the object to compare to
  Returns:
    -1 if this is less than `object`, +1 if this is greater than `object`, 0 if they are equal.

### doubleValue

    public double doubleValue()

  Gets the fraction as a `double`. This calculates the fraction as the numerator divided by denominator.

  Returns:
    the fraction as a `double`

### equals

    public boolean equals(Object other)

  Test for the equality of two fractions. If the lowest term numerator and denominators are the same for both fractions, the two fractions are considered to be equal.

  Parameters:
    `other` - fraction to test for equality to this fraction
  Returns:
    true if two fractions are equal, false if object is `null`, not an instance of `Fraction`, or not equal to this fraction instance.

### floatValue

    public float floatValue()

  Gets the fraction as a `float`. This calculates the fraction as the numerator divided by denominator.

  Returns:
    the fraction as a `float`

### getDenominator

    public int getDenominator()

  Access the denominator.

  Returns:
    the denominator.

### getNumerator

    public int getNumerator()

  Access the numerator.

  Returns:
    the numerator.

### hashCode

    public int hashCode()

  Gets a hashCode for the fraction.

  Returns:
    a hash code value for this object

### intValue

    public int intValue()

  Gets the fraction as an `int`. This returns the whole number part of the fraction.

  Returns:
    the whole number fraction part

### longValue

    public long longValue()

  Gets the fraction as a `long`. This returns the whole number part of the fraction.

  Returns:
    the whole number fraction part

### negate

    public Fraction negate()

  Return the additive inverse of this fraction.

  Returns:
    the negation of this fraction.

### reciprocal

    public Fraction reciprocal()

  Return the multiplicative inverse of this fraction.

  Returns:
    the reciprocal fraction

### add

    public Fraction add(Fraction fraction)

  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`
  Returns:
    a `Fraction` instance with the resulting values
  Throws:
    `IllegalArgumentException` - if the fraction is `null`
    `ArithmeticException` - if the resulting numerator or denominator exceeds `Integer.MAX_VALUE`

### subtract

    public Fraction subtract(Fraction fraction)

  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`
  Returns:
    a `Fraction` instance with the resulting values
  Throws:
    `IllegalArgumentException` - if the fraction is `null`
    `ArithmeticException` - if the resulting numerator or denominator cannot be represented in an `int`.

### multiply

    public Fraction multiply(Fraction fraction)

  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`
  Returns:
    a `Fraction` instance with the resulting values
  Throws:
    `IllegalArgumentException` - if the fraction is `null`
    `ArithmeticException` - if the resulting numerator or denominator exceeds `Integer.MAX_VALUE`

### divide

    public Fraction divide(Fraction fraction)

  Divide the value of this fraction by another.

  Parameters:
    `fraction` - the fraction to divide by, must not be `null`
  Returns:
    a `Fraction` instance with the resulting values
  Throws:
    `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_VALUE`

### getReducedFraction

    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.

  Parameters:
    `numerator` - the numerator, for example the three in 'three sevenths'
    `denominator` - the denominator, for example the seven in 'three sevenths'
  Returns:
    a new fraction instance, with the numerator and denominator reduced
  Throws:
    `ArithmeticException` - if the denominator is `zero`

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2003-2005 The Apache Software Foundation. All Rights Reserved.