Class NumberHelper


  • public final class NumberHelper
    extends Object
    Author:
    Daniel Kuppitz (http://gremlin.guru)
    • Method Detail

      • getHighestCommonNumberClass

        public static Class<? extends Number> getHighestCommonNumberClass​(Number... numbers)
      • getHighestCommonNumberClass

        public static Class<? extends Number> getHighestCommonNumberClass​(boolean forceFloatingPoint,
                                                                          Number... numbers)
      • add

        public static Number add​(Number a,
                                 Number b)
        Adds two numbers returning the highest common number class between them.

        This method returns a result using the highest common number class between the two inputs. If an overflow occurs (either integer or floating-point), the method promotes the precision by increasing the bit width, until a suitable type is found. If no suitable type exists (e.g., for very large integers beyond 64-bit), an ArithmeticException is thrown. For floating-point numbers, if double overflows, the result is Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY instead of an exception.

             a = 1, b = 1 -> 2
             a = null, b = 1 -> null
             a = 1, b = null -> 1
             a = null, b = null -> null
         
        Parameters:
        a - should be thought of as the seed to be modified by b
        b - the modifier to {code a}
      • sub

        public static Number sub​(Number a,
                                 Number b)
        Subtracts two numbers returning the highest common number class between them.

        This method returns a result using the highest common number class between the two inputs. If an overflow occurs (either integer or floating-point), the method promotes the precision by increasing the bit width, until a suitable type is found. If no suitable type exists (e.g., for very large integers beyond 64-bit), an ArithmeticException is thrown. For floating-point numbers, if double overflows, the result is Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY instead of an exception.

             a = 1, b = 1 -> 0
             a = null, b = 1 -> null
             a = 1, b = null -> 1
             a = null, b = null -> null
         
        Parameters:
        a - should be thought of as the seed to be modified by b
        b - the modifier to {code a}
      • mul

        public static Number mul​(Number a,
                                 Number b)
        Multiplies two numbers returning the highest common number class between them.

        This method returns a result using the highest common number class between the two inputs. If an overflow occurs (either integer or floating-point), the method promotes the precision by increasing the bit width, until a suitable type is found. If no suitable type exists (e.g., for very large integers beyond 64-bit), an ArithmeticException is thrown. For floating-point numbers, if double overflows, the result is Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY instead of an exception.

             a = 1, b = 2 -> 2
             a = null, b = 1 -> null
             a = 1, b = null -> 1
             a = null, b = null -> null
         
        Parameters:
        a - should be thought of as the seed to be modified by b
        b - the modifier to {code a}
      • div

        public static Number div​(Number a,
                                 Number b,
                                 boolean forceFloatingPoint)
        Divides two numbers returning the highest common number class between them.

        This method returns a result using the highest common number class between the two inputs. If an overflow occurs (either integer or floating-point), the method promotes the precision by increasing the bit width, until a suitable type is found. If no suitable type exists (e.g., for very large integers beyond 64-bit), an ArithmeticException is thrown. For floating-point numbers, if double overflows, the result is Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY instead of an exception.

             a = 4, b = 2 -> 2
             a = null, b = 1 -> null
             a = 1, b = null -> 1
             a = null, b = null -> null
         
        Parameters:
        a - should be thought of as the seed to be modified by b
        b - the modifier to {code a}
        forceFloatingPoint - when set to true ensures that the return value is the highest common floating number class
      • min

        public static Number min​(Number a,
                                 Number b)
        Gets the smaller number of the two provided returning the highest common number class between them.
             a = 4, b = 2 -> 2
             a = null, b = 1 -> 1
             a = 1, b = null -> 1
             a = null, b = null -> null
             a = NaN, b = 1 -> 1
             a = 1, b = NaN -> 1
             a = NaN, b = NaN -> NaN
         
      • min

        public static Comparable min​(Comparable a,
                                     Comparable b)
        Gets the smaller number of the two provided returning the highest common number class between them.
             a = 4, b = 2 -> 2
             a = null, b = 1 -> 1
             a = 1, b = null -> 1
             a = null, b = null -> null
             a = NaN, b = 1 -> 1
             a = 1, b = NaN -> 1
             a = NaN, b = NaN -> NaN
         
      • max

        public static Number max​(Number a,
                                 Number b)
        Gets the larger number of the two provided returning the highest common number class between them.
             a = 4, b = 2 -> 4
             a = null, b = 1 -> 1
             a = 1, b = null -> 1
             a = null, b = null -> null
             a = NaN, b = 1 -> 1
             a = 1, b = NaN -> 1
             a = NaN, b = NaN -> NaN
         
      • max

        public static Comparable max​(Comparable a,
                                     Comparable b)
        Gets the larger number of the two provided returning the highest common number class between them.
             a = 4, b = 2 -> 4
             a = null, b = 1 -> 1
             a = 1, b = null -> 1
             a = null, b = null -> null
             a = NaN, b = 1 -> 1
             a = 1, b = NaN -> 1
             a = NaN, b = NaN -> NaN
         
      • compare

        public static Integer compare​(Number a,
                                      Number b)
        Compares two numbers. Follows orderability semantics for NaN, which places NaN after +Inf.
             a = 4, b = 2 -> 1
             a = 2, b = 4 -> -1
             a = null, b = 1 -> -1
             a = 1, b = null -> 1
             a = null, b = null -> 0
             a = NaN, b = NaN -> 0
             a = NaN, b = Inf -> 1
         
      • coerceTo

        public static Number coerceTo​(Number a,
                                      Class<? extends Number> clazz)
        Coerces the given number to the specified numeric type if it can fit into it. Otherwise, retains the original type.
        Parameters:
        a - the number to be coerced
        clazz - the target numeric type class
        Returns:
        the coerced number in the specified type or the original type if it cannot fit
        Throws:
        IllegalArgumentException - if the specified numeric type is unsupported
      • castTo

        public static Number castTo​(Number a,
                                    GType typeToken)
        Casts the given number to the specified numeric type if it can fit into it. Otherwise, throw.
        Parameters:
        a - the number to be cast
        typeToken - the number token denoting the desired type to cast
        Returns:
        the number cast to the specified type
        Throws:
        IllegalArgumentException - if the specified numeric type is unsupported
        ArithmeticException - if the number overflows
      • isNaN

        public static boolean isNaN​(Object object)
      • eitherAreNaN

        public static boolean eitherAreNaN​(Object first,
                                           Object second)
      • bothAreNaN

        public static boolean bothAreNaN​(Object first,
                                         Object second)
      • isPositiveInfinity

        public static boolean isPositiveInfinity​(Object value)
      • isNegativeInfinity

        public static boolean isNegativeInfinity​(Object value)