Class Fraction

java.lang.Object
java.lang.Number
org.apache.commons.numbers.fraction.Fraction
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<Fraction>, Addition<Fraction>, Multiplication<Fraction>, NativeOperators<Fraction>

public class Fraction
extends java.lang.Number
implements java.lang.Comparable<Fraction>, java.io.Serializable, NativeOperators<Fraction>
Representation of a rational number.
See Also:
Serialized Form
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static Fraction FOUR_FIFTHS
    A fraction representing "4/5".
    static Fraction MINUS_ONE
    A fraction representing "-1 / 1".
    static Fraction ONE
    A fraction representing "1".
    static Fraction ONE_FIFTH
    A fraction representing "1/5".
    static Fraction ONE_HALF
    A fraction representing "1/2".
    static Fraction ONE_QUARTER
    A fraction representing "1/4".
    static Fraction ONE_THIRD
    A fraction representing "1/3".
    static Fraction THREE_FIFTHS
    A fraction representing "3/5".
    static Fraction THREE_QUARTERS
    A fraction representing "3/4".
    static Fraction TWO
    A fraction representing "2 / 1".
    static Fraction TWO_FIFTHS
    A fraction representing "2/5".
    static Fraction TWO_QUARTERS
    A fraction representing "2/4".
    static Fraction TWO_THIRDS
    A fraction representing "2/3".
    static Fraction ZERO
    A fraction representing "0".
  • Constructor Summary

    Constructors 
    Constructor Description
    Fraction​(double value)
    Create a fraction given the double value.
    Fraction​(double value, double epsilon, int maxIterations)
    Create a fraction given the double value and maximum error allowed.
    Fraction​(double value, int maxDenominator)
    Create a fraction given the double value and maximum denominator.
    Fraction​(int num)
    Create a fraction from an int.
    Fraction​(int num, int den)
    Create a fraction given the numerator and denominator.
  • Method Summary

    Modifier and Type Method Description
    Fraction abs()
    Returns the absolute value of this fraction.
    Fraction add​(int i)
    Add an integer to the fraction.
    Fraction add​(Fraction fraction)
    Adds the value of this fraction to another, returning the result in reduced form.
    int compareTo​(Fraction object)
    Compares this object to another based on size.
    Fraction divide​(int i)
    Divide the fraction by an integer.
    Fraction divide​(Fraction fraction)
    Divide the value of this fraction by another.
    double doubleValue()
    Gets the fraction as a double.
    boolean equals​(java.lang.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​(int i)
    Multiply the fraction by an integer.
    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 one()
    Identity element.
    double percentageValue()
    Gets the fraction percentage as a double.
    Fraction pow​(int n)
    Repeated multiplication.
    Fraction reciprocal()
    Return the multiplicative inverse of this fraction.
    Fraction subtract​(int i)
    Subtract an integer from the fraction.
    Fraction subtract​(Fraction fraction)
    Subtracts the value of another fraction from the value of this one, returning the result in reduced form.
    java.lang.String toString()
    Returns the String representing this fraction, ie "num / dem" or just "num" if the denominator is one.
    Fraction zero()
    Identity element.

    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 Details

  • Constructor Details

    • Fraction

      public Fraction​(double value)
      Create a fraction given the double value.
      Parameters:
      value - the double value to convert to a fraction.
      Throws:
      java.lang.IllegalArgumentException - if the continued fraction failed to converge.
    • Fraction

      public Fraction​(double value, double epsilon, int maxIterations)
      Create a fraction given the double value and maximum error allowed.

      References:

      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:
      java.lang.IllegalArgumentException - if the continued fraction failed to converge.
    • Fraction

      public Fraction​(double value, int maxDenominator)
      Create a fraction given the double value and maximum denominator.

      References:

      Parameters:
      value - the double value to convert to a fraction.
      maxDenominator - The maximum allowed value for denominator
      Throws:
      java.lang.IllegalArgumentException - if the continued fraction failed to converge
    • Fraction

      public Fraction​(int num)
      Create a fraction from an int. The fraction is num / 1.
      Parameters:
      num - the numerator.
    • 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:
      java.lang.ArithmeticException - if the denominator is zero or if integer overflow occurs
  • Method Details

    • abs

      public Fraction abs()
      Returns the absolute value of this fraction.
      Returns:
      the absolute value.
    • compareTo

      public int compareTo​(Fraction object)
      Compares this object to another based on size.
      Specified by:
      compareTo in interface java.lang.Comparable<Fraction>
      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.
      Specified by:
      doubleValue in class java.lang.Number
      Returns:
      the fraction as a double
    • equals

      public boolean equals​(java.lang.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.
      Overrides:
      equals in class java.lang.Object
      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.
      Specified by:
      floatValue in class java.lang.Number
      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.
      Overrides:
      hashCode in class java.lang.Object
      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.
      Specified by:
      intValue in class java.lang.Number
      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.
      Specified by:
      longValue in class java.lang.Number
      Returns:
      the whole number fraction part
    • zero

      public Fraction zero()
      Identity element.
      Specified by:
      zero in interface Addition<Fraction>
      Returns:
      the field element such that for all a, zero().add(a).equals(a) is true.
    • one

      public Fraction one()
      Identity element.
      Specified by:
      one in interface Multiplication<Fraction>
      Returns:
      the field element such that for all a, one().multiply(a).equals(a) is true.
    • negate

      public Fraction negate()
      Return the additive inverse of this fraction.
      Specified by:
      negate in interface Addition<Fraction>
      Returns:
      the negation of this fraction.
    • reciprocal

      public Fraction reciprocal()
      Return the multiplicative inverse of this fraction.
      Specified by:
      reciprocal in interface Multiplication<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.

      Specified by:
      add in interface Addition<Fraction>
      Parameters:
      fraction - the fraction to add, must not be null
      Returns:
      a Fraction instance with the resulting values
      Throws:
      java.lang.NullPointerException - if the fraction is null
      java.lang.ArithmeticException - if the resulting numerator or denominator exceeds Integer.MAX_VALUE
    • add

      public Fraction add​(int i)
      Add an integer to the fraction.
      Parameters:
      i - the integer to add.
      Returns:
      this + i
    • subtract

      public Fraction subtract​(Fraction fraction)

      Subtracts the value of another fraction from the value of this one, returning the result in reduced form.

      Specified by:
      subtract in interface NativeOperators<Fraction>
      Parameters:
      fraction - the fraction to subtract, must not be null
      Returns:
      a Fraction instance with the resulting values
      Throws:
      java.lang.NullPointerException - if the fraction is null
      java.lang.ArithmeticException - if the resulting numerator or denominator cannot be represented in an int.
    • subtract

      public Fraction subtract​(int i)
      Subtract an integer from the fraction.
      Parameters:
      i - the integer to subtract.
      Returns:
      this - i
    • multiply

      public Fraction multiply​(Fraction fraction)

      Multiplies the value of this fraction by another, returning the result in reduced form.

      Specified by:
      multiply in interface Multiplication<Fraction>
      Parameters:
      fraction - the fraction to multiply by, must not be null
      Returns:
      a Fraction instance with the resulting values
      Throws:
      java.lang.NullPointerException - if the fraction is null
      java.lang.ArithmeticException - if the resulting numerator or denominator exceeds Integer.MAX_VALUE
    • multiply

      public Fraction multiply​(int i)
      Multiply the fraction by an integer.
      Specified by:
      multiply in interface NativeOperators<Fraction>
      Parameters:
      i - the integer to multiply by.
      Returns:
      this * i
    • pow

      public Fraction pow​(int n)
      Description copied from interface: NativeOperators
      Repeated multiplication.
      Specified by:
      pow in interface NativeOperators<Fraction>
      Parameters:
      n - Power.
      Returns:
      this^n
    • divide

      public Fraction divide​(Fraction fraction)

      Divide the value of this fraction by another.

      Specified by:
      divide in interface NativeOperators<Fraction>
      Parameters:
      fraction - the fraction to divide by, must not be null
      Returns:
      a Fraction instance with the resulting values
      Throws:
      java.lang.ArithmeticException - if the fraction to divide by is zero or if the resulting numerator or denominator exceeds Integer.MAX_VALUE
    • divide

      public Fraction divide​(int i)
      Divide the fraction by an integer.
      Parameters:
      i - the integer to divide by.
      Returns:
      this * i
    • percentageValue

      public double percentageValue()

      Gets the fraction percentage as a double. This calculates the fraction as the numerator divided by denominator multiplied by 100.

      Returns:
      the fraction percentage as a double.
    • 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:
      java.lang.ArithmeticException - if the denominator is zero
    • toString

      public java.lang.String toString()

      Returns the String representing this fraction, ie "num / dem" or just "num" if the denominator is one.

      Overrides:
      toString in class java.lang.Object
      Returns:
      a string representation of the fraction.
      See Also:
      Object.toString()