Class BigRational

java.lang.Object
solvers.dataStructures.BigRational
All Implemented Interfaces:
Comparable<BigRational>

public class BigRational extends Object implements Comparable<BigRational>
This class is a representation of rational numbers. Essentially, it uses two BigIntegers to represent any rational number. It also contains the necessary methods for the basic arithmetic operations.
Author:
Michail Makaronidis, 2010
  • Field Details

    • ZERO

      public static final BigRational ZERO
      This is a variable equal to zero.
    • ONE

      public static final BigRational ONE
      This is a variable equal to one.
    • MINUS_ONE

      public static final BigRational MINUS_ONE
      This is a variable equal to minus one.
  • Constructor Details

    • BigRational

      public BigRational(int numerator, int denominator)
      The constructor creates and initialises a new BigRational object
      Parameters:
      numerator - The numerator of the rational number
      denominator - The denominator of the rational number
    • BigRational

      public BigRational(int numerator)
      The constructor creates and initialises a new BigRational object where the rational is an integer (no denominator is needed).
      Parameters:
      numerator - The number
    • BigRational

      public BigRational(String s)
      The constructor creates and initialises a new BigRational object from a string, e.g., "-343/1273".
      Parameters:
      s - The string representing the number
    • BigRational

      public BigRational(BigInteger numerator, BigInteger denominator)
      The constructor creates and initialises a new BigRational object
      Parameters:
      numerator - The numerator of the rational number
      denominator - The denominator of the rational number
    • BigRational

      public BigRational(BigDecimal numerator, BigDecimal denominator)
      The constructor creates and initialises a new BigRational object
      Parameters:
      numerator - The numerator of the rational number
      denominator - The denominator of the rational number
    • BigRational

      public BigRational(BigDecimal a)
      The constructor creates and initialises a new BigRational object and sets it equal to a BigDecimal. No denominator is needed as input, it is added automatically.
      Parameters:
      a - The number
    • BigRational

      public BigRational(BigInteger a)
      The constructor creates and initialises a new BigRational object and sets it equal to a BigInteger. No denominator is needed as input, it is added automatically.
      Parameters:
      a - The number
    • BigRational

      public BigRational(double d)
      The constructor creates and initialises a new BigRational object and sets it equal to a double. No denominator is needed as input, it is added automatically.
      Parameters:
      d - The number
    • BigRational

      public BigRational()
      The constructor creates and initialises a new BigRational object with the default value of zero.
  • Method Details

    • copy

      public BigRational copy()
      Returns a copy of the current BigRational. Amended by Jack, to copy undefined as well
      Returns:
      The BigRational object copy
    • toString

      public String toString()
      Return string representation of the BigRational
      Overrides:
      toString in class Object
      Returns:
      String representation of the BigRational
    • compareTo

      public int compareTo(BigRational b)
      Compares the BigRational with another.
      Specified by:
      compareTo in interface Comparable<BigRational>
      Parameters:
      b - The other number
      Returns:
      Returns { -1, 0, + 1 } if a < b, a = b, or a > b
    • greaterThan

      public boolean greaterThan(BigRational b)
      Checks whether this number is greater than another.
      Parameters:
      b - The other number
      Returns:
      True if this number is greater than the other, else false.
    • smallerThan

      public boolean smallerThan(BigRational b)
      Checks whether this number is smaller than another.
      Parameters:
      b - The other number
      Returns:
      True if this number is smaller than the other, else false.
    • greaterOrEqualThan

      public boolean greaterOrEqualThan(BigRational b)
      Checks whether this number is greater than or equal to another.
      Parameters:
      b - The other number
      Returns:
      True if this number is greater than or equal to the other, else false.
    • smallerOrEqualThan

      public boolean smallerOrEqualThan(BigRational b)
      Checks whether this number is smaller than or equal to another.
      Parameters:
      b - The other number
      Returns:
      True if this number is smaller than or equal to the other, else false.
    • isZero

      public boolean isZero()
      Decides if this BigRational is equal to zero.
      Returns:
      True if equal to zero, false otherwise
    • isOne

      public boolean isOne()
      Decides if this BigRational is equal to one.
      Returns:
      True if equal to one, false otherwise
    • isMinusOne

      public boolean isMinusOne()
      Decides if this BigRational is equal to minus one.
      Returns:
      True if equal to minus one, false otherwise
    • isPositive

      public boolean isPositive()
      Decides if this BigRational is positive.
      Returns:
      True if positive, false otherwise
    • isNegative

      public boolean isNegative()
      Decides if this BigRational is negative.
      Returns:
      True if negative, false otherwise
    • abs

      public BigRational abs()
      Returns a BigRational object equal to the absolute value of this BigRational.
      Returns:
      The absolute value BigRational
    • equals

      public boolean equals(Object y)
      Decides if this BigRational is equal to another.
      Overrides:
      equals in class Object
      Parameters:
      y - The other BigRational
      Returns:
      True if equal, false otherwise
    • hashCode

      public int hashCode()
      Returns a hashCode consistent with equals() and compareTo().
      Overrides:
      hashCode in class Object
      Returns:
      The hashCode
    • multiply

      public BigRational multiply(BigRational b)
      Multiplies this number (a) with another (b)
      Parameters:
      b - The number b
      Returns:
      The number a*b
    • multiply

      public BigRational multiply(BigInteger b)
      Multiplies this number (a) with another (b)
      Parameters:
      b - The number b
      Returns:
      The number a*b
    • isInteger

      public boolean isInteger()
      Decides whether the BigRational represents an integer number.
      Returns:
      True if it represents an integer, no otherwise
    • add

      public BigRational add(BigRational b)
      Adds this number (a) to another (b)
      Parameters:
      b - The number b
      Returns:
      The number a+b
    • negate

      public BigRational negate()
      Negates this number (a)
      Returns:
      The number -a
    • subtract

      public BigRational subtract(BigRational b)
      Subtracts a number (b) from this one (a)
      Parameters:
      b - The number b
      Returns:
      The number a-b
    • reciprocal

      public BigRational reciprocal()
      Return the reciprocal (1/a) of this number (a)
      Returns:
      The number 1/a
    • divide

      public BigRational divide(BigRational b)
      Divides this number (a) by another (b)
      Parameters:
      b - The number b
      Returns:
      The number a/b
    • pow

      public BigRational pow(int n)
      Computes this number raised to the power of another
      Parameters:
      n - The exponent
      Returns:
      The result of the exponentiation
    • asBigDecimal

      public BigDecimal asBigDecimal()
      Returns the number as a BigDecimal. This can only work if the division of the numerator by the denominator does not have an infinite decimal expansion. This can be ensured by checking isBigDecimal() first.
      Returns:
      The division result
    • isBigDecimal

      public boolean isBigDecimal()
      Returns whether the number can be expressed as a BigDecimal. This can only happen if the division of the numerator by the denominator does not have an infinite decimal expansion.
      Returns:
      True if it can be expressed as a BigDecimal, false otherwise.
    • approximateAsDouble

      public double approximateAsDouble()
      Approximates the current rational number as a double. Leads to loss of precision both in decimal digits and (possibly) in magnitude (if this > MAX_DOUBLE), therefore it is used only for printing purposes and not for calculations.
      Returns:
      The double approximating this rational number.
    • makeUndefined

      public void makeUndefined()
      Flags the current BigRational as indeterminable.
    • makeDefined

      public void makeDefined()
      Flags the current BigRational as determinable (default).
    • isUndefined

      public boolean isUndefined()
      Checks whether the current BigRational is flagged as indeterminable.
      Returns:
      True if it is indeterminable, false otherwise.
    • mod

      public BigRational mod(BigInteger m)
      Returns (this mod m), allowing for negative numbers or zeros as results.
      Parameters:
      m - The divisor
      Returns:
      The result of the modulo operation
    • modNormal

      public BigRational modNormal(BigInteger m)
      Returns (this mod m), allowing for positive numbers or zeros as results.
      Parameters:
      m - The divisor
      Returns:
      The result of the modulo operation
    • log

      public double log()
      Returns the logarithm of this BigRational number
      Returns:
      The logarithm of this BigRational number