Class MathHelper

java.lang.Object
com.helger.base.numeric.MathHelper

@Immutable public final class MathHelper extends Object
Contains several math help routines.
Author:
Philip Helger
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    abs(double dValue)
    This is a sanity method wrapping Math.abs (double), so that you don't have to think whether you need to invoke the abs method from this class or the one from Math directly.
    static float
    abs(float fValue)
    This is a sanity method wrapping Math.abs (float), so that you don't have to think whether you need to invoke the abs method from this class or the one from Math directly.
    static int
    abs(int nValue)
    This is a fix for Math.abs as it would return Integer.MIN_VALUE for Integer.MIN_VALUE which is very unexpected.
    static long
    abs(long nValue)
    This is a fix for Math.abs as it would return Long.MIN_VALUE for Long.MIN_VALUE which is very unexpected.
    static boolean
    canConvertLongToInt(long nValue)
    Check if the passed long value can be safely converted to an int without losing information.
    static double
    getDividedDouble(int nDividend, int nDivisor)
    Divides the passed int dividend through the passed divisor (nDividend / nDivisor)
    static double
    getDividedDouble(long nDividend, long nDivisor)
    Divides the passed int dividend through the passed divisor (nDividend / nDivisor)
    static int
    getIntDivided(int nDividend, int nDivisor, @NonNull RoundingMode eRoundingMode)
    Divide two integers using the specified rounding mode.
    static int
    getIntDividedCeil(int nDividend, int nDivisor)
    Divide two integers and round up (ceiling).
    static int
    getIntDividedFloor(int nDividend, int nDivisor)
    Divide two integers and round down (floor).
    static int
    getLongAsInt(long nValue, int nFallback)
    Convert a long to an int, returning a fallback value if the conversion would lose information.
    static long
    getLongDivided(long nDividend, long nDivisor, @NonNull RoundingMode eRoundingMode)
    Divide two longs using the specified rounding mode.
    static long
    getLongDividedCeil(long nDividend, long nDivisor)
    Divide two longs and round up (ceiling).
    static long
    getLongDividedFloor(long nDividend, long nDivisor)
    Divide two longs and round down (floor).
    static double
    getMaxDouble(double dValue, double... aValues)
    Get the maximum of the passed double values.
    static double
    getMaxFloat(float fValue, float... aValues)
    Get the maximum of the passed float values.
    static int
    getMaxInt(int nValue, int... aValues)
    Get the maximum of the passed int values.
    static long
    getMaxLong(long nValue, long... aValues)
    Get the maximum of the passed long values.
    static double
    getMinDouble(double dValue, double... aValues)
    Get the minimum of the passed double values.
    static double
    getMinFloat(float fValue, float... aValues)
    Get the minimum of the passed float values.
    static int
    getMinInt(int nValue, int... aValues)
    Get the minimum of the passed int values.
    static long
    getMinLong(long nValue, long... aValues)
    Get the minimum of the passed long values.
    static int
    getRoundedUp(int nToRound, int nMultiple)
    Round up to the nearest multiple of the value to round.
    static long
    Converts the passed signed integer to an unsigned long
    static double
    hypot(double a, double b)
     
    static boolean
    Check if only a single bit is set.
    Source: http://stackoverflow.com/questions/12483843/test-if-a-bitboard-have-only-one-bit-set-to-1
    Say n has any bits set, the least significant is bit number k.
    static boolean
    Check if only a single bit is set.
    Source: http://stackoverflow.com/questions/12483843/test-if-a-bitboard-have-only-one-bit-set-to-1
    Say n has any bits set, the least significant is bit number k.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getRoundedUp

      public static int getRoundedUp(int nToRound, @Nonnegative int nMultiple)
      Round up to the nearest multiple of the value to round.
      Parameters:
      nToRound - Value to round. May be positive or negative.
      nMultiple - Multiple to use. Must be ≥ 0.
      Returns:
      The rounded value.
    • getDividedDouble

      public static double getDividedDouble(int nDividend, int nDivisor)
      Divides the passed int dividend through the passed divisor (nDividend / nDivisor)
      Parameters:
      nDividend - the dividend
      nDivisor - the divisor
      Returns:
      a double representing the exact quotient. Returns Double.NaN if the divisor is 0.
    • getDividedDouble

      public static double getDividedDouble(long nDividend, long nDivisor)
      Divides the passed int dividend through the passed divisor (nDividend / nDivisor)
      Parameters:
      nDividend - the dividend
      nDivisor - the divisor
      Returns:
      a double representing the exact quotient. Returns Double.NaN if the divisor is 0.
    • getIntDividedCeil

      public static int getIntDividedCeil(int nDividend, int nDivisor)
      Divide two integers and round up (ceiling).
      Parameters:
      nDividend - the dividend
      nDivisor - the divisor
      Returns:
      the result of the division rounded towards positive infinity.
    • getIntDividedFloor

      public static int getIntDividedFloor(int nDividend, int nDivisor)
      Divide two integers and round down (floor).
      Parameters:
      nDividend - the dividend
      nDivisor - the divisor
      Returns:
      the result of the division rounded towards negative infinity.
    • getIntDivided

      public static int getIntDivided(int nDividend, int nDivisor, @NonNull RoundingMode eRoundingMode)
      Divide two integers using the specified rounding mode.
      Parameters:
      nDividend - the dividend
      nDivisor - the divisor
      eRoundingMode - The rounding mode to use. May not be null.
      Returns:
      the result of the division with the specified rounding applied.
    • getLongDividedCeil

      public static long getLongDividedCeil(long nDividend, long nDivisor)
      Divide two longs and round up (ceiling).
      Parameters:
      nDividend - the dividend
      nDivisor - the divisor
      Returns:
      the result of the division rounded towards positive infinity.
    • getLongDividedFloor

      public static long getLongDividedFloor(long nDividend, long nDivisor)
      Divide two longs and round down (floor).
      Parameters:
      nDividend - the dividend
      nDivisor - the divisor
      Returns:
      the result of the division rounded towards negative infinity.
    • getLongDivided

      public static long getLongDivided(long nDividend, long nDivisor, @NonNull RoundingMode eRoundingMode)
      Divide two longs using the specified rounding mode.
      Parameters:
      nDividend - the dividend
      nDivisor - the divisor
      eRoundingMode - The rounding mode to use. May not be null.
      Returns:
      the result of the division with the specified rounding applied.
    • canConvertLongToInt

      public static boolean canConvertLongToInt(long nValue)
      Check if the passed long value can be safely converted to an int without losing information.
      Parameters:
      nValue - The long value to check.
      Returns:
      true if the value fits in an int, false otherwise.
    • getLongAsInt

      @CheckReturnValue public static int getLongAsInt(long nValue, int nFallback)
      Convert a long to an int, returning a fallback value if the conversion would lose information.
      Parameters:
      nValue - The long value to convert.
      nFallback - The fallback value to return if the long cannot be safely converted to an int.
      Returns:
      The int value, or the fallback if the value does not fit in an int.
    • getMaxInt

      public static int getMaxInt(int nValue, int... aValues)
      Get the maximum of the passed int values.
      Parameters:
      nValue - The first value.
      aValues - The remaining values.
      Returns:
      The maximum of all passed values.
    • getMaxLong

      public static long getMaxLong(long nValue, long... aValues)
      Get the maximum of the passed long values.
      Parameters:
      nValue - The first value.
      aValues - The remaining values.
      Returns:
      The maximum of all passed values.
    • getMaxFloat

      public static double getMaxFloat(float fValue, float... aValues)
      Get the maximum of the passed float values.
      Parameters:
      fValue - The first value.
      aValues - The remaining values.
      Returns:
      The maximum of all passed values.
    • getMaxDouble

      public static double getMaxDouble(double dValue, double... aValues)
      Get the maximum of the passed double values.
      Parameters:
      dValue - The first value.
      aValues - The remaining values.
      Returns:
      The maximum of all passed values.
    • getMinInt

      public static int getMinInt(int nValue, int... aValues)
      Get the minimum of the passed int values.
      Parameters:
      nValue - The first value.
      aValues - The remaining values.
      Returns:
      The minimum of all passed values.
    • getMinLong

      public static long getMinLong(long nValue, long... aValues)
      Get the minimum of the passed long values.
      Parameters:
      nValue - The first value.
      aValues - The remaining values.
      Returns:
      The minimum of all passed values.
    • getMinFloat

      public static double getMinFloat(float fValue, float... aValues)
      Get the minimum of the passed float values.
      Parameters:
      fValue - The first value.
      aValues - The remaining values.
      Returns:
      The minimum of all passed values.
    • getMinDouble

      public static double getMinDouble(double dValue, double... aValues)
      Get the minimum of the passed double values.
      Parameters:
      dValue - The first value.
      aValues - The remaining values.
      Returns:
      The minimum of all passed values.
    • abs

      @Nonnegative public static int abs(int nValue)
      This is a fix for Math.abs as it would return Integer.MIN_VALUE for Integer.MIN_VALUE which is very unexpected. Instead an exception is thrown.
      Parameters:
      nValue - Input value
      Returns:
      the absolute value of the argument.
      Throws:
      IllegalArgumentException - if the input value is Integer.MIN_VALUE
    • abs

      @Nonnegative public static long abs(long nValue)
      This is a fix for Math.abs as it would return Long.MIN_VALUE for Long.MIN_VALUE which is very unexpected. Instead an exception is thrown.
      Parameters:
      nValue - Input value
      Returns:
      the absolute value of the argument.
      Throws:
      IllegalArgumentException - if the input value is Long.MIN_VALUE
    • abs

      @Nonnegative public static float abs(float fValue)
      This is a sanity method wrapping Math.abs (float), so that you don't have to think whether you need to invoke the abs method from this class or the one from Math directly.
      Parameters:
      fValue - Input value
      Returns:
      the absolute value of the argument.
    • abs

      @Nonnegative public static double abs(double dValue)
      This is a sanity method wrapping Math.abs (double), so that you don't have to think whether you need to invoke the abs method from this class or the one from Math directly.
      Parameters:
      dValue - Input value
      Returns:
      the absolute value of the argument.
    • hypot

      public static double hypot(double a, double b)
      Parameters:
      a - a
      b - b
      Returns:
      sqrt(a*a + b*b) without under/overflow.
    • getUnsignedInt

      public static long getUnsignedInt(int n)
      Converts the passed signed integer to an unsigned long
      Parameters:
      n - Source int
      Returns:
      The unsigned long
    • isExactlyOneBitSetToOne

      public static boolean isExactlyOneBitSetToOne(int n)
      Check if only a single bit is set.
      Source: http://stackoverflow.com/questions/12483843/test-if-a-bitboard-have-only-one-bit-set-to-1
      Say n has any bits set, the least significant is bit number k. Then n-1 has the same bits as n for indices above k, a 0-bit in place k and 1-bits in the less significant places, so the bitwise and removes the least significant set bit from n. If n had only one bit set, the result becomes 0, if n had more bits set, the result is nonzero.
      Parameters:
      n - Source number
      Returns:
      true if exactly one bit is set
    • isExactlyOneBitSetToOne

      public static boolean isExactlyOneBitSetToOne(long n)
      Check if only a single bit is set.
      Source: http://stackoverflow.com/questions/12483843/test-if-a-bitboard-have-only-one-bit-set-to-1
      Say n has any bits set, the least significant is bit number k. Then n-1 has the same bits as n for indices above k, a 0-bit in place k and 1-bits in the less significant places, so the bitwise and removes the least significant set bit from n. If n had only one bit set, the result becomes 0, if n had more bits set, the result is nonzero.
      Parameters:
      n - Source number
      Returns:
      true if exactly one bit is set