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.
    • 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
      • isPositiveOrZero

        public boolean isPositiveOrZero()
        Yields true if this number is positive or 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
      • 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. If one of them is not a number (according to isNaN()), then NaN is returned. If other is zero (according to is(int)), then an ArithmeticException is thrown. If both are infinite (according to isInfinite()), then NaN is returned.
        Parameters:
        other - the divisor
        Returns:
        this / other
        Throws:
        java.lang.ArithmeticException - if other is 0
      • 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
      • 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