Class Rational

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

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

All results of arithmetic operations are normalized immediately.

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
    • 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
      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)
      FIXME ???
    • 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)
      Return the comparison result of this and x, from {-1,0,+1}.
      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