Class Rational

All Implemented Interfaces:
Serializable, Comparable<Rational>

public class Rational
extends Number
implements Comparable<Rational>
Immutable, arbitrary-precision rational numbers, expressed as fractions. A Rational consists of integral numerator and denominator parts, either or both of which may be negative. Two equal Rational values may be expressed as different fractions, but their canonical forms will always be identical.

The denominator of a Rational value cannot be zero. As such, this class cannot not represent infinite or NaN values, and will throw an ArithmeticException in cases where such a value would be the result.

All methods and constructors for this class throw NullPointerException when passed a null object reference for any input parameter.

See Also:
Wikipedia definition of Rational number
  • Constructor Summary

    Constructors 
    Constructor Description
    Rational​(long n, long d)
    Creates a Rational number given a numerator and denominator.
    Rational​(BigInteger n, BigInteger d)
    Creates a Rational number given a numerator and denominator.
  • Method Summary

    Modifier and Type Method Description
    Rational abs()
    Returns a Rational that is the absolute value of this Rational.
    Rational add​(double addend)
    Computes the value of this Rational plus the given number.
    Rational add​(long addend)
    Computes the value of this Rational plus the given number.
    Rational add​(Rational addend)
    Computes the value of this Rational plus the given other Rational.
    Rational canonical()
    Converts this Rational to its canonical form - an irreducible fraction with a positive denominator.
    int compareTo​(Rational other)
    Compares this Rational value to another for natural ordering.
    BigDecimal decimalValue()
    Returns the decimal expansion of this Rational value.
    Rational divide​(double divisor)
    Computes the value of this Rational divided by the given number.
    Rational divide​(long divisor)
    Computes the value of this Rational divided by the given number.
    Rational divide​(Rational divisor)
    Computes the value of this Rational divided by the given other Rational.
    double doubleValue()
    Converts this Rational value to a double.
    boolean equals​(Object other)
    Test whether this Rational is equivalent to the given object.
    float floatValue()
    Converts this Rational value to a float.
    BigInteger getDenominator()
    Get the denominator part of this Rational number's fractional form.
    BigInteger getNumerator()
    Get the numerator part of this Rational number's fractional form.
    int hashCode()
    Returns the hash code for this Rational.
    int intValue()
    Converts this Rational value to an int.
    boolean isProper()
    Returns true if this is a proper fraction.
    boolean isRepeatingDecimal()
    Returns true if this value has a non-terminating decimal expansion.
    long longValue()
    Converts this Rational value to a long.
    Rational multiply​(double multiplier)
    Computes the value of this Rational multiplied by the given number.
    Rational multiply​(long multiplier)
    Computes the value of this Rational multiplied by the given number.
    Rational multiply​(Rational multiplier)
    Computes the value of this Rational multiplied by the given other Rational.
    Rational negate()
    Returns a Rational that is the additive inverse of this value.
    Rational pow​(int exponent)
    Computes the value of this Rational raised to the power of the given exponent.
    Rational reciprocal()
    Returns a Rational that is the multiplicative inverse of this value.
    int signum()
    Returns the sign of this value.
    Rational subtract​(double subtrahend)
    Computes the value of this Rational minus the given number.
    Rational subtract​(long subtrahend)
    Computes the value of this Rational minus the given number.
    Rational subtract​(Rational subtrahend)
    Computes the value of this Rational minus the given other Rational.
    String toString()
    Returns the string representation of this Rational.
    static Rational valueOf​(double source)
    Translates the given double into an equivalent Rational value.
    static Rational valueOf​(long source)
    Translates the given long into an equivalent Rational value.
    static Rational valueOf​(String source)
    Translates the given String into an equivalent Rational value.

    Methods inherited from class java.lang.Number

    byteValue, shortValue

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait