Class Rational

java.lang.Object
java.lang.Number
org.sosy_lab.common.rationals.Rational
All Implemented Interfaces:
Serializable, Comparable<Rational>

@Immutable public final class Rational extends Number implements Comparable<Rational>
Rational class, throws IllegalArgumentException on unsupported operations (e.g. 1/0).

The Rational object is immutable. All arithmetic operations return new instances.

For performance and convenience, there is always only a single Rational instance representing numbers 0, 1 and -1. These numbers can be compared using == operator.

See Also:
  • Field Details

    • ZERO

      public static final Rational ZERO
    • ONE

      public static final Rational ONE
    • NEG_ONE

      public static final Rational NEG_ONE
  • Method Details

    • of

      public static Rational of(BigInteger numerator, BigInteger denominator)
      Create a new rational from a numerator and a denominator.
    • ofLongs

      public static Rational ofLongs(long numerator, long denominator)
      Create a new rational from two longs.
    • ofLong

      public static Rational ofLong(long numerator)
      Create a new rational equal to the given long.
    • ofBigInteger

      public static Rational ofBigInteger(BigInteger numerator)
      Create a new rational equal to the given BigInteger.
    • ofString

      public static Rational ofString(String s)
      Reverses the effect of toString(). Supports 2 different formats: with slash (e.g. 25/17) or without slash (e.g. 5)
      Parameters:
      s - Input string
      Throws:
      NumberFormatException - iff s is not a valid representation of Rational.
      IllegalArgumentException - If the resulting rational is undefined (e.g. 0/0 or 1/0).
    • of

      public static Rational of(String s)
      Syntax sugar helper for creating Rationals.
      See Also:
    • of

      public static Rational of(long l)
      Syntax sugar.
      See Also:
    • ofBigDecimal

      public static Rational ofBigDecimal(BigDecimal decimal)
      Convert a given BigDecimal to Rational.
    • times

      public Rational times(Rational b)
      Multiply by b, return a new instance.
    • plus

      public Rational plus(Rational b)
      Return a new instance equal to the sum of this and b.
    • minus

      public Rational minus(Rational b)
      Return a new instance equal to this - b.
    • divides

      public Rational divides(Rational b)
      Return this / b.
    • reciprocal

      public Rational reciprocal()
      Return reciprocal of this.
      Throws:
      IllegalArgumentException - If invoked on zero.
    • negate

      public Rational negate()
      Return negation of this.
    • doubleValue

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

      public boolean isIntegral()
    • getNum

      public BigInteger getNum()
    • getDen

      public BigInteger getDen()
    • signum

      public int signum()
      Returns -1, 0 or 1, representing the sign of the rational number.
    • abs

      public Rational abs()
      Returns absolute value of this.
    • toString

      public String toString()
      Returns string of the form num/den.
      Overrides:
      toString in class Object
    • compareTo

      public int compareTo(Rational b)
      Specified by:
      compareTo in interface Comparable<Rational>
    • equals

      public boolean equals(@Nullable Object y)
      Overrides:
      equals in class Object
    • hashCode

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

      public static Rational max(Rational a, Rational b)
    • 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