Class BigRational

java.lang.Object
java.lang.Number
de.tilman_neumann.jml.base.BigRational
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<BigRational>

public class BigRational
extends java.lang.Number
implements java.lang.Comparable<BigRational>
Big rational numbers with exact arithmetics.
See Also:
Serialized Form
  • Field Details

  • Constructor Details

    • BigRational

      public BigRational​(java.math.BigInteger n)
      Constructor for an integer.
      Parameters:
      n - Number in decimal representation.
    • BigRational

      public BigRational​(java.math.BigInteger num, java.math.BigInteger den)
      Constructor for a rational number.
      Parameters:
      num - Numerator
      den - Denominator
  • Method Details

    • add

      public BigRational add​(BigRational b)
      Computes the sum of this and the argument.
      Parameters:
      b - Argument.
      Returns:
      sum
    • subtract

      public BigRational subtract​(BigRational b)
      Computes the subtraction of this and the argument.
      Parameters:
      b - Argument.
      Returns:
      subtraction
    • multiply

      public BigRational multiply​(java.math.BigInteger b)
      Computes the product of this and the argument.
      Parameters:
      b - Argument.
      Returns:
      product
    • multiply

      public BigRational multiply​(BigRational b)
      Computes the product of this and the argument.
      Parameters:
      b - Argument.
      Returns:
      product
    • divide

      public BigRational divide​(java.math.BigInteger b)
      Computes the fraction of this and the argument.
      Parameters:
      b - Argument.
      Returns:
      fraction
    • divide

      public BigRational divide​(BigRational b)
      Computes the fraction of this and the argument.
      Parameters:
      b - Argument.
      Returns:
      fraction
    • abs

      public BigRational abs()
      Returns:
      absolut value
    • negate

      public BigRational negate()
      Returns:
      copy of this with opposite sign
    • reciprocal

      public BigRational reciprocal()
      Returns:
      1/this
    • normalize

      public BigRational normalize()
      Returns:
      this with gcd of numerator and denominator reduced to 1, and the denominator being positive.
    • expandTo

      public BigRational expandTo​(java.math.BigInteger newDenominator)
    • getNumerator

      public java.math.BigInteger getNumerator()
      Returns:
      The numerator of this number.
    • getDenominator

      public java.math.BigInteger getDenominator()
      Returns:
      The denominator of this number.
    • signum

      public int signum()
      Returns:
      <0/0/>0 if this is negative/zero/positive.
    • isZero

      public boolean isZero()
      Returns:
      true if this is zero, false otherwise
    • isInteger

      public boolean isInteger()
      Returns:
      true if this is integral.
    • floor

      public java.math.BigInteger floor()
      Returns:
      the nearest smaller-or-equal integer value.
    • ceil

      public java.math.BigInteger ceil()
      Returns:
      the nearest bigger-or-equal integer value.
    • round

      public java.math.BigInteger round()
      Returns:
      the nearest integer value. In case of a tie, the smaller integer value is returned (rounding mode HALF_DOWN).
    • compareTo

      public int compareTo​(BigRational other)
      Specified by:
      compareTo in interface java.lang.Comparable<BigRational>
    • compareTo

      public int compareTo​(java.math.BigInteger other)
    • equals

      public boolean equals​(java.lang.Object o)
      Overrides:
      equals in class java.lang.Object
    • equals

      public boolean equals​(java.math.BigInteger other)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class java.lang.Object
    • byteValue

      public byte byteValue()
      Overrides:
      byteValue in class java.lang.Number
      Returns:
      this as a byte (rounding may be necessary)
    • intValue

      public int intValue()
      Specified by:
      intValue in class java.lang.Number
      Returns:
      this as an int (rounding may be necessary)
    • longValue

      public long longValue()
      Specified by:
      longValue in class java.lang.Number
      Returns:
      this as a long integer (rounding may be necessary)
    • floatValue

      public float floatValue()
      Specified by:
      floatValue in class java.lang.Number
      Returns:
      this as a float (rounding may be necessary)
    • doubleValue

      public double doubleValue()
      Specified by:
      doubleValue in class java.lang.Number
      Returns:
      this as a double (rounding may be necessary)
    • toBigDecimal

      public java.math.BigDecimal toBigDecimal​(Precision decPrec)
    • toBigDecimal

      public java.math.BigDecimal toBigDecimal​(Scale decPrec)
      Converts this to a BigDecimal with decPrec digits precision.
      Parameters:
      decPrec - Precision in decimal digits.
      Returns:
      this as a BigDecimal with the wanted precision.
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object
      Returns:
      this as a (fractional) string
    • toString

      public java.lang.String toString​(Scale decPrec)
      Converts this into a string with the given decimal digits precision.
      Parameters:
      decPrec - output precision in decimal digits.
      Returns:
      this as a string with the wanted precision.