Package com.drew.lang

Class Rational

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

public class Rational extends Number implements Comparable<Rational>, Serializable
Immutable class for holding a rational number without loss of precision. Provides a familiar representation via toString() in form numerator/denominator. Note that any value with a numerator of zero will be treated as zero, even if the denominator is also zero.
Author:
Drew Noakes https://drewnoakes.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Rational(long numerator, long denominator)
    Creates a new instance of Rational.
  • Method Summary

    Modifier and Type
    Method
    Description
    final byte
    Returns the value of the specified number as a byte.
    int
    Compares two Rational instances, returning true if they are mathematically equivalent (in consistence with equals(Object) method).
    double
    Returns the value of the specified number as a double.
    boolean
    Indicates whether this instance and other are numerically equal, even if their representations differ.
    boolean
    Compares two Rational instances, returning true if they are mathematically equivalent.
    boolean
    Indicates whether this instance and other have identical Numerator and Denominator.
    float
    Returns the value of the specified number as a float.
    Returns the absolute value of this object as a new Rational.
    final long
    Returns the denominator.
    final long
    Returns the numerator.
    Returns the reciprocal value of this object as a new Rational.
    Simplifies the representation of this Rational number.
    int
     
    final int
    Returns the value of the specified number as an int.
    boolean
    Checks if this Rational number is an Integer, either positive or negative.
    boolean
    True if the value is non-zero and numerator and denominator are either both positive or both negative.
    boolean
    Checks if either the numerator or denominator are zero.
    final long
    Returns the value of the specified number as a long.
    final short
    Returns the value of the specified number as a short.
    toSimpleString(boolean allowDecimal)
    Returns the simplest representation of this Rational's value possible.
    Returns a string representation of the object of form numerator/denominator.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Rational

      public Rational(long numerator, long denominator)
      Creates a new instance of Rational. Rational objects are immutable, so once you've set your numerator and denominator values here, you're stuck with them!
  • Method Details

    • doubleValue

      public double doubleValue()
      Returns the value of the specified number as a double. This may involve rounding.
      Specified by:
      doubleValue in class Number
      Returns:
      the numeric value represented by this object after conversion to type double.
    • floatValue

      public float floatValue()
      Returns the value of the specified number as a float. This may involve rounding.
      Specified by:
      floatValue in class Number
      Returns:
      the numeric value represented by this object after conversion to type float.
    • byteValue

      public final byte byteValue()
      Returns the value of the specified number as a byte. This may involve rounding or truncation. This implementation simply casts the result of doubleValue() to byte.
      Overrides:
      byteValue in class Number
      Returns:
      the numeric value represented by this object after conversion to type byte.
    • intValue

      public final int intValue()
      Returns the value of the specified number as an int. This may involve rounding or truncation. This implementation simply casts the result of doubleValue() to int.
      Specified by:
      intValue in class Number
      Returns:
      the numeric value represented by this object after conversion to type int.
    • longValue

      public final long longValue()
      Returns the value of the specified number as a long. This may involve rounding or truncation. This implementation simply casts the result of doubleValue() to long.
      Specified by:
      longValue in class Number
      Returns:
      the numeric value represented by this object after conversion to type long.
    • shortValue

      public final short shortValue()
      Returns the value of the specified number as a short. This may involve rounding or truncation. This implementation simply casts the result of doubleValue() to short.
      Overrides:
      shortValue in class Number
      Returns:
      the numeric value represented by this object after conversion to type short.
    • getDenominator

      public final long getDenominator()
      Returns the denominator.
    • getNumerator

      public final long getNumerator()
      Returns the numerator.
    • getReciprocal

      public Rational getReciprocal()
      Returns the reciprocal value of this object as a new Rational.
      Returns:
      the reciprocal in a new object
    • getAbsolute

      public Rational getAbsolute()
      Returns the absolute value of this object as a new Rational.
      Returns:
      the absolute value in a new object
    • isInteger

      public boolean isInteger()
      Checks if this Rational number is an Integer, either positive or negative.
    • isZero

      public boolean isZero()
      Checks if either the numerator or denominator are zero.
    • isPositive

      public boolean isPositive()
      True if the value is non-zero and numerator and denominator are either both positive or both negative.
    • toString

      public String toString()
      Returns a string representation of the object of form numerator/denominator.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the object.
    • toSimpleString

      public String toSimpleString(boolean allowDecimal)
      Returns the simplest representation of this Rational's value possible.
    • compareTo

      public int compareTo(Rational that)
      Compares two Rational instances, returning true if they are mathematically equivalent (in consistence with equals(Object) method).
      Specified by:
      compareTo in interface Comparable<Rational>
      Parameters:
      that - the Rational to compare this instance to.
      Returns:
      the value 0 if this Rational is equal to the argument Rational mathematically; a value less than 0 if this Rational is less than the argument Rational; and a value greater than 0 if this Rational is greater than the argument Rational.
    • equals

      public boolean equals(Rational other)
      Indicates whether this instance and other are numerically equal, even if their representations differ. For example, 1/2 is equal to 10/20 by this method. Similarly, 1/0 is equal to 100/0 by this method. To test equal representations, use EqualsExact.
      Parameters:
      other - The rational value to compare with
    • equalsExact

      public boolean equalsExact(Rational other)
      Indicates whether this instance and other have identical Numerator and Denominator.

      For example, 1/2 is not equal to 10/20 by this method. Similarly, 1/0 is not equal to 100/0 by this method. To test numerically equivalence, use Equals(Rational).

      Parameters:
      other - The rational value to compare with
    • equals

      public boolean equals(Object obj)
      Compares two Rational instances, returning true if they are mathematically equivalent.
      Overrides:
      equals in class Object
      Parameters:
      obj - the Rational to compare this instance to.
      Returns:
      true if instances are mathematically equivalent, otherwise false. Will also return false if obj is not an instance of Rational.
    • hashCode

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

      public Rational getSimplifiedInstance()

      Simplifies the representation of this Rational number.

      For example, 5/10 simplifies to 1/2 because both Numerator and Denominator share a common factor of 5.

      Uses the Euclidean Algorithm to find the greatest common divisor.

      Returns:
      A simplified instance if one exists, otherwise a copy of the original value.