Class Rational

java.lang.Object
java.lang.Number
org.monte.media.math.Rational
All Implemented Interfaces:
Serializable, Comparable<Rational>

public class Rational extends Number implements Comparable<Rational>
Represents a Rational number numerator/denominator.

A number is represented by two longs: the first represents the numerator of a fraction; the second, the denominator.

Invariants:

  • denominator &gt;=0, the denominator is always a positive integer
  • 0/1 is the unique* representation of 0.
  • {code 1/0},-1/0 are the unique representations of positive and negative infinity.
Author:
Werner Randelshofer
See Also:
  • Field Details

  • Constructor Details

    • Rational

      public Rational(long numerator)
    • Rational

      public Rational(long numerator, long denominator)
    • Rational

      public Rational(Rational r)
  • Method Details

    • getNumerator

      public long getNumerator()
    • getDenominator

      public long getDenominator()
    • add

      public Rational add(Rational that)
    • add

      public Rational add(long num, long den)
    • subtract

      public Rational subtract(Rational that)
    • negate

      public Rational negate()
    • inverse

      public Rational inverse()
    • floor

      public Rational floor(long d)
      Returns the closest rational with the specified denominator which is smaller or equal than this number.
    • floorNumerator

      public long floorNumerator(long d)
      Returns the closest numerator for the specified denominator which is smaller or equal than this number.
    • ceil

      public Rational ceil(long d)
      Returns the closest rational with the specified denominator which is greater or equal than this number.
    • multiply

      public Rational multiply(Rational that)
    • multiply

      public Rational multiply(long integer)
    • divide

      public Rational divide(Rational that)
    • toString

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

      public String toDescriptiveString()
    • 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
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • compareTo

      public int compareTo(Rational that)
      return { -1, 0, +1 } if a < b, a = b, or a > b.
      Specified by:
      compareTo in interface Comparable<Rational>
    • hashCode

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

      public static Rational max(Rational a, Rational b)
    • min

      public static Rational min(Rational a, Rational b)
    • isZero

      public boolean isZero()
    • isLessOrEqualZero

      public boolean isLessOrEqualZero()
    • valueOf

      public static Rational valueOf(double d)
    • valueOf

      public static Rational valueOf(long num, long den)
    • valueOf

      public static Rational valueOf(BigInteger num, BigInteger den)
    • round

      public Rational round(long d)
    • valueOf

      public static Rational valueOf(String str)
      Parses a string.

      A rational can be represented in the following ways:

      • As a long number
      • As a double number
      • As an integer/integer rational number
      Throws:
      NumberFormatException - if str can not be parsed.