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)
     
    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, RoundingMode eRoundingMode)
     
    static int
    getIntDividedCeil(int nDividend, int nDivisor)
     
    static int
    getIntDividedFloor(int nDividend, int nDivisor)
     
    static int
    getLongAsInt(long nValue, int nFallback)
     
    static long
    getLongDivided(long nDividend, long nDivisor, RoundingMode eRoundingMode)
     
    static long
    getLongDividedCeil(long nDividend, long nDivisor)
     
    static long
    getLongDividedFloor(long nDividend, long nDivisor)
     
    static double
    getMaxDouble(double dValue, double... aValues)
     
    static double
    getMaxFloat(float fValue, float... aValues)
     
    static int
    getMaxInt(int nValue, int... aValues)
     
    static long
    getMaxLong(long nValue, long... aValues)
     
    static double
    getMinDouble(double dValue, double... aValues)
     
    static double
    getMinFloat(float fValue, float... aValues)
     
    static int
    getMinInt(int nValue, int... aValues)
     
    static long
    getMinLong(long nValue, long... aValues)
     
    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)
    • getIntDividedFloor

      public static int getIntDividedFloor(int nDividend, int nDivisor)
    • getIntDivided

      public static int getIntDivided(int nDividend, int nDivisor, @Nonnull RoundingMode eRoundingMode)
    • getLongDividedCeil

      public static long getLongDividedCeil(long nDividend, long nDivisor)
    • getLongDividedFloor

      public static long getLongDividedFloor(long nDividend, long nDivisor)
    • getLongDivided

      public static long getLongDivided(long nDividend, long nDivisor, @Nonnull RoundingMode eRoundingMode)
    • canConvertLongToInt

      public static boolean canConvertLongToInt(long nValue)
    • getLongAsInt

      @CheckReturnValue public static int getLongAsInt(long nValue, int nFallback)
    • getMaxInt

      public static int getMaxInt(int nValue, @Nonnull int... aValues)
    • getMaxLong

      public static long getMaxLong(long nValue, @Nonnull long... aValues)
    • getMaxFloat

      public static double getMaxFloat(float fValue, @Nonnull float... aValues)
    • getMaxDouble

      public static double getMaxDouble(double dValue, @Nonnull double... aValues)
    • getMinInt

      public static int getMinInt(int nValue, @Nonnull int... aValues)
    • getMinLong

      public static long getMinLong(long nValue, @Nonnull long... aValues)
    • getMinFloat

      public static double getMinFloat(float fValue, @Nonnull float... aValues)
    • getMinDouble

      public static double getMinDouble(double dValue, @Nonnull double... aValues)
    • 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