Class Rational

All Implemented Interfaces:
Serializable, Comparable<Rational>, Formattable

public final class Rational extends Number implements Comparable<Rational>, Formattable
A rational number, stored as numerator and denominator integer pair. Rational is a value-based class.
Version:
22.1.2024
Author:
H.Drachenfels
See Also:
  • Field Details

    • ZERO

      public static final Rational ZERO
      The number 0 as Rational.
    • ONE

      public static final Rational ONE
      The number 1 as Rational.
    • MAX_VALUE

      public static final Rational MAX_VALUE
      The largest positive value of type Rational, Integer.MAX_VALUE.
    • MIN_VALUE

      public static final Rational MIN_VALUE
      The smallest positive nonzero value of type Rational, 1 / Integer.MAX_VALUE.
    • numerator

      private final int numerator
      The numerator part of the rational number.
    • denominator

      private final int denominator
      The denominator part of the rational number. Always greater or equal 1. The denominator is 1 if numerator is 0.
    • FORMAT

      private static final Pattern FORMAT
  • Constructor Details

    • Rational

      private Rational(long n, long d)
  • Method Details

    • valueOf

      public static Rational valueOf(int n, int d)
      Rational number with numerator n and denominator d. For denominator 0 an IllegalArgumentException is thrown.
      Parameters:
      n - the numerator
      d - the denominator (must not be 0)
      Returns:
      a Rational with the specified value
    • valueOf

      public static Rational valueOf(int n)
      Converts an integer to a rational number. This factory method is provided in preference to an (int) constructor because it allows for reuse of frequently used Rationals.
      Parameters:
      n - the integer to convert
      Returns:
      a Rational with the specified value
    • valueOf

      public static Rational valueOf(String s)
      Converts a string to a rational number. The string must have the format specified by toString. Otherwise NumberFormatException is thrown.
      Parameters:
      s - the string to convert
      Returns:
      a Rational with the specified value
    • gcd

      private static long gcd(long a, long b)
      Computes the greates common divisor using Euclids algorithm. For negative numbers the result is undefined.
      Parameters:
      a - a positive number
      b - a positive number
      Returns:
      the gcd of a and b
    • getNumerator

      public int getNumerator()
      Returns the numerator part of the reduced rational number.
      Returns:
      the numerator
    • getDenominator

      public int getDenominator()
      Returns the denominator part of the reduced rational number.
      Returns:
      the denominator
    • signum

      public int signum()
      Returns the signum function of this Rational.
      Returns:
      -1, 0 or 1 as the value of this Rational is negative,zero or positive.
    • negate

      public Rational negate()
      Returns a Rational with inverted sign.
      Returns:
      -this
    • invert

      public Rational invert()
      Returns a Rational with numerator and denominator swapped.
      Returns:
      1/this
    • add

      public Rational add(Rational r)
      Returns a Rational whose value is (this + r).
      Parameters:
      r - value to be added to this Rational
      Returns:
      this + r
    • subtract

      public Rational subtract(Rational r)
      Returns a Rational whose value is (this - r).
      Parameters:
      r - value to be subtracted from this Rational
      Returns:
      this - r
    • multiply

      public Rational multiply(Rational r)
      Returns a Rational whose value is (this * r).
      Parameters:
      r - value to be multiplied by this Rational
      Returns:
      this * r
    • divide

      public Rational divide(Rational r)
      Returns a Rational whose value is (this / r).
      Parameters:
      r - value by which this Rational is to be divided
      Returns:
      this / r
    • toString

      public String toString()
      Returns a string representation of this Rational. The string decimal representation including sign of the numerator is followed by '/' and the string representation of the denominator.
      Overrides:
      toString in class Object
      Returns:
      string representation of this Rational.
    • equals

      public boolean equals(Object o)
      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
    • doubleValue

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

      public float floatValue()
      Specified by:
      floatValue in class Number
    • compareTo

      public int compareTo(Rational r)
      Specified by:
      compareTo in interface Comparable<Rational>
    • formatTo

      public void formatTo(Formatter f, int flags, int width, int precision)
      Specified by:
      formatTo in interface Formattable