Class RationalNumber

  • All Implemented Interfaces:
    Serializable

    public final class RationalNumber
    extends Number
    Represents a rational number dividend/divisor with dividend and divisor being integer numbers.

    Since:
    2.0
    Version:
    1.2, June 21, 2019
    Author:
    Andi Huber, Werner Keil
    See Also:
    Serialized Form
    Implementation Requirements:
    This implementation uses BigInteger to represent 'dividend' and 'divisor'.
    • Field Detail

      • DIVISION_CHARACTER

        public static char DIVISION_CHARACTER
        The default DIVISION_CHARACTER is ÷ which (on Windows) can by typed using Alt+ 246.

        Note: Number parsing will fail if this is a white-space character.

    • Method Detail

      • ofInteger

        public static RationalNumber ofInteger​(long number)
        Returns a RationalNumber with divisor ONE. In other words, returns a RationalNumber that represents given integer number.
        Parameters:
        number -
        Returns:
        number/1
        Throws:
        NullPointerException - - if number is null
      • ofInteger

        public static RationalNumber ofInteger​(BigInteger number)
        Returns a RationalNumber with divisor ONE. In other words, returns a RationalNumber that represents given integer number.
        Parameters:
        number -
        Returns:
        number/1
        Throws:
        NullPointerException - - if number is null
      • of

        public static RationalNumber of​(long dividend,
                                        long divisor)
        Returns a RationalNumber that represents the division dividend/divisor.
        Parameters:
        dividend -
        divisor -
        Returns:
        dividend/divisor
        Throws:
        IllegalArgumentException - if divisor = 0
      • of

        public static RationalNumber of​(double number)
        Returns a RationalNumber that represents the given double precision number, with an accuracy equivalent to BigDecimal.valueOf(double).
        Parameters:
        number -
      • of

        public static RationalNumber of​(BigDecimal decimalValue)
        Returns a RationalNumber that represents the given BigDecimal decimalValue.
        Parameters:
        decimalValue -
      • of

        public static RationalNumber of​(BigInteger dividend,
                                        BigInteger divisor)
        Returns a RationalNumber that represents the division dividend/divisor.
        Implementation Note:
        this implementation stores dividend and divisor after canceling down from given parameters
        Parameters:
        dividend - the dividend
        divisor - the divisor
        Returns:
        dividend/divisor
        Throws:
        IllegalArgumentException - if divisor = 0
        NullPointerException - - if dividend is null or divisor is null
      • getDividend

        public BigInteger getDividend()
        For a non-negative rational number, returns a non-negative dividend. Otherwise returns a negative dividend. In other words, by convention, the integer returned includes the sign of this RationalNumber, whereas @link getDivisor() does not and is always non-negative.
        Returns:
        sign(a/b) * abs(a), (given rational number a/b)
      • getDivisor

        public BigInteger getDivisor()
        By convention, returns a non-negative divisor.
        Returns:
        abs(b), (given rational number a/b)
      • isInteger

        public boolean isInteger()
        Returns:
        whether this RationalNumber represents an integer number
      • signum

        public int signum()
        Returns:
        the sign of this RationalNumber: -1, 0 or +1
      • bigDecimalValue

        public BigDecimal bigDecimalValue()
        The BigDecimal representation of this RationalNumber.
        Implementation Note:
        the conversion calculation is done lazily and thread-safe
        Returns:
        this RationalNumber converted to BigDecimal representation
      • add

        public RationalNumber add​(RationalNumber that)
        Returns a new instance of RationalNumber representing the addition this + that.
        Parameters:
        that -
        Returns:
        this + that
      • subtract

        public RationalNumber subtract​(RationalNumber that)
        Returns a new instance of RationalNumber representing the subtraction this - that.
        Parameters:
        that -
        Returns:
        this - that
      • multiply

        public RationalNumber multiply​(RationalNumber that)
        Returns a new instance of RationalNumber representing the multiplication this * that.
        Parameters:
        that -
        Returns:
        this * that
      • divide

        public RationalNumber divide​(RationalNumber that)
        Returns a new instance of RationalNumber representing the division this / that.
        Parameters:
        that -
        Returns:
        this / that
      • negate

        public RationalNumber negate()
        Returns a new instance of RationalNumber representing the negation of this.
        Returns:
        -this
      • reciprocal

        public RationalNumber reciprocal()
        Returns a new instance of RationalNumber representing the reciprocal of this.
        Returns:
        1/this
      • pow

        public RationalNumber pow​(int exponent)
        Returns a new instance of RationalNumber representing the reciprocal of this.
        Parameters:
        exponent -
        Returns:
        this^exponent
      • abs

        public RationalNumber abs()
        Returns a RationalNumber whose value is the absolute value of this RationalNumber.
        Returns:
        abs(this)
      • compareTo

        public int compareTo​(RationalNumber that)
        Compares two RationalNumber values numerically.
        Parameters:
        that -
        Returns:
        the value 0 if this equals (numerically) that; a value less than 0 if this < that; and a value greater than 0 if this > that
      • toRationalString

        public String toRationalString()
        Returns a string representation of this RationalNumber, using fractional notation, eg. 5÷3 or -5÷3.
        Returns:
        string representation of this RationalNumber, using fractional notation.
        Since:
        2.0
      • toRationalString

        public String toRationalString​(char divisionCharacter)
        Returns a string representation of this RationalNumber, using fractional notation, eg. 5÷3 or -5÷3.
        Parameters:
        divisionCharacter - the character to use instead of the default ÷
        Returns:
        string representation of this RationalNumber, using fractional notation.
        Since:
        2.0
      • equals

        public boolean equals​(Object x)
        Compares this RationalNumber with the specified Object for equality.
        Overrides:
        equals in class Object
        Parameters:
        x - Object to which this RationalNumber is to be compared.
        Returns:
        true if and only if the specified Object is a RationalNumber whose value is numerically equal to this RationalNumber.