Class BigFraction

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

Representation of a rational number using arbitrary precision.

The number is expressed as the quotient p/q of two BigIntegers, a numerator p and a non-zero denominator q.

This class is immutable. Rational number

See Also:
  • Field Details

    • ZERO

      public static final BigFraction ZERO
      A fraction representing "0".
    • ONE

      public static final BigFraction ONE
      A fraction representing "1".
  • Method Details

    • from

      public static BigFraction from(double value)
      Create a fraction given the double value.

      This factory method behaves differently to the method from(double, double, int). It converts the double value exactly, considering its internal bits representation. This works for all values except NaN and infinities and does not requires any loop or convergence threshold.

      Since this conversion is exact and since double numbers are sometimes approximated, the fraction created may seem strange in some cases. For example, calling from(1.0 / 3.0) does not create the fraction \( \frac{1}{3} \), but the fraction \( \frac{6004799503160661}{18014398509481984} \) because the double number passed to the method is not exactly \( \frac{1}{3} \) (which cannot be represented exactly in IEEE754).

      Parameters:
      value - Value to convert to a fraction.
      Returns:
      a new instance.
      Throws:
      IllegalArgumentException - if the given value is NaN or infinite.
      See Also:
    • from

      public static BigFraction from(double value, double epsilon, int maxIterations)
      Create a fraction given the double value and maximum error allowed.

      References:

      Parameters:
      value - 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.
      Returns:
      a new instance.
      Throws:
      IllegalArgumentException - if the given value is NaN or infinite; epsilon is not positive; or maxIterations < 1.
      ArithmeticException - if the continued fraction failed to converge.
    • from

      public static BigFraction from(double value, int maxDenominator)
      Create a fraction given the double value and maximum denominator.

      References:

      Note: The magnitude of the maxDenominator is used allowing use of Integer.MIN_VALUE for a supported maximum denominator of 231.

      Parameters:
      value - Value to convert to a fraction.
      maxDenominator - Maximum allowed value for denominator.
      Returns:
      a new instance.
      Throws:
      IllegalArgumentException - if the given value is NaN or infinite or maxDenominator is zero.
      ArithmeticException - if the continued fraction failed to converge.
    • of

      public static BigFraction of(int num)
      Create a fraction given the numerator. The denominator is 1.
      Parameters:
      num - the numerator.
      Returns:
      a new instance.
    • of

      public static BigFraction of(long num)
      Create a fraction given the numerator. The denominator is 1.
      Parameters:
      num - Numerator.
      Returns:
      a new instance.
    • of

      public static BigFraction of(BigInteger num)
      Create a fraction given the numerator. The denominator is 1.
      Parameters:
      num - Numerator.
      Returns:
      a new instance.
      Throws:
      NullPointerException - if numerator is null.
    • of

      public static BigFraction of(int num, int den)
      Create a fraction given the numerator and denominator. The fraction is reduced to lowest terms.
      Parameters:
      num - Numerator.
      den - Denominator.
      Returns:
      a new instance.
      Throws:
      ArithmeticException - if den is zero.
    • of

      public static BigFraction of(long num, long den)
      Create a fraction given the numerator and denominator. The fraction is reduced to lowest terms.
      Parameters:
      num - Numerator.
      den - Denominator.
      Returns:
      a new instance.
      Throws:
      ArithmeticException - if den is zero.
    • of

      public static BigFraction of(BigInteger num, BigInteger den)
      Create a fraction given the numerator and denominator. The fraction is reduced to lowest terms.
      Parameters:
      num - Numerator.
      den - Denominator.
      Returns:
      a new instance.
      Throws:
      NullPointerException - if numerator or denominator are null.
      ArithmeticException - if the denominator is zero.
    • parse

      public static BigFraction parse(String s)
      Returns a BigFraction instance representing the specified string s.

      If s is null, then a NullPointerException is thrown.

      The string must be in a format compatible with that produced by BigFraction.toString(). The format expects an integer optionally followed by a '/' character and and second integer. Leading and trailing spaces are allowed around each numeric part. Each numeric part is parsed using BigInteger(String). The parts are interpreted as the numerator and optional denominator of the fraction. If absent the denominator is assumed to be "1".

      Examples of valid strings and the equivalent BigFraction are shown below:

       "0"                 = BigFraction.of(0)
       "42"                = BigFraction.of(42)
       "0 / 1"             = BigFraction.of(0, 1)
       "1 / 3"             = BigFraction.of(1, 3)
       "-4 / 13"           = BigFraction.of(-4, 13)

      Note: The fraction is returned in reduced form and the numerator and denominator may not match the values in the input string. For this reason the result of BigFraction.parse(s).toString().equals(s) may not be true.

      Parameters:
      s - String representation.
      Returns:
      an instance.
      Throws:
      NullPointerException - if the string is null.
      NumberFormatException - if the string does not contain a parsable fraction.
      See Also:
    • zero

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

      public boolean isZero()
      Check if this is a neutral element of addition, i.e. this.add(a) returns a or an element representing the same value as a.

      The default implementation calls equals(zero()). Implementations may want to employ more a efficient method. This may even be required if an implementation has multiple representations of zero and its equals method differentiates between them.

      Specified by:
      isZero in interface Addition<BigFraction>
      Returns:
      true if this is a neutral element of addition.
      See Also:
    • one

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

      public boolean isOne()
      Check if this is a neutral element of multiplication, i.e. this.multiply(a) returns a or an element representing the same value as a.

      The default implementation calls equals(one()). Implementations may want to employ more a efficient method. This may even be required if an implementation has multiple representations of one and its equals method differentiates between them.

      Specified by:
      isOne in interface Multiplication<BigFraction>
      Returns:
      true if this is a neutral element of multiplication.
      See Also:
    • getNumerator

      Access the numerator as a BigInteger.
      Returns:
      the numerator as a BigInteger.
    • getNumeratorAsInt

      public int getNumeratorAsInt()
      Access the numerator as an int.
      Returns:
      the numerator as an int.
    • getNumeratorAsLong

      public long getNumeratorAsLong()
      Access the numerator as a long.
      Returns:
      the numerator as a long.
    • getDenominator

      Access the denominator as a BigInteger.
      Returns:
      the denominator as a BigInteger.
    • getDenominatorAsInt

      public int getDenominatorAsInt()
      Access the denominator as an int.
      Returns:
      the denominator as an int.
    • getDenominatorAsLong

      public long getDenominatorAsLong()
      Access the denominator as a long.
      Returns:
      the denominator as a long.
    • signum

      public int signum()
      Retrieves the sign of this fraction.
      Returns:
      -1 if the value is strictly negative, 1 if it is strictly positive, 0 if it is 0.
    • abs

      public BigFraction abs()
      Returns the absolute value of this fraction.
      Returns:
      the absolute value.
    • negate

      public BigFraction negate()
      Description copied from interface: Addition
      Additive inverse.
      Specified by:
      negate in interface Addition<BigFraction>
      Returns:
      -this.
    • reciprocal

      Multiplicative inverse.

      Raises an exception if the fraction is equal to zero.

      Specified by:
      reciprocal in interface Multiplication<BigFraction>
      Returns:
      this-1.
      Throws:
      ArithmeticException - if the current numerator is zero
    • doubleValue

      public double doubleValue()
      Returns the double value closest to this fraction.
      Specified by:
      doubleValue in class Number
      Returns:
      the fraction as a double.
    • floatValue

      public float floatValue()
      Returns the float value closest to this fraction.
      Specified by:
      floatValue in class Number
      Returns:
      the fraction as a double.
    • intValue

      public int intValue()
      Returns the whole number part of the fraction.
      Specified by:
      intValue in class Number
      Returns:
      the largest int value that is not larger than this fraction.
    • longValue

      public long longValue()
      Returns the whole number part of the fraction.
      Specified by:
      longValue in class Number
      Returns:
      the largest long value that is not larger than this fraction.
    • bigDecimalValue

      Returns the BigDecimal representation of this fraction. This calculates the fraction as numerator divided by denominator.
      Returns:
      the fraction as a BigDecimal.
      Throws:
      ArithmeticException - if the exact quotient does not have a terminating decimal expansion.
      See Also:
    • bigDecimalValue

      public BigDecimal bigDecimalValue(RoundingMode roundingMode)
      Returns the BigDecimal representation of this fraction. This calculates the fraction as numerator divided by denominator following the passed rounding mode.
      Parameters:
      roundingMode - Rounding mode to apply.
      Returns:
      the fraction as a BigDecimal.
      See Also:
    • bigDecimalValue

      public BigDecimal bigDecimalValue(int scale, RoundingMode roundingMode)
      Returns the BigDecimal representation of this fraction. This calculates the fraction as numerator divided by denominator following the passed scale and rounding mode.
      Parameters:
      scale - scale of the BigDecimal quotient to be returned. see BigDecimal for more information.
      roundingMode - Rounding mode to apply.
      Returns:
      the fraction as a BigDecimal.
      Throws:
      ArithmeticException - if roundingMode == RoundingMode.UNNECESSARY and the specified scale is insufficient to represent the result of the division exactly.
      See Also:
    • add

      public BigFraction add(int value)
      Adds the specified value to this fraction, returning the result in reduced form.
      Parameters:
      value - Value to add.
      Returns:
      this + value.
    • add

      public BigFraction add(long value)
      Adds the specified value to this fraction, returning the result in reduced form.
      Parameters:
      value - Value to add.
      Returns:
      this + value.
    • add

      public BigFraction add(BigInteger value)
      Adds the specified value to this fraction, returning the result in reduced form.
      Parameters:
      value - Value to add.
      Returns:
      this + value.
    • add

      public BigFraction add(BigFraction value)
      Adds the specified value to this fraction, returning the result in reduced form.
      Specified by:
      add in interface Addition<BigFraction>
      Parameters:
      value - Value to add.
      Returns:
      this + value.
    • subtract

      public BigFraction subtract(int value)
      Subtracts the specified value from this fraction, returning the result in reduced form.
      Parameters:
      value - Value to subtract.
      Returns:
      this - value.
    • subtract

      public BigFraction subtract(long value)
      Subtracts the specified value from this fraction, returning the result in reduced form.
      Parameters:
      value - Value to subtract.
      Returns:
      this - value.
    • subtract

      Subtracts the specified value from this fraction, returning the result in reduced form.
      Parameters:
      value - Value to subtract.
      Returns:
      this - value.
    • subtract

      Subtracts the specified value from this fraction, returning the result in reduced form.
      Specified by:
      subtract in interface NativeOperators<BigFraction>
      Parameters:
      value - Value to subtract.
      Returns:
      this - value.
    • multiply

      public BigFraction multiply(int value)
      Multiply this fraction by the passed value, returning the result in reduced form.
      Specified by:
      multiply in interface NativeOperators<BigFraction>
      Parameters:
      value - Value to multiply by.
      Returns:
      this * value.
    • multiply

      public BigFraction multiply(long value)
      Multiply this fraction by the passed value, returning the result in reduced form.
      Parameters:
      value - Value to multiply by.
      Returns:
      this * value.
    • multiply

      Multiply this fraction by the passed value, returning the result in reduced form.
      Parameters:
      value - Value to multiply by.
      Returns:
      this * value.
    • multiply

      Multiply this fraction by the passed value, returning the result in reduced form.
      Specified by:
      multiply in interface Multiplication<BigFraction>
      Parameters:
      value - Value to multiply by.
      Returns:
      this * value.
    • divide

      public BigFraction divide(int value)
      Divide this fraction by the passed value, returning the result in reduced form.
      Parameters:
      value - Value to divide by
      Returns:
      this / value.
      Throws:
      ArithmeticException - if the value to divide by is zero
    • divide

      public BigFraction divide(long value)
      Divide this fraction by the passed value, returning the result in reduced form.
      Parameters:
      value - Value to divide by
      Returns:
      this / value.
      Throws:
      ArithmeticException - if the value to divide by is zero
    • divide

      public BigFraction divide(BigInteger value)
      Divide this fraction by the passed value, returning the result in reduced form.
      Parameters:
      value - Value to divide by
      Returns:
      this / value.
      Throws:
      ArithmeticException - if the value to divide by is zero
    • divide

      public BigFraction divide(BigFraction value)
      Divide this fraction by the passed value, returning the result in reduced form.
      Specified by:
      divide in interface NativeOperators<BigFraction>
      Parameters:
      value - Value to divide by
      Returns:
      this / value.
      Throws:
      ArithmeticException - if the value to divide by is zero
    • pow

      public BigFraction pow(int exponent)
      Returns a BigFraction whose value is thisexponent, returning the result in reduced form.
      Specified by:
      pow in interface NativeOperators<BigFraction>
      Parameters:
      exponent - exponent to which this BigFraction is to be raised.
      Returns:
      thisexponent.
      Throws:
      ArithmeticException - if the intermediate result would overflow.
    • toString

      public String toString()
      Returns the String representing this fraction. Uses:
      • "0" if numerator is zero.
      • "numerator" if denominator is one.
      • "numerator / denominator" for all other cases.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the fraction.
    • compareTo

      public int compareTo(BigFraction other)
      Compares this object with the specified object for order using the signed magnitude.
      Specified by:
      compareTo in interface Comparable<BigFraction>
      Parameters:
      other -
      Returns:
    • equals

      public boolean equals(Object other)
      Test for equality with another object. If the other object is a Fraction then a comparison is made of the sign and magnitude; otherwise false is returned.
      Overrides:
      equals in class Object
      Parameters:
      other -
      Returns:
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object