Class BigRational

java.lang.Object
io.github.syst3ms.skriptparser.util.math.BigRational
All Implemented Interfaces:
Comparable<BigRational>

public class BigRational extends Object implements Comparable<BigRational>
A rational number represented as a quotient of two values.

Basic calculations with rational numbers (+ - * /) have no loss of precision. This allows to use BigRational as a replacement for BigDecimal if absolute accuracy is desired.

Wikipedia: Rational number

The values are internally stored as BigDecimal (for performance optimizations) but represented as BigInteger (for mathematical correctness)

The following basic calculations have no loss of precision:

The following calculations are special cases of the ones listed above and have no loss of precision:

I do not claim ownership of this code, it is the intellectual property of @obermuhlner.
  • Field Details

  • Method Details

    • getNumerator

      public BigDecimal getNumerator()
      Returns the numerator of this rational number as BigDecimal.
      Returns:
      the numerator as BigDecimal
    • getDenominator

      public BigDecimal getDenominator()
      Returns the denominator of this rational number as BigDecimal.

      Guaranteed to not be 0.

      Guaranteed to be positive.

      Returns:
      the denominator as BigDecimal
    • negate

      public BigRational negate()
      Negates this rational number (inverting the sign).

      The result has no loss of precision.

      Examples:

      • BigRational.valueOf(3.5).negate() returns BigRational.valueOf(-3.5)
      Returns:
      the negated rational number
    • reciprocal

      public BigRational reciprocal()
      Calculates the reciprocal of this rational number (1/x).

      The result has no loss of precision.

      Examples:

      • BigRational.valueOf(0.5).reciprocal() returns BigRational.valueOf(2)
      • BigRational.valueOf(-2).reciprocal() returns BigRational.valueOf(-0.5)
      Returns:
      the reciprocal rational number
      Throws:
      ArithmeticException - if this number is 0 (division by zero)
    • add

      public BigRational add(BigRational value)
      Calculates the addition (+) of this rational number and the specified argument.

      The result has no loss of precision.

      Parameters:
      value - the rational number to add
      Returns:
      the resulting rational number
    • add

      public BigRational add(BigInteger value)
      Calculates the addition (+) of this rational number and the specified argument.

      This is functionally identical to this.add(BigRational.valueOf(value)) but slightly faster.

      The result has no loss of precision.

      Parameters:
      value - the BigInteger to add
      Returns:
      the resulting rational number
    • subtract

      public BigRational subtract(BigRational value)
      Calculates the subtraction (-) of this rational number and the specified argument.

      The result has no loss of precision.

      Parameters:
      value - the rational number to subtract
      Returns:
      the resulting rational number
    • multiply

      public BigRational multiply(BigRational value)
      Calculates the multiplication (*) of this rational number and the specified argument.

      The result has no loss of precision.

      Parameters:
      value - the rational number to multiply
      Returns:
      the resulting rational number
    • multiply

      public BigRational multiply(BigInteger value)
      Calculates the multiplication (*) of this rational number and the specified argument.

      This is functionally identical to this.multiply(BigRational.valueOf(value)) but slightly faster.

      The result has no loss of precision.

      Parameters:
      value - the BigInteger to multiply
      Returns:
      the resulting rational number
    • multiply

      public BigRational multiply(int value)
      Calculates the multiplication (*) of this rational number and the specified argument.

      This is functionally identical to this.multiply(BigRational.valueOf(value)) but slightly faster.

      The result has no loss of precision.

      Parameters:
      value - the int value to multiply
      Returns:
      the resulting rational number
    • divide

      public BigRational divide(BigRational value)
      Calculates the division (/) of this rational number and the specified argument.

      The result has no loss of precision.

      Parameters:
      value - the rational number to divide (0 is not allowed)
      Returns:
      the resulting rational number
      Throws:
      ArithmeticException - if the argument is 0 (division by zero)
    • divide

      public BigRational divide(BigInteger value)
      Calculates the division (/) of this rational number and the specified argument.

      This is functionally identical to this.divide(BigRational.valueOf(value)) but slightly faster.

      The result has no loss of precision.

      Parameters:
      value - the BigInteger to divide (0 is not allowed)
      Returns:
      the resulting rational number
      Throws:
      ArithmeticException - if the argument is 0 (division by zero)
    • divide

      public BigRational divide(int value)
      Calculates the division (/) of this rational number and the specified argument.

      This is functionally identical to this.divide(BigRational.valueOf(value)) but slightly faster.

      The result has no loss of precision.

      Parameters:
      value - the int value to divide (0 is not allowed)
      Returns:
      the resulting rational number
      Throws:
      ArithmeticException - if the argument is 0 (division by zero)
    • isZero

      public boolean isZero()
      Returns whether this rational number is zero.
      Returns:
      true if this rational number is zero (0), false if it is not zero
    • pow

      public BigRational pow(int exponent)
      Calculates this rational number to the power (xy) of the specified argument.

      The result has no loss of precision.

      Parameters:
      exponent - exponent to which this rational number is to be raised
      Returns:
      the resulting rational number
    • toBigDecimal

      public BigDecimal toBigDecimal()
      Returns this rational number as a BigDecimal.
      Returns:
      the BigDecimal value
    • toBigDecimal

      public BigDecimal toBigDecimal(MathContext mc)
      Returns this rational number as a BigDecimal with the precision specified by the MathContext.
      Parameters:
      mc - the MathContext specifying the precision of the calculated result
      Returns:
      the BigDecimal
    • compareTo

      public int compareTo(BigRational other)
      Specified by:
      compareTo in interface Comparable<BigRational>
    • hashCode

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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • valueOf

      public static BigRational valueOf(int value)
      Creates a rational number of the specified int value.
      Parameters:
      value - the int value
      Returns:
      the rational number
    • valueOf

      public static BigRational valueOf(int numerator, int denominator)
      Creates a rational number of the specified numerator/denominator int values.
      Parameters:
      numerator - the numerator int value
      denominator - the denominator int value (0 not allowed)
      Returns:
      the rational number
      Throws:
      ArithmeticException - if the denominator is 0 (division by zero)
    • valueOf

      public static BigRational valueOf(BigInteger numerator, BigInteger denominator)
      Creates a rational number of the specified numerator/denominator BigInteger values.
      Parameters:
      numerator - the numerator BigInteger value
      denominator - the denominator BigInteger value (0 not allowed)
      Returns:
      the rational number
      Throws:
      ArithmeticException - if the denominator is 0 (division by zero)
    • valueOf

      public static BigRational valueOf(BigInteger value)
      Creates a rational number of the specified BigInteger value.
      Parameters:
      value - the BigInteger value
      Returns:
      the rational number
    • valueOf

      public static BigRational valueOf(BigDecimal value)
      Creates a rational number of the specified BigDecimal value.
      Parameters:
      value - the double value
      Returns:
      the rational number