Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (C) 2006-2014 phloc systems
* http://www.phloc.com
* office[at]phloc[dot]com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.phloc.commons.math;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import com.phloc.commons.CGlobal;
import com.phloc.commons.annotations.PresentForCodeCoverage;
import com.phloc.commons.equals.EqualsUtils;
/**
* Contains several math help routines.
*
* @author Philip Helger
*/
@Immutable
public final class MathHelper
{
private static final long LONG_HIGH_BITS = 0xFFFFFFFF80000000L;
@PresentForCodeCoverage
@SuppressWarnings ("unused")
private static final MathHelper s_aInstance = new MathHelper ();
private MathHelper ()
{}
/**
* Divides the passed int dividend through the passed divisor (nDividend /
* nDivisor)
*
* @param nDividend
* the dividend
* @param nDivisor
* the divisor
* @return a double representing the exact quotient. Returns
* {@link Double#NaN} if the divisor is 0.
*/
public static double getDividedDouble (final int nDividend, final int nDivisor)
{
final double dDividend = nDividend;
final double dDivisor = nDivisor;
return dDividend / dDivisor;
}
/**
* Get the division result using {@link BigDecimal}.
*
* @param nDividend
* the dividend
* @param nDivisor
* the divisor
* @return the result of the division
* @throws ArithmeticException
* if the divisor is 0.
*/
@Nonnull
public static BigDecimal getDividedBigDecimal (final int nDividend, final int nDivisor) throws ArithmeticException
{
final BigDecimal aDividend = BigDecimal.valueOf (nDividend);
final BigDecimal aDivisor = BigDecimal.valueOf (nDivisor);
return aDividend.divide (aDivisor);
}
public static int getIntDividedCeil (final int nDividend, final int nDivisor)
{
return getIntDivided (nDividend, nDivisor, RoundingMode.CEILING);
}
public static int getIntDividedFloor (final int nDividend, final int nDivisor)
{
return getIntDivided (nDividend, nDivisor, RoundingMode.FLOOR);
}
public static int getIntDivided (final int nDividend, final int nDivisor, @Nonnull final RoundingMode eRoundingMode)
{
return new BigDecimal (nDividend).divide (new BigDecimal (nDivisor), eRoundingMode).intValue ();
}
public static long getLongDividedCeil (final long nDividend, final long nDivisor)
{
return getLongDivided (nDividend, nDivisor, RoundingMode.CEILING);
}
public static long getLongDividedFloor (final long nDividend, final long nDivisor)
{
return getLongDivided (nDividend, nDivisor, RoundingMode.FLOOR);
}
public static long getLongDivided (final long nDividend,
final long nDivisor,
@Nonnull final RoundingMode eRoundingMode)
{
return new BigDecimal (nDividend).divide (new BigDecimal (nDivisor), eRoundingMode).longValue ();
}
public static boolean canConvertLongToInt (final long nValue)
{
return (nValue & LONG_HIGH_BITS) == 0 || (nValue & LONG_HIGH_BITS) == LONG_HIGH_BITS;
}
@CheckReturnValue
public static int getLongAsInt (final long nValue, final int nFallback)
{
return canConvertLongToInt (nValue) ? (int) nValue : nFallback;
}
public static int getMaxInt (final int nValue, @Nonnull final int... aValues)
{
int ret = nValue;
for (final int n : aValues)
ret = Math.max (ret, n);
return ret;
}
public static long getMaxLong (final long nValue, @Nonnull final long... aValues)
{
long ret = nValue;
for (final long n : aValues)
ret = Math.max (ret, n);
return ret;
}
public static double getMaxFloat (final float fValue, @Nonnull final float... aValues)
{
float ret = fValue;
for (final float f : aValues)
ret = Math.max (ret, f);
return ret;
}
public static double getMaxDouble (final double dValue, @Nonnull final double... aValues)
{
double ret = dValue;
for (final double d : aValues)
ret = Math.max (ret, d);
return ret;
}
@Nonnull
public static BigDecimal getMaxBigDecimal (@Nonnull final BigDecimal aValue, @Nonnull final BigDecimal... aValues)
{
BigDecimal ret = aValue;
for (final BigDecimal a : aValues)
if (a.compareTo (ret) > 0)
ret = a;
return ret;
}
@Nonnull
public static BigInteger getMaxBigInteger (@Nonnull final BigInteger aValue, @Nonnull final BigInteger... aValues)
{
BigInteger ret = aValue;
for (final BigInteger a : aValues)
if (a.compareTo (ret) > 0)
ret = a;
return ret;
}
public static int getMinInt (final int nValue, @Nonnull final int... aValues)
{
int ret = nValue;
for (final int n : aValues)
ret = Math.min (ret, n);
return ret;
}
public static long getMinLong (final long nValue, @Nonnull final long... aValues)
{
long ret = nValue;
for (final long n : aValues)
ret = Math.min (ret, n);
return ret;
}
public static double getMinFloat (final float fValue, @Nonnull final float... aValues)
{
float ret = fValue;
for (final float f : aValues)
ret = Math.min (ret, f);
return ret;
}
public static double getMinDouble (final double dValue, @Nonnull final double... aValues)
{
double ret = dValue;
for (final double d : aValues)
ret = Math.min (ret, d);
return ret;
}
@Nonnull
public static BigDecimal getMinBigDecimal (@Nonnull final BigDecimal aValue, @Nonnull final BigDecimal... aValues)
{
BigDecimal ret = aValue;
for (final BigDecimal a : aValues)
if (a.compareTo (ret) < 0)
ret = a;
return ret;
}
@Nonnull
public static BigInteger getMinBigInteger (@Nonnull final BigInteger aValue, @Nonnull final BigInteger... aValues)
{
BigInteger ret = aValue;
for (final BigInteger a : aValues)
if (a.compareTo (ret) < 0)
ret = a;
return ret;
}
/**
* This is a fix for Math.abs as it would return
* {@link Integer#MIN_VALUE} for {@link Integer#MIN_VALUE} which is very
* unexpected. Instead an exception is thrown.
*
* @param nValue
* Input value
* @return the absolute value of the argument.
* @throws IllegalArgumentException
* if the input value is {@link Integer#MIN_VALUE}
*/
@Nonnegative
public static int abs (final int nValue)
{
// As Integer.MIN_VALUE is -2^31 and Integer.MAX_VALUE is 2^31-1 it means
// that there is not integer value matching 2^31!!!
if (nValue == Integer.MIN_VALUE)
throw new IllegalArgumentException ("There is not absolute value for Integer.MIN_VALUE!");
return Math.abs (nValue);
}
/**
* This is a fix for Math.abs as it would return
* {@link Long#MIN_VALUE} for {@link Long#MIN_VALUE} which is very unexpected.
* Instead an exception is thrown.
*
* @param nValue
* Input value
* @return the absolute value of the argument.
* @throws IllegalArgumentException
* if the input value is {@link Long#MIN_VALUE}
*/
@Nonnegative
public static long abs (final long nValue)
{
// As Long.MIN_VALUE is -2^63 and Integer.MAX_VALUE is 2^63-1 it means
// that there is not integer value matching 2^63!!!
if (nValue == Long.MIN_VALUE)
throw new IllegalArgumentException ("There is not absolute value for Long.MIN_VALUE!");
return Math.abs (nValue);
}
/**
* 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.
*
* @param fValue
* Input value
* @return the absolute value of the argument.
*/
@Nonnegative
public static float abs (final float fValue)
{
return Math.abs (fValue);
}
/**
* 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.
*
* @param dValue
* Input value
* @return the absolute value of the argument.
*/
@Nonnegative
public static double abs (final double dValue)
{
return Math.abs (dValue);
}
/**
* This is a sanity method wrapping BigDecimal.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.
*
* @param aValue
* Input value
* @return the absolute value of the argument.
*/
@Nonnull
public static BigDecimal abs (@Nonnull final BigDecimal aValue)
{
return aValue.abs ();
}
/**
* This is a sanity method wrapping BigInteger.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.
*
* @param aValue
* Input value
* @return the absolute value of the argument.
*/
@Nonnull
public static BigInteger abs (@Nonnull final BigInteger aValue)
{
return aValue.abs ();
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is = 0.
*/
public static boolean isEqualToZero (@Nonnull final BigDecimal aValue)
{
return EqualsUtils.equals (aValue, BigDecimal.ZERO);
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is != 0.
*/
public static boolean isNotEqualToZero (@Nonnull final BigDecimal aValue)
{
return !EqualsUtils.equals (aValue, BigDecimal.ZERO);
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is < 0.
*/
public static boolean isLowerThanZero (@Nonnull final BigDecimal aValue)
{
return aValue.compareTo (BigDecimal.ZERO) < 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is ≤ 0.
*/
public static boolean isLowerOrEqualThanZero (@Nonnull final BigDecimal aValue)
{
return aValue.compareTo (BigDecimal.ZERO) <= 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is > 0.
*/
public static boolean isGreaterThanZero (@Nonnull final BigDecimal aValue)
{
return aValue.compareTo (BigDecimal.ZERO) > 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is ≥ 0.
*/
public static boolean isGreaterOrEqualThanZero (@Nonnull final BigDecimal aValue)
{
return aValue.compareTo (BigDecimal.ZERO) >= 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is = 1.
*/
public static boolean isEqualToOne (@Nonnull final BigDecimal aValue)
{
return EqualsUtils.equals (aValue, BigDecimal.ONE);
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is != 1.
*/
public static boolean isNotEqualToOne (@Nonnull final BigDecimal aValue)
{
return !EqualsUtils.equals (aValue, BigDecimal.ONE);
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is < 1.
*/
public static boolean isLowerThanOne (@Nonnull final BigDecimal aValue)
{
return aValue.compareTo (BigDecimal.ONE) < 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is ≤ 1.
*/
public static boolean isLowerOrEqualThanOne (@Nonnull final BigDecimal aValue)
{
return aValue.compareTo (BigDecimal.ONE) <= 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is > 1.
*/
public static boolean isGreaterThanOne (@Nonnull final BigDecimal aValue)
{
return aValue.compareTo (BigDecimal.ONE) > 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is ≥ 1.
*/
public static boolean isGreaterOrEqualThanOne (@Nonnull final BigDecimal aValue)
{
return aValue.compareTo (BigDecimal.ONE) >= 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is = 10.
*/
public static boolean isEqualToTen (@Nonnull final BigDecimal aValue)
{
return EqualsUtils.equals (aValue, BigDecimal.TEN);
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is != 10.
*/
public static boolean isNotEqualToTen (@Nonnull final BigDecimal aValue)
{
return !EqualsUtils.equals (aValue, BigDecimal.TEN);
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is < 10.
*/
public static boolean isLowerThanTen (@Nonnull final BigDecimal aValue)
{
return aValue.compareTo (BigDecimal.TEN) < 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is ≤ 10.
*/
public static boolean isLowerOrEqualThanTen (@Nonnull final BigDecimal aValue)
{
return aValue.compareTo (BigDecimal.TEN) <= 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is > 10.
*/
public static boolean isGreaterThanTen (@Nonnull final BigDecimal aValue)
{
return aValue.compareTo (BigDecimal.TEN) > 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is ≥ 10.
*/
public static boolean isGreaterOrEqualThanTen (@Nonnull final BigDecimal aValue)
{
return aValue.compareTo (BigDecimal.TEN) >= 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is = 100.
*/
public static boolean isEqualTo100 (@Nonnull final BigDecimal aValue)
{
return EqualsUtils.equals (aValue, CGlobal.BIGDEC_100);
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is != 100.
*/
public static boolean isNotEqualTo100 (@Nonnull final BigDecimal aValue)
{
return !EqualsUtils.equals (aValue, CGlobal.BIGDEC_100);
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is < 100.
*/
public static boolean isLowerThan100 (@Nonnull final BigDecimal aValue)
{
return aValue.compareTo (CGlobal.BIGDEC_100) < 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is ≤ 100.
*/
public static boolean isLowerOrEqualThan100 (@Nonnull final BigDecimal aValue)
{
return aValue.compareTo (CGlobal.BIGDEC_100) <= 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is > 100.
*/
public static boolean isGreaterThan100 (@Nonnull final BigDecimal aValue)
{
return aValue.compareTo (CGlobal.BIGDEC_100) > 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is ≥ 100.
*/
public static boolean isGreaterOrEqualThan100 (@Nonnull final BigDecimal aValue)
{
return aValue.compareTo (CGlobal.BIGDEC_100) >= 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is = 0.
*/
public static boolean isEqualToZero (@Nonnull final BigInteger aValue)
{
return EqualsUtils.equals (aValue, BigInteger.ZERO);
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is != 0.
*/
public static boolean isNotEqualToZero (@Nonnull final BigInteger aValue)
{
return !EqualsUtils.equals (aValue, BigInteger.ZERO);
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is < 0.
*/
public static boolean isLowerThanZero (@Nonnull final BigInteger aValue)
{
return aValue.compareTo (BigInteger.ZERO) < 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is ≤ 0.
*/
public static boolean isLowerOrEqualThanZero (@Nonnull final BigInteger aValue)
{
return aValue.compareTo (BigInteger.ZERO) <= 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is > 0.
*/
public static boolean isGreaterThanZero (@Nonnull final BigInteger aValue)
{
return aValue.compareTo (BigInteger.ZERO) > 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is ≥ 0.
*/
public static boolean isGreaterOrEqualThanZero (@Nonnull final BigInteger aValue)
{
return aValue.compareTo (BigInteger.ZERO) >= 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is = 1.
*/
public static boolean isEqualToOne (@Nonnull final BigInteger aValue)
{
return EqualsUtils.equals (aValue, BigInteger.ONE);
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is != 1.
*/
public static boolean isNotEqualToOne (@Nonnull final BigInteger aValue)
{
return !EqualsUtils.equals (aValue, BigInteger.ONE);
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is < 1.
*/
public static boolean isLowerThanOne (@Nonnull final BigInteger aValue)
{
return aValue.compareTo (BigInteger.ONE) < 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is ≤ 1.
*/
public static boolean isLowerOrEqualThanOne (@Nonnull final BigInteger aValue)
{
return aValue.compareTo (BigInteger.ONE) <= 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is > 1.
*/
public static boolean isGreaterThanOne (@Nonnull final BigInteger aValue)
{
return aValue.compareTo (BigInteger.ONE) > 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is ≥ 1.
*/
public static boolean isGreaterOrEqualThanOne (@Nonnull final BigInteger aValue)
{
return aValue.compareTo (BigInteger.ONE) >= 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is = 10.
*/
public static boolean isEqualToTen (@Nonnull final BigInteger aValue)
{
return EqualsUtils.equals (aValue, BigInteger.TEN);
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is != 10.
*/
public static boolean isNotEqualToTen (@Nonnull final BigInteger aValue)
{
return !EqualsUtils.equals (aValue, BigInteger.TEN);
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is < 10.
*/
public static boolean isLowerThanTen (@Nonnull final BigInteger aValue)
{
return aValue.compareTo (BigInteger.TEN) < 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is ≤ 10.
*/
public static boolean isLowerOrEqualThanTen (@Nonnull final BigInteger aValue)
{
return aValue.compareTo (BigInteger.TEN) <= 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is > 10.
*/
public static boolean isGreaterThanTen (@Nonnull final BigInteger aValue)
{
return aValue.compareTo (BigInteger.TEN) > 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is ≥ 10.
*/
public static boolean isGreaterOrEqualThanTen (@Nonnull final BigInteger aValue)
{
return aValue.compareTo (BigInteger.TEN) >= 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is = 100.
*/
public static boolean isEqualTo100 (@Nonnull final BigInteger aValue)
{
return EqualsUtils.equals (aValue, CGlobal.BIGINT_100);
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is != 100.
*/
public static boolean isNotEqualTo100 (@Nonnull final BigInteger aValue)
{
return !EqualsUtils.equals (aValue, CGlobal.BIGINT_100);
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is < 100.
*/
public static boolean isLowerThan100 (@Nonnull final BigInteger aValue)
{
return aValue.compareTo (CGlobal.BIGINT_100) < 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is ≤ 100.
*/
public static boolean isLowerOrEqualThan100 (@Nonnull final BigInteger aValue)
{
return aValue.compareTo (CGlobal.BIGINT_100) <= 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is > 100.
*/
public static boolean isGreaterThan100 (@Nonnull final BigInteger aValue)
{
return aValue.compareTo (CGlobal.BIGINT_100) > 0;
}
/**
* @param aValue
* Value to compare. May not be null.
* @return true if the value is ≥ 100.
*/
public static boolean isGreaterOrEqualThan100 (@Nonnull final BigInteger aValue)
{
return aValue.compareTo (CGlobal.BIGINT_100) >= 0;
}
/**
* Get the passed String as a BigDecimal without any trailing zeroes.
*
* @param sValue
* The String to be used as a BigDecimal to be modified. May be
* null.
* @return null if the input value is null.
*/
@Nullable
@CheckReturnValue
public static BigDecimal getWithoutTrailingZeroes (@Nullable final String sValue)
{
if (sValue == null)
return null;
return getWithoutTrailingZeroes (new BigDecimal (sValue));
}
/**
* 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
*
*
* @param aValue
* The BigDecimal to be modified. May be null.
* @return null if the input value is null.
*/
@Nullable
@CheckReturnValue
public static BigDecimal getWithoutTrailingZeroes (@Nullable final BigDecimal aValue)
{
if (aValue == null)
return null;
// stripTrailingZeros does not work for "0"!
if (BigDecimal.ZERO.compareTo (aValue) == 0)
return BigDecimal.ZERO;
final BigDecimal ret = aValue.stripTrailingZeros ();
// Avoid stuff like "6E2"
return ret.scale () >= 0 ? ret : ret.setScale (0);
}
/**
* 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
*
*
* @param aBD
* The BigDecimal to check. May not be null.
* @return The minimum number of fraction digits. Always ≥ 0.
*/
@Nonnegative
public static int getFractionDigits (@Nonnull final BigDecimal aBD)
{
return getWithoutTrailingZeroes (aBD).scale ();
}
/**
* Add x% to base
*
* @param aBase
* Base value. May not be null.
* @param aPercentage
* Percentage value (0-100). May not be null.
* @return base + x% (=aBase * (100 + perc) / 100). Never
* null.
*/
@Nonnull
public static BigDecimal addPercent (@Nonnull final BigDecimal aBase, @Nonnull final BigDecimal aPercentage)
{
return aBase.multiply (CGlobal.BIGDEC_100.add (aPercentage)).divide (CGlobal.BIGDEC_100);
}
/**
* Subtract x% from base
*
* @param aBase
* Base value. May not be null.
* @param aPercentage
* Percentage value (0-100). May not be null.
* @return base - x% (=aBase * (100 - perc) / 100). Never
* null.
*/
@Nonnull
public static BigDecimal subtractPercent (@Nonnull final BigDecimal aBase, @Nonnull final BigDecimal aPercentage)
{
return aBase.multiply (CGlobal.BIGDEC_100.subtract (aPercentage)).divide (CGlobal.BIGDEC_100);
}
/**
* Get x% from base
*
* @param aBase
* Base value. May not be null.
* @param aPercentage
* Percentage value (0-100). May not be null.
* @return x% from base (=aBase * perc / 100). Never
* null.
*/
@Nonnull
public static BigDecimal getPercentValue (@Nonnull final BigDecimal aBase, @Nonnull final BigDecimal aPercentage)
{
return aBase.multiply (aPercentage).divide (CGlobal.BIGDEC_100);
}
/**
* Get x% from base with rounding etc.
*
* @param aBase
* Base value. May not be null.
* @param aPercentage
* Percentage value (0-100). May not be null.
* @param nScale
* Maximum scale to achieve.
* @param eRoundingMode
* Rounding mode to used. May not be null.
* @return x% from base (=aBase * perc / 100). Never
* null.
*/
@Nonnull
public static BigDecimal getPercentValue (@Nonnull final BigDecimal aBase,
@Nonnull final BigDecimal aPercentage,
@Nonnegative final int nScale,
@Nonnull final RoundingMode eRoundingMode)
{
return aBase.multiply (aPercentage).divide (CGlobal.BIGDEC_100, nScale, eRoundingMode);
}
/**
* @param a
* a
* @param b
* b
* @return sqrt(a*a + b*b) without under/overflow.
*/
public static double hypot (final double a, final double b)
{
if (a == 0)
return b;
if (b == 0)
return a;
double r;
final double dAbsA = abs (a);
final double dAbsB = abs (b);
if (dAbsA > dAbsB)
{
r = b / a;
r = dAbsA * Math.sqrt (1 + r * r);
}
else
{
r = a / b;
r = dAbsB * Math.sqrt (1 + r * r);
}
return r;
}
/**
* Converts the passed signed integer to an unsigned long
*
* @param a
* Source int
* @return The unsigned long
*/
public static long getUnsignedInt (final int a)
{
return a & 0x00000000ffffffffL;
}
}