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) Check if the passed long value can be safely converted to an int without losing information.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, @NonNull RoundingMode eRoundingMode) Divide two integers using the specified rounding mode.static intgetIntDividedCeil(int nDividend, int nDivisor) Divide two integers and round up (ceiling).static intgetIntDividedFloor(int nDividend, int nDivisor) Divide two integers and round down (floor).static intgetLongAsInt(long nValue, int nFallback) Convert a long to an int, returning a fallback value if the conversion would lose information.static longgetLongDivided(long nDividend, long nDivisor, @NonNull RoundingMode eRoundingMode) Divide two longs using the specified rounding mode.static longgetLongDividedCeil(long nDividend, long nDivisor) Divide two longs and round up (ceiling).static longgetLongDividedFloor(long nDividend, long nDivisor) Divide two longs and round down (floor).static doublegetMaxDouble(double dValue, double... aValues) Get the maximum of the passed double values.static doublegetMaxFloat(float fValue, float... aValues) Get the maximum of the passed float values.static intgetMaxInt(int nValue, int... aValues) Get the maximum of the passed int values.static longgetMaxLong(long nValue, long... aValues) Get the maximum of the passed long values.static doublegetMinDouble(double dValue, double... aValues) Get the minimum of the passed double values.static doublegetMinFloat(float fValue, float... aValues) Get the minimum of the passed float values.static intgetMinInt(int nValue, int... aValues) Get the minimum of the passed int values.static longgetMinLong(long nValue, long... aValues) Get the minimum of the passed long values.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) Divide two integers and round up (ceiling).- Parameters:
nDividend- the dividendnDivisor- 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 dividendnDivisor- the divisor- Returns:
- the result of the division rounded towards negative infinity.
-
getIntDivided
Divide two integers using the specified rounding mode.- Parameters:
nDividend- the dividendnDivisor- the divisoreRoundingMode- The rounding mode to use. May not benull.- 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 dividendnDivisor- 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 dividendnDivisor- 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 dividendnDivisor- the divisoreRoundingMode- The rounding mode to use. May not benull.- 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:
trueif the value fits in an int,falseotherwise.
-
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 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
-