Class Rational

java.lang.Object
java.lang.Number
eu.bandm.tools.ops.Rational
All Implemented Interfaces:
Serializable, Comparable<Rational>

public class Rational extends Number implements Comparable<Rational>
Realizing Rational numbers, internally based on BigInteger.

All instances returned by this code are always cancelled and normalized. (Internal intermediate data not necessarily.)

See Also:
  • Field Details

  • Method Details

    • valueOf

      public static final Rational valueOf(long n)
      evident
    • getNumerator

      public final BigInteger getNumerator()
      evident
    • getDenominator

      public final BigInteger getDenominator()
      evident
    • isInteger

      public final boolean isInteger()
      evident
    • isZero

      public final boolean isZero()
      evident
    • isOne

      public final boolean isOne()
      evident
    • isPositive

      public final boolean isPositive()
      evident
    • isNegative

      public final boolean isNegative()
      evident
    • isNatural

      public final boolean isNatural()
      evident
    • valueOf

      public static final Rational valueOf(String n) throws NumberFormatException
      Parser. The string must be either a plain integer, according to Integer.parseInt(String). Or it contains exactly one "/" sign, separating both such strings. The string after the "/" must not have a sign symbol.
      Throws:
      NumberFormatException
    • valueOf

      public static final Rational valueOf(BigInteger n)
      evident.
    • tenPower

      protected static BigInteger tenPower(int exp)
      Calculates 10^exp. Uses the local cache.
    • valueOf

      public static final Rational valueOf(BigDecimal n)
      evident
    • valueOf

      public static final Rational valueOf(long n, long d)
      evident
    • valueOf

      public static final Rational valueOf(BigInteger n, BigInteger d)
      evident
    • bigIntegerValue

      public BigInteger bigIntegerValue()
      Return rounded value. Implies rounding towards zero, according to Java default rules. Relies on BigInteger.divide(BigInteger). See Java specification.
    • byteValue

      public byte byteValue()
      Return rounded value.
      Overrides:
      byteValue in class Number
      See Also:
    • shortValue

      public short shortValue()
      Return rounded value.
      Overrides:
      shortValue in class Number
      See Also:
    • intValue

      public int intValue()
      Return rounded value.
      Specified by:
      intValue in class Number
      See Also:
    • intValues

      public int[] intValues()
      Returns enumerator and denominator. For the conversion of the interal BigIntegers see BigInteger.intValue().
    • longValue

      public long longValue()
      Return rounded value.
      Specified by:
      longValue in class Number
      See Also:
    • floatValue

      public float floatValue()
      Returns division result. For the conversion of the internal BigIntegers see BigInteger.floatValue().
      Specified by:
      floatValue in class Number
    • doubleValue

      public double doubleValue()
      Returns division result. For the conversion of the internal BigIntegers see BigInteger.doubleValue().
      Specified by:
      doubleValue in class Number
    • add

      public Rational add(Rational x)
      Return the normalized addition result.
    • subtract

      public Rational subtract(Rational x)
      Return the normalized subtraction result.
    • negate

      public Rational negate()
      Return the normalized negation.
    • multiply

      public Rational multiply(Rational x)
      Return the normalized multiplication result.
    • multiply

      public Rational multiply(long factor)
      Return the normalized multiplication result.
    • divide

      public Rational divide(Rational x)
      Return the normalized division result.
    • divide

      public Rational divide(long factor)
      Return the normalized division result.
    • inverse

      public Rational inverse()
      Return the normalized inverse result.
    • power

      public Rational power(int n)
      Return the normalized power of this to the exponent n.
    • modulo

      public Rational modulo(Rational x)
      Returns the fractional rest of the division by x, with "this/x" defined as with integer division, namely rounding towards zero, not towards negative infinity. This method behaves like java integer {code %}, cf https://docs.oracle.com/javase/specs/jls/se22/html/jls-15.html#jls-15.17.3.

         5%3 produces 2 (note that 5/3 produces 1)
         5%(-3) produces 2 (note that 5/(-3) produces -1)
         (-5)%3 produces -2 (note that (-5)/3 produces -1)
         (-5)%(-3) produces -2 (note that (-5)/(-3) produces 1)   *  
        
    • signum

      public int signum()
      Return the signum information -1,0,+1
    • abs

      public Rational abs()
      Return the absolute (positive) value
    • min

      public Rational min(Rational x)
      Return the smaller of this and x.
    • max

      public Rational max(Rational x)
      Return the larger of this and x.
    • equals

      public boolean equals(Object o)
      evident
      Overrides:
      equals in class Object
    • compareTo

      public int compareTo(Rational x)
      Specified by:
      compareTo in interface Comparable<Rational>
    • hashCode

      public int hashCode()
      evident
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      evident
      Overrides:
      toString in class Object
    • additiveName

      public String additiveName(@Opt @Opt String separator)
      Shows integer part and fractional rest separately. If integer part == 0, then the simple toString() is returned. Otheriwse, if value < 0, then the represenationof the negation is inclosed in -(..).
      Parameters:
      separator - string set betweeninteger and fractional part, defaults to » « (= one blank).