Class Fraction

    • Field Summary

      Fields 
      Modifier and Type Field Description
      int denominator
      The b term in the a/b fraction.
      int numerator
      The a term in the a/b fraction.
    • Constructor Summary

      Constructors 
      Constructor Description
      Fraction​(int numerator, int denominator)
      Creates a new fraction.
      Fraction​(String s)
      Creates a new fraction from the given text.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Fraction add​(Fraction other)
      Returns the simplified result of adding the given fraction to this fraction.
      byte byteValue()
      Returns this fraction rounded toward zero, if the result can be represented as a signed byte.
      int ceil()
      Returns this fraction rounded toward positive infinity.
      int compareTo​(Fraction other)
      Compares this fraction with the given one for order.
      Fraction divide​(Fraction other)
      Returns the simplified result of dividing this fraction by the given fraction.
      double doubleValue()
      Returns the fraction as a double-precision floating point number.
      boolean equals​(Object other)
      Compares this fraction with the given object for equality.
      float floatValue()
      Returns the fraction as a single-precision floating point number.
      int floor()
      Returns this fraction rounded toward negative infinity.
      int hashCode()
      Returns a hash code value for this fraction.
      int intValue()
      Returns this fraction rounded toward zero.
      long longValue()
      Returns this fraction rounded toward zero.
      Fraction multiply​(Fraction other)
      Returns the simplified result of multiplying the given fraction with this fraction.
      Fraction negate()
      Returns the negative value of this fraction.
      int round()
      Returns this fraction rounded toward nearest integer.
      short shortValue()
      Returns this fraction rounded toward zero, if the result can be represented as a short integer.
      int signum()
      Returns the sign of this fraction.
      Fraction simplify()
      Returns a fraction equivalent to this but represented by the smallest possible numerator and denominator values.
      Fraction subtract​(Fraction other)
      Returns the simplified result of subtracting the given fraction from this fraction.
      String toString()
      Returns a string representation of this fraction.
      Fraction unique()
      Returns a unique fraction instance equals to this.
      static Fraction valueOf​(double value)
      Converts the given IEEE 754 double-precision value to a fraction.
    • Field Detail

      • numerator

        public final int numerator
        The a term in the a/b fraction. Can be positive, negative or zero.
        See Also:
        double­Value()
      • denominator

        public final int denominator
        The b term in the a/b fraction. Can be positive, negative or zero. If zero, then the fraction floating point value will be positive infinity, negative infinity or NaN depending on the numerator value.
        See Also:
        double­Value()
    • Constructor Detail

      • Fraction

        public Fraction​(int numerator,
                        int denominator)
        Creates a new fraction. This constructor stores the fraction exactly as specified; it does not simplify it. The fraction can be simplified after construction by a call to simplify().
        Parameters:
        numerator - the a term in the a/b fraction.
        denominator - the b term in the a/b fraction.
      • Fraction

        public Fraction​(String s)
                 throws NumberFormatException
        Creates a new fraction from the given text. This constructor is the converse of to­String() method. It can parse single numbers like "3", fractions like "2/3", Unicode characters like "⅔" and infinity symbols "∞" and "−∞". The given text shall not contain spaces.
        Parameters:
        s - the text to parse.
        Throws:
        Number­Format­Exception - if the given text can not be parsed.
        Since:
        1.0
    • Method Detail

      • valueOf

        public static Fraction valueOf​(double value)
        Converts the given IEEE 754 double-precision value to a fraction. If successful, this method returns a fraction such as double­Value() is equal to the given value in the sense of Double​.equals(Object): infinities, positive and negative zeros are preserved, but various NaN values are collapsed to a single NaN value.
        Design note: this method does not return approximated values because it is difficult to choose which fraction is best. For example choosing an approximated fraction for π value is quite arbitrary, and searching the fraction closer than any other fraction representable by this class is computationally expansive. Even with common fractions, the algorithm currently implemented in this class can detect that 1.6666666666666667 is equal to 5⁄3 but can not detect easily that 1.66666666666666 (same number with two decimal digits dropped) is close to 5⁄3.
        This method accepts only values between -2147483648 and 2147483647 inclusive, i.e. values in the range of 32-bits integers. If the given value has fraction digits, then the validity range will be smaller depending on the denominator required for representing that value.
        Parameters:
        value - the double-precision value to convert to a fraction.
        Returns:
        a fraction such as double­Value() is equals to the given value.
        Throws:
        Illegal­Argument­Exception - if the given value can not be converted to a fraction.
        Since:
        1.0
      • unique

        public Fraction unique()
        Returns a unique fraction instance equals to this. If this method has been invoked previously on another Fraction with the same value than this, then that previous instance is returned (provided that it has not yet been garbage collected). Otherwise this method adds this fraction to the pool of fractions that may be returned in next unique() invocations, then returns this.

        This method is useful for saving memory when a potentially large amount of Fraction instances will be kept for a long time and many instances are likely to have the same values. It is usually not worth to invoke this method for short-lived instances.

        Returns:
        a unique instance of a fraction equals to this.
      • simplify

        public Fraction simplify()
        Returns a fraction equivalent to this but represented by the smallest possible numerator and denominator values. If this fraction can not be simplified, then this method returns this.
        Returns:
        the simplest fraction equivalent to this fraction.
      • negate

        public Fraction negate()
        Returns the negative value of this fraction. This method does not simplify the fraction.
        Returns:
        the result of -this.
        Throws:
        Arithmetic­Exception - if the result overflows.
      • add

        public Fraction add​(Fraction other)
        Returns the simplified result of adding the given fraction to this fraction.
        Parameters:
        other - the fraction to add to this fraction.
        Returns:
        the simplified result of this + other.
        Throws:
        Arithmetic­Exception - if the result overflows.
      • subtract

        public Fraction subtract​(Fraction other)
        Returns the simplified result of subtracting the given fraction from this fraction.
        Parameters:
        other - the fraction to subtract from this fraction.
        Returns:
        the simplified result of this - other.
        Throws:
        Arithmetic­Exception - if the result overflows.
      • multiply

        public Fraction multiply​(Fraction other)
        Returns the simplified result of multiplying the given fraction with this fraction.
        Parameters:
        other - the fraction to multiply with this fraction.
        Returns:
        the simplified result of this × other.
        Throws:
        Arithmetic­Exception - if the result overflows.
      • divide

        public Fraction divide​(Fraction other)
        Returns the simplified result of dividing this fraction by the given fraction.
        Parameters:
        other - the fraction by which to divide this fraction.
        Returns:
        the simplified result of thisother.
        Throws:
        Arithmetic­Exception - if the result overflows.
      • round

        public int round()
        Returns this fraction rounded toward nearest integer. If the result is located at equal distance from the two nearest integers, then rounds to the even one.
        Returns:
        numerator / denominator rounded toward nearest integer.
      • floor

        public int floor()
        Returns this fraction rounded toward negative infinity. This is different from the default operation on primitive types, which rounds toward zero.

        Tip: if the numerator and the denominator are both positive or both negative, then the result is positive and identical to numerator / denominator.

        Returns:
        numerator / denominator rounded toward negative infinity.
      • ceil

        public int ceil()
        Returns this fraction rounded toward positive infinity. This is different from the default operation on primitive types, which rounds toward zero.
        Returns:
        numerator / denominator rounded toward positive infinity.
      • longValue

        public long longValue()
        Returns this fraction rounded toward zero.
        Specified by:
        long­Value in class Number
        Returns:
        this fraction rounded toward zero.
      • shortValue

        public short shortValue()
        Returns this fraction rounded toward zero, if the result can be represented as a short integer.
        Overrides:
        short­Value in class Number
        Returns:
        this fraction rounded toward zero.
        Throws:
        Arithmetic­Exception - if the result can not be represented as a short integer.
      • byteValue

        public byte byteValue()
        Returns this fraction rounded toward zero, if the result can be represented as a signed byte.
        Overrides:
        byte­Value in class Number
        Returns:
        this fraction rounded toward zero.
        Throws:
        Arithmetic­Exception - if the result can not be represented as a signed byte.
      • signum

        public int signum()
        Returns the sign of this fraction. The return value is -1 if this fraction is negative; 0 if the numerator is zero; and 1 if this fraction is positive.
        Returns:
        the sign of this fraction.
        Since:
        1.0
        See Also:
        Integer​.signum(int)
      • compareTo

        public int compareTo​(Fraction other)
        Compares this fraction with the given one for order.
        Specified by:
        compare­To in interface Comparable<Fraction>
        Parameters:
        other - the fraction to compare to this fraction for ordering.
        Returns:
        a negative number if this fraction is smaller than the given fraction, a positive number if greater, or 0 if equals.
      • equals

        public boolean equals​(Object other)
        Compares this fraction with the given object for equality. This method returns true only if the two objects are fractions with same numerator and denominator values. Fractions with different values are not considered equal even if the two fraction are equivalent.
        Overrides:
        equals in class Object
        Parameters:
        other - the object to compare with this fraction for equality.
        Returns:
        true if the given object is an other fraction with the same numerator and denominator values.
      • hashCode

        public int hashCode()
        Returns a hash code value for this fraction.
        Overrides:
        hash­Code in class Object
      • toString

        public String toString()
        Returns a string representation of this fraction. This method returns Unicode symbol if possible.
        Overrides:
        to­String in class Object
        Returns:
        a string representation of this fraction.