Package com.helger.commons.math
Class MathHelper
java.lang.Object
com.helger.commons.math.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 BigDecimalabs(BigDecimal aValue) This is a sanity method wrappingBigDecimal.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 BigDecimal directly.static BigIntegerabs(BigInteger aValue) This is a sanity method wrappingBigInteger.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 BigInteger directly.static BigDecimaladdPercent(BigDecimal aBase, BigDecimal aPercentage) Add x% to basestatic BigDecimaladdPercent(BigDecimal aBase, BigDecimal aPercentage, int nScale, RoundingMode eRoundingMode) Add x% to basestatic booleancanConvertLongToInt(long nValue) static BigDecimalgetDividedBigDecimal(long nDividend, long nDivisor) Get the division result usingBigDecimal.static BigDecimalgetDividedBigDecimal(long nDividend, long nDivisor, int nScale, RoundingMode eRoundingMode) Get the division result usingBigDecimal.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 intGet the number of effective fraction digits by the specified BigDecimal.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 BigDecimalgetMaxBigDecimal(BigDecimal aValue, BigDecimal... aValues) static BigIntegergetMaxBigInteger(BigInteger aValue, BigInteger... aValues) 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 BigDecimalgetMinBigDecimal(BigDecimal aValue, BigDecimal... aValues) static BigIntegergetMinBigInteger(BigInteger aValue, BigInteger... 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 BigDecimalgetPercentValue(BigDecimal aBase, BigDecimal aPercentage) Get x% from basestatic BigDecimalgetPercentValue(BigDecimal aBase, BigDecimal aPercentage, int nScale, RoundingMode eRoundingMode) Get x% from base with rounding etc.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 BigDecimalgetWithoutTrailingZeroes(String sValue) Get the passed String as a BigDecimal without any trailing zeroes.static BigDecimalgetWithoutTrailingZeroes(BigDecimal aValue) Get the passed BigDecimal without any trailing zeroes.static doublehypot(double a, double b) static booleanisEQ0(BigDecimal aValue) static booleanisEQ0(BigInteger aValue) static booleanisEQ1(BigDecimal aValue) static booleanisEQ1(BigInteger aValue) static booleanisEQ10(BigDecimal aValue) static booleanisEQ10(BigInteger aValue) static booleanisEQ100(BigDecimal aValue) static booleanisEQ100(BigInteger aValue) 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.static booleanisGE0(BigDecimal aValue) static booleanisGE0(BigInteger aValue) static booleanisGE1(BigDecimal aValue) static booleanisGE1(BigInteger aValue) static booleanisGE10(BigDecimal aValue) static booleanisGE10(BigInteger aValue) static booleanisGE100(BigDecimal aValue) static booleanisGE100(BigInteger aValue) static booleanisGT0(BigDecimal aValue) static booleanisGT0(BigInteger aValue) static booleanisGT1(BigDecimal aValue) static booleanisGT1(BigInteger aValue) static booleanisGT10(BigDecimal aValue) static booleanisGT10(BigInteger aValue) static booleanisGT100(BigDecimal aValue) static booleanisGT100(BigInteger aValue) static booleanisLE0(BigDecimal aValue) static booleanisLE0(BigInteger aValue) static booleanisLE1(BigDecimal aValue) static booleanisLE1(BigInteger aValue) static booleanisLE10(BigDecimal aValue) static booleanisLE10(BigInteger aValue) static booleanisLE100(BigDecimal aValue) static booleanisLE100(BigInteger aValue) static booleanisLT0(BigDecimal aValue) static booleanisLT0(BigInteger aValue) static booleanisLT1(BigDecimal aValue) static booleanisLT1(BigInteger aValue) static booleanisLT10(BigDecimal aValue) static booleanisLT10(BigInteger aValue) static booleanisLT100(BigDecimal aValue) static booleanisLT100(BigInteger aValue) static booleanisNE0(BigDecimal aValue) static booleanisNE0(BigInteger aValue) static booleanisNE1(BigDecimal aValue) static booleanisNE1(BigInteger aValue) static booleanisNE10(BigDecimal aValue) static booleanisNE10(BigInteger aValue) static booleanisNE100(BigDecimal aValue) static booleanisNE100(BigInteger aValue) static BigDecimalsubtractPercent(BigDecimal aBase, BigDecimal aPercentage) Subtract x% from basestatic BigDecimalsubtractPercent(BigDecimal aBase, BigDecimal aPercentage, int nScale, RoundingMode eRoundingMode) Subtract x% from basestatic BigDecimaltoBigDecimal(double d) static BigDecimaltoBigDecimal(float f) static BigDecimaltoBigDecimal(int n) static BigDecimaltoBigDecimal(long n) static BigDecimaltoBigDecimal(Number aNumber) static BigDecimaltoBigDecimal(String sNumber) static BigIntegertoBigInteger(int n) static BigIntegertoBigInteger(long n) static BigIntegertoBigInteger(Number aNumber) static BigIntegertoBigInteger(String sNumber)
-
Method Details
-
getRoundedUp
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.
-
getDividedBigDecimal
Get the division result usingBigDecimal.- Parameters:
nDividend- the dividendnDivisor- the divisor- Returns:
- the result of the division
- Throws:
ArithmeticException- if the divisor is 0.
-
getDividedBigDecimal
@Nonnull public static BigDecimal getDividedBigDecimal(long nDividend, long nDivisor, @Nonnegative int nScale, @Nonnull RoundingMode eRoundingMode) Get the division result usingBigDecimal.- Parameters:
nDividend- the dividendnDivisor- the divisornScale- Number of fraction digits. Must be ≥ 0.eRoundingMode- Round mode to be used. May not benull.- Returns:
- the result of the division
- Throws:
ArithmeticException- if the divisor is 0.- Since:
- v11.0.2
-
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
-
getMaxInt
-
getMaxLong
-
getMaxFloat
-
getMaxDouble
-
getMaxBigDecimal
@Nonnull public static BigDecimal getMaxBigDecimal(@Nonnull BigDecimal aValue, @Nonnull BigDecimal... aValues) -
getMaxBigInteger
@Nonnull public static BigInteger getMaxBigInteger(@Nonnull BigInteger aValue, @Nonnull BigInteger... aValues) -
getMinInt
-
getMinLong
-
getMinFloat
-
getMinDouble
-
getMinBigDecimal
@Nonnull public static BigDecimal getMinBigDecimal(@Nonnull BigDecimal aValue, @Nonnull BigDecimal... aValues) -
getMinBigInteger
@Nonnull public static BigInteger getMinBigInteger(@Nonnull BigInteger aValue, @Nonnull BigInteger... aValues) -
abs
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
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
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
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.
-
abs
This is a sanity method wrappingBigDecimal.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 BigDecimal directly.- Parameters:
aValue- Input value- Returns:
- the absolute value of the argument.
-
abs
This is a sanity method wrappingBigInteger.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 BigInteger directly.- Parameters:
aValue- Input value- Returns:
- the absolute value of the argument.
-
isEQ0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is = 0.
-
isNE0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is != 0.
-
isLT0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is < 0.
-
isLE0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≤ 0.
-
isGT0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is > 0.
-
isGE0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≥ 0.
-
isEQ1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is = 1.
-
isNE1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is != 1.
-
isLT1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is < 1.
-
isLE1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≤ 1.
-
isGT1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is > 1.
-
isGE1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≥ 1.
-
isEQ10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is = 10.
-
isNE10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is != 10.
-
isLT10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is < 10.
-
isLE10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≤ 10.
-
isGT10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is > 10.
-
isGE10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≥ 10.
-
isEQ100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is = 100.
-
isNE100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is != 100.
-
isLT100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is < 100.
-
isLE100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≤ 100.
-
isGT100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is > 100.
-
isGE100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≥ 100.
-
isEQ0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is = 0.
-
isNE0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is != 0.
-
isLT0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is < 0.
-
isLE0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≤ 0.
-
isGT0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is > 0.
-
isGE0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≥ 0.
-
isEQ1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is = 1.
-
isNE1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is != 1.
-
isLT1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is < 1.
-
isLE1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≤ 1.
-
isGT1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is > 1.
-
isGE1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≥ 1.
-
isEQ10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is = 10.
-
isNE10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is != 10.
-
isLT10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is < 10.
-
isLE10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≤ 10.
-
isGT10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is > 10.
-
isGE10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≥ 10.
-
isEQ100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is = 100.
-
isNE100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is != 100.
-
isLT100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is < 100.
-
isLE100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≤ 100.
-
isGT100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is > 100.
-
isGE100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≥ 100.
-
getWithoutTrailingZeroes
@Nullable @CheckReturnValue public static BigDecimal getWithoutTrailingZeroes(@Nullable String sValue) Get the passed String as a BigDecimal without any trailing zeroes.- Parameters:
sValue- The String to be used as a BigDecimal to be modified. May benull.- Returns:
nullif the input value isnull.
-
getWithoutTrailingZeroes
@Nullable @CheckReturnValue public static BigDecimal getWithoutTrailingZeroes(@Nullable BigDecimal aValue) Get the passed BigDecimal without any trailing zeroes. Examples:- new BigDecimal ("0.00000000") --> 0
- new BigDecimal ("10") --> 10
- new BigDecimal ("10.00000000") --> 10
- new BigDecimal ("10.1") --> 10.1
- new BigDecimal ("10.10000000") --> 10.1
- new BigDecimal ("10.345") --> 10.345
- new BigDecimal ("10.3450000000") --> 10.345
- Parameters:
aValue- The BigDecimal to be modified. May benull.- Returns:
nullif the input value isnull.
-
getFractionDigits
Get the number of effective fraction digits by the specified BigDecimal. Examples:- new BigDecimal ("10") --> 0
- new BigDecimal ("10.00000000") --> 0
- new BigDecimal ("10.1") --> 1
- new BigDecimal ("10.10000000") --> 1
- new BigDecimal ("10.345") --> 3
- new BigDecimal ("10.3450000000") --> 3
- Parameters:
aBD- The BigDecimal to check. May not benull.- Returns:
- The minimum number of fraction digits. Always ≥ 0.
-
addPercent
@Nonnull public static BigDecimal addPercent(@Nonnull BigDecimal aBase, @Nonnull BigDecimal aPercentage) Add x% to base- Parameters:
aBase- Base value. May not benull.aPercentage- Percentage value (0-100). May not benull.- Returns:
- base + x% (
=aBase * (100 + perc) / 100). Nevernull.
-
addPercent
@Nonnull public static BigDecimal addPercent(@Nonnull BigDecimal aBase, @Nonnull BigDecimal aPercentage, @Nonnegative int nScale, @Nonnull RoundingMode eRoundingMode) Add x% to base- Parameters:
aBase- Base value. May not benull.aPercentage- Percentage value (0-100). May not benull.nScale- Maximum scale to achieve.eRoundingMode- Rounding mode to used. May not benull.- Returns:
- base + x% (
=aBase * (100 + perc) / 100). Nevernull.
-
subtractPercent
@Nonnull public static BigDecimal subtractPercent(@Nonnull BigDecimal aBase, @Nonnull BigDecimal aPercentage) Subtract x% from base- Parameters:
aBase- Base value. May not benull.aPercentage- Percentage value (0-100). May not benull.- Returns:
- base - x% (
=aBase * (100 - perc) / 100). Nevernull.
-
subtractPercent
@Nonnull public static BigDecimal subtractPercent(@Nonnull BigDecimal aBase, @Nonnull BigDecimal aPercentage, @Nonnegative int nScale, @Nonnull RoundingMode eRoundingMode) Subtract x% from base- Parameters:
aBase- Base value. May not benull.aPercentage- Percentage value (0-100). May not benull.nScale- Maximum scale to achieve.eRoundingMode- Rounding mode to used. May not benull.- Returns:
- base - x% (
=aBase * (100 - perc) / 100). Nevernull.
-
getPercentValue
@Nonnull public static BigDecimal getPercentValue(@Nonnull BigDecimal aBase, @Nonnull BigDecimal aPercentage) Get x% from base- Parameters:
aBase- Base value. May not benull.aPercentage- Percentage value (0-100). May not benull.- Returns:
- x% from base (
=aBase * perc / 100). Nevernull.
-
getPercentValue
@Nonnull public static BigDecimal getPercentValue(@Nonnull BigDecimal aBase, @Nonnull BigDecimal aPercentage, @Nonnegative int nScale, @Nonnull RoundingMode eRoundingMode) Get x% from base with rounding etc.- Parameters:
aBase- Base value. May not benull.aPercentage- Percentage value (0-100). May not benull.nScale- Maximum scale to achieve.eRoundingMode- Rounding mode to used. May not benull.- Returns:
- x% from base (
=aBase * perc / 100). Nevernull.
-
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
-
toBigDecimal
-
toBigDecimal
-
toBigDecimal
-
toBigDecimal
-
toBigDecimal
-
toBigDecimal
-
toBigInteger
-
toBigInteger
-
toBigInteger
-
toBigInteger
-
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
-