Class Rational

java.lang.Object
java.lang.Number
com.github.kokorin.jaffree.Rational
All Implemented Interfaces:
Serializable, Comparable<Rational>

public final class Rational extends Number implements Comparable<Rational>
Represents rational numbers.

Double and Float can't represent every rational number without precision loss, which is crucial for correct media representation.

See Also:
  • Field Details

    • ZERO

      public static final Rational ZERO
    • ONE

      public static final Rational ONE
  • Constructor Details

    • Rational

      public Rational(long numerator, long denominator)
      Create Rational.
      Parameters:
      numerator - numerator
      denominator - denominator
  • Method Details

    • getNumerator

      public long getNumerator()
      Returns this Rational numerator.
      Returns:
      numerator
    • getDenominator

      public long getDenominator()
      Returns this Rational denominator.
      Returns:
      denominator
    • compareTo

      public int compareTo(Rational that)
      Specified by:
      compareTo in interface Comparable<Rational>
    • intValue

      public int intValue()
      Specified by:
      intValue in class Number
    • longValue

      public long longValue()
      Specified by:
      longValue in class Number
    • floatValue

      public float floatValue()
      Specified by:
      floatValue in class Number
    • doubleValue

      public double doubleValue()
      Specified by:
      doubleValue in class Number
    • negate

      public Rational negate()
      Returns a Rational whose value is (-this).
      Returns:
      -this
    • add

      public Rational add(Number value)
      Returns a Rational whose value is (this + value).
      Parameters:
      value - value to be added to this Rational.
      Returns:
      this + val
    • subtract

      public Rational subtract(Number value)
      Returns a Rational whose value is (this - value).
      Parameters:
      value - value to be subtracted from this Rational.
      Returns:
      this - val
    • multiply

      public Rational multiply(Number value)
      Returns a Rational whose value is (this * value).
      Parameters:
      value - value to be multiplied by this Rational.
      Returns:
      this * value
    • divide

      public Rational divide(Number value)
      Returns a Rational whose value is (this / value).
      Parameters:
      value - divisor
      Returns:
      this / value
    • inverse

      public Rational inverse()
      Returns a Rational whose value is (1 / this).
      Returns:
      1 / this
    • lessThan

      public boolean lessThan(Number that)
      Returns true if (this < value).
      Parameters:
      that - value to be compared with this Rational.
      Returns:
      this < value
    • lessThanOrEqual

      public boolean lessThanOrEqual(Number that)
      Returns true if (this <= value).
      Parameters:
      that - value to be compared with this Rational.
      Returns:
      this <= value
    • greaterThan

      public boolean greaterThan(Number that)
      Returns true if (this > value).
      Parameters:
      that - value to be compared with this Rational.
      Returns:
      this > value
    • greaterThanOrEqual

      public boolean greaterThanOrEqual(Number that)
      Returns true if (this >= value).
      Parameters:
      that - value to be compared with this Rational.
      Returns:
      this >= value
    • simplify

      public Rational simplify()
      Simplifies this Rational by dividing numerator and denominator by greatest common divisor.

      E.g. 2/6 is simplified to 1/3

      Returns:
      simplified Rational
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toString

      public String toString(String delimiter)
      Converts Rational to String using specified delimiter.
      Parameters:
      delimiter - delimiter
      Returns:
      this converted to String
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • valueOf

      public static Rational valueOf(long value)
      Returns a Rational whose value is equal to that of the specified long.
      Parameters:
      value - value of the Rational to return.
      Returns:
      a Rational with the specified value.
    • valueOf

      public static Rational valueOf(double value)
      Returns a Rational whose value is equal to that of the specified double.
      Parameters:
      value - value of the Rational to return.
      Returns:
      a Rational with the specified value.
    • valueOf

      public static Rational valueOf(Number value)
      Returns a Rational whose value is equal to that of the specified Number.
      Parameters:
      value - value of the Rational to return.
      Returns:
      a Rational with the specified value.
    • valueOf

      public static Rational valueOf(String value) throws NumberFormatException
      Parses Rational.

      Numerator and denominator are expected to be separated by / (slash) symbol.

      Parameters:
      value - value to parse
      Returns:
      Rational value
      Throws:
      NumberFormatException - if wrong format
    • valueOf

      public static Rational valueOf(String value, String delimiter) throws NumberFormatException
      Parses Rational.

      Numerator and denominator are expected to be separated by delimiter.

      Parameters:
      value - value to parse
      delimiter - delimiter which separates numerator and denominator
      Returns:
      Rational value
      Throws:
      NumberFormatException - if wrong format