Package com.helger.base.numeric
Class MathHelper
java.lang.Object
com.helger.base.numeric.MathHelper
Contains several math help routines.
- Author:
- Philip Helger
-
Method Summary
Modifier and TypeMethodDescriptionstatic doubleabs(double dValue) This is a sanity method wrappingMath.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 floatabs(float fValue) This is a sanity method wrappingMath.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 intabs(int nValue) This is a fix forMath.absas it would returnInteger.MIN_VALUEforInteger.MIN_VALUEwhich is very unexpected.static longabs(long nValue) This is a fix forMath.absas it would returnLong.MIN_VALUEforLong.MIN_VALUEwhich is very unexpected.static booleancanConvertLongToInt(long nValue) static doublegetDividedDouble(int nDividend, int nDivisor) Divides the passed int dividend through the passed divisor (nDividend / nDivisor)static doublegetDividedDouble(long nDividend, long nDivisor) Divides the passed int dividend through the passed divisor (nDividend / nDivisor)static intgetIntDivided(int nDividend, int nDivisor, RoundingMode eRoundingMode) static intgetIntDividedCeil(int nDividend, int nDivisor) static intgetIntDividedFloor(int nDividend, int nDivisor) static intgetLongAsInt(long nValue, int nFallback) static longgetLongDivided(long nDividend, long nDivisor, RoundingMode eRoundingMode) static longgetLongDividedCeil(long nDividend, long nDivisor) static longgetLongDividedFloor(long nDividend, long nDivisor) static doublegetMaxDouble(double dValue, double... aValues) static doublegetMaxFloat(float fValue, float... aValues) static intgetMaxInt(int nValue, int... aValues) static longgetMaxLong(long nValue, long... aValues) static doublegetMinDouble(double dValue, double... aValues) static doublegetMinFloat(float fValue, float... aValues) static intgetMinInt(int nValue, int... aValues) static longgetMinLong(long nValue, long... aValues) static intgetRoundedUp(int nToRound, int nMultiple) Round up to the nearest multiple of the value to round.static longgetUnsignedInt(int n) Converts the passed signed integer to an unsigned longstatic doublehypot(double a, double b) static booleanisExactlyOneBitSetToOne(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.static booleanisExactlyOneBitSetToOne(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.
-
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 dividendnDivisor- the divisor- Returns:
- a double representing the exact quotient. Returns
Double.NaNif 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 dividendnDivisor- the divisor- Returns:
- a double representing the exact quotient. Returns
Double.NaNif the divisor is 0.
-
getIntDividedCeil
public static int getIntDividedCeil(int nDividend, int nDivisor) -
getIntDividedFloor
public static int getIntDividedFloor(int nDividend, int nDivisor) -
getIntDivided
-
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 forMath.absas it would returnInteger.MIN_VALUEforInteger.MIN_VALUEwhich 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 isInteger.MIN_VALUE
-
abs
@Nonnegative public static long abs(long nValue) This is a fix forMath.absas it would returnLong.MIN_VALUEforLong.MIN_VALUEwhich 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 isLong.MIN_VALUE
-
abs
@Nonnegative public static float abs(float fValue) This is a sanity method wrappingMath.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 wrappingMath.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- ab- 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:
trueif 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:
trueif exactly one bit is set
-