Class 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:
    Serialized Form
    • Method Detail

      • 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).
      • 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.
      • negate

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

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

        public boolean isIntegral()
      • 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
      • equals

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

        public int hashCode()
        Overrides:
        hashCode in class Object
      • 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