Class MathNumber

  • All Implemented Interfaces:
    java.lang.Comparable<MathNumber>

    public class MathNumber
    extends java.lang.Object
    implements java.lang.Comparable<MathNumber>
    A wrapper around BigDecimal to represent the mathematical concept of a number, that can be also plus or minus infinity, in a convenient way.
    • Constructor Summary

      Constructors 
      Constructor Description
      MathNumber​(double number)
      Builds a math number representing the given value.
      MathNumber​(long number)
      Builds a math number representing the given value.
      MathNumber​(java.math.BigDecimal number)
      Builds a math number representing the given value.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      MathNumber abs()
      Yields the absolute value of this number.
      MathNumber add​(MathNumber other)
      Yields the result of this + other.
      int compareTo​(MathNumber other)  
      MathNumber divide​(MathNumber other)
      Yields the result of this / other.
      boolean equals​(java.lang.Object obj)  
      boolean geq​(MathNumber other)
      Yields true if this number is greater than or equal to other.
      java.math.BigDecimal getNumber()
      Yields the BigDecimal of this abstract value.
      boolean gt​(MathNumber other)
      Yields true if this number is greater than other.
      int hashCode()  
      boolean is​(int n)
      Yields true if this number is number represents exactly the given integer.
      boolean isFinite()
      Yields true if this number is finite (i.e.
      boolean isInfinite()
      Yields true if this number is infinite (i.e.
      boolean isMinusInfinity()
      Yields true if this number is minus infinity.
      boolean isNaN()
      Yields true if this number is not a number, that is, obtained from an operation that does not produce a result (e.g.
      boolean isNegative()
      Yields true if this number is negative.
      boolean isPlusInfinity()
      Yields true if this number is plus infinity.
      boolean isPositive()
      Yields true if this number is positive.
      boolean isZero()
      Yields true if this number is equal to zero.
      boolean leq​(MathNumber other)
      Yields true if this number is less than or equals to other.
      boolean lt​(MathNumber other)
      Yields true if this number is less than other.
      MathNumber max​(MathNumber other)
      Yields the maximum number between this and other.
      MathNumber min​(MathNumber other)
      Yields the minimum number between this and other.
      MathNumber multiply​(MathNumber other)
      Yields the result of this * other.
      MathNumber roundDown()
      Rounds down this number to the next integer value towards minus infinity (see RoundingMode.FLOOR).
      MathNumber roundUp()
      Rounds down this number to the next integer value towards plus infinity (see RoundingMode.CEILING).
      MathNumber subtract​(MathNumber other)
      Yields the result of this - other.
      byte toByte()
      Yields the byte value of this math number.
      double toDouble()
      Yields the double value of this math number.
      float toFloat()
      Yields the float value of this math number.
      int toInt()
      Yields the integer value of this math number.
      long toLong()
      Yields the long value of this math number.
      short toShort()
      Yields the short value of this math number.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • PLUS_INFINITY

        public static final MathNumber PLUS_INFINITY
        The constant for plus infinity.
      • MINUS_INFINITY

        public static final MathNumber MINUS_INFINITY
        The constant for minus infinity.
      • ZERO

        public static final MathNumber ZERO
        The constant 0.
      • ONE

        public static final MathNumber ONE
        The constant 1.
      • MINUS_ONE

        public static final MathNumber MINUS_ONE
        The constant -1.
      • NaN

        public static final MathNumber NaN
        A constant for representing numbers obtained from an operation that does not produce a result (e.g. infinity divided by infinity).
    • Constructor Detail

      • MathNumber

        public MathNumber​(long number)
        Builds a math number representing the given value.
        Parameters:
        number - the value
      • MathNumber

        public MathNumber​(double number)
        Builds a math number representing the given value.
        Parameters:
        number - the value
      • MathNumber

        public MathNumber​(java.math.BigDecimal number)
        Builds a math number representing the given value.
        Parameters:
        number - the value
    • Method Detail

      • isMinusInfinity

        public boolean isMinusInfinity()
        Yields true if this number is minus infinity.
        Returns:
        true if that condition holds
      • isPlusInfinity

        public boolean isPlusInfinity()
        Yields true if this number is plus infinity.
        Returns:
        true if that condition holds
      • isInfinite

        public boolean isInfinite()
        Yields true if this number is infinite (i.e. plus or minus infinity).
        Returns:
        true if that condition holds
      • isFinite

        public boolean isFinite()
        Yields true if this number is finite (i.e. not infinity).
        Returns:
        true if that condition holds
      • is

        public boolean is​(int n)
        Yields true if this number is number represents exactly the given integer.
        Parameters:
        n - the integer to test
        Returns:
        true if that condition holds
      • isZero

        public boolean isZero()
        Yields true if this number is equal to zero.
        Returns:
        true if that condition holds
      • isNegative

        public boolean isNegative()
        Yields true if this number is negative.
        Returns:
        true if that condition holds
      • isPositive

        public boolean isPositive()
        Yields true if this number is positive.
        Returns:
        true if that condition holds
      • isNaN

        public boolean isNaN()
        Yields true if this number is not a number, that is, obtained from an operation that does not produce a result (e.g. infinity divided by infinity).
        Returns:
        true if that condition holds
      • add

        public MathNumber add​(MathNumber other)
        Yields the result of this + other. If one of them is not a number (according to isNaN()), then NaN is returned.
        Parameters:
        other - the other operand
        Returns:
        this + other
      • subtract

        public MathNumber subtract​(MathNumber other)
        Yields the result of this - other. If one of them is not a number (according to isNaN()), then NaN is returned.
        Parameters:
        other - the other operand
        Returns:
        this - other
      • multiply

        public MathNumber multiply​(MathNumber other)
        Yields the result of this * other. If one of them is not a number (according to isNaN()), then NaN is returned.
        Parameters:
        other - the other factor
        Returns:
        this * other
      • divide

        public MathNumber divide​(MathNumber other)
        Yields the result of this / other. This method returns NaN if one of the argument is not a number (according to isNaN()), if other is zero (according to isZero()), or if both arguments are infinite (according to isInfinite()).
        Parameters:
        other - the divisor
        Returns:
        this / other
      • compareTo

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

        public MathNumber min​(MathNumber other)
        Yields the minimum number between this and other. If one of them is not a number (according to isNaN()), then NaN is returned.
        Parameters:
        other - the other number
        Returns:
        the minimum between this and other
      • max

        public MathNumber max​(MathNumber other)
        Yields the maximum number between this and other. If one of them is not a number (according to isNaN()), then NaN is returned.
        Parameters:
        other - the other number
        Returns:
        the maximum between this and other
      • leq

        public boolean leq​(MathNumber other)
        Yields true if this number is less than or equals to other.
        Parameters:
        other - the other number
        Returns:
        true if @code{this} is less or equals than @code{other}.
      • gt

        public boolean gt​(MathNumber other)
        Yields true if this number is greater than other.
        Parameters:
        other - the other number
        Returns:
        true if
      • lt

        public boolean lt​(MathNumber other)
        Yields true if this number is less than other.
        Parameters:
        other - the other number
        Returns:
        true if
      • geq

        public boolean geq​(MathNumber other)
        Yields true if this number is greater than or equal to other.
        Parameters:
        other - the other number
        Returns:
        true if
      • abs

        public MathNumber abs()
        Yields the absolute value of this number. If it is not a number (according to isNaN()), then NaN is returned.
        Returns:
        the absolute value of this
      • roundUp

        public MathNumber roundUp()
        Rounds down this number to the next integer value towards plus infinity (see RoundingMode.CEILING). If this number is infinite or is not a number (according to isInfinite() and isNaN(), respectively), this is returned without rounding.
        Returns:
        this number rounded up towards plus infinity
      • roundDown

        public MathNumber roundDown()
        Rounds down this number to the next integer value towards minus infinity (see RoundingMode.FLOOR). If this number is infinite or is not a number (according to isInfinite() and isNaN(), respectively), this is returned without rounding.
        Returns:
        this number rounded down towards minus infinity
      • hashCode

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

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

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getNumber

        public java.math.BigDecimal getNumber()
        Yields the BigDecimal of this abstract value.
        Returns:
        the BigDecimal of this abstract value
        Throws:
        java.lang.IllegalStateException - If this number is not a number or is plus infinite or is minus infinite (according to isNaN(), isPlusInfinity() and isMinusInfinity() respectively).