Package dev.nm.number.big
Class BigDecimalUtils
- java.lang.Object
-
- dev.nm.number.big.BigDecimalUtils
-
public final class BigDecimalUtils extends Object
These are the utility functions to manipulateBigDecimal.- See Also:
- Class BigDecimal
-
-
Field Summary
Fields Modifier and Type Field Description static BigDecimalPIthe value of PI
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intcompare(BigDecimal n1, BigDecimal n2, int p)Compare twoBigDecimals up to a precision.static booleanequals(BigDecimal n1, BigDecimal n2, int precision)Check if twoBigDecimals are equal up to a precision.static BigDecimalexp(double x)Compute ex.static BigDecimalexp(double x, int scale)Compute ex.static BigDecimalexp(BigDecimal x)Compute ex.static BigDecimalexp(BigDecimal x, int scale)Compute ex.static BigDecimalgetFractional(BigDecimal num)Get the fractional part of a number.static BigDecimalgetWhole(BigDecimal num)Get the integral part of a number (discarding the fractional part).static BigDecimallog(BigDecimal x)Compute log(x).static BigDecimallog(BigDecimal x, int scale)Compute log(x) up to a scale.static BigDecimalpow(BigDecimal a, int n)Compute a to the power of n, where n is an integer.static BigDecimalpow(BigDecimal a, int n, int scale)Compute a to the power of n, where n is an integer.static BigDecimalpow(BigDecimal a, BigDecimal b)Compute a to the power of b.static BigDecimalpow(BigDecimal a, BigDecimal b, int scale)Compute a to the power of b.static BigDecimalsum(double... big)Sum up big numbers.static BigDecimalsum(BigDecimal... big)Sum up theBigDecimalnumbers.
-
-
-
Field Detail
-
PI
public static final BigDecimal PI
the value of PI- See Also:
- Joy of PI
-
-
Method Detail
-
compare
public static int compare(BigDecimal n1, BigDecimal n2, int p)
Compare twoBigDecimals up to a precision. In other words, if the absolute difference between the two numbers falls below a threshold, they are considered equal.- Parameters:
n1- aBigDecimaln2- aBigDecimalp- the threshold is 1e-p- Returns:
- -1, 0, or 1 when
n1is numerically less than, equal to, or greater thann2, respectively
-
equals
public static boolean equals(BigDecimal n1, BigDecimal n2, int precision)
Check if twoBigDecimals are equal up to a precision.- Parameters:
n1- aBigDecimaln2- aBigDecimalprecision- the threshold is 1e-p- Returns:
trueif the numbers are equal up to a precision
-
sum
public static BigDecimal sum(BigDecimal... big)
Sum up theBigDecimalnumbers.- Parameters:
big-BigDecimalnumbers- Returns:
- the sum
-
sum
public static BigDecimal sum(double... big)
Sum up big numbers.- Parameters:
big- numbers- Returns:
- the sum
-
getWhole
public static BigDecimal getWhole(BigDecimal num)
Get the integral part of a number (discarding the fractional part).- Parameters:
num- aBigDecimal- Returns:
- the integral part of the number
-
getFractional
public static BigDecimal getFractional(BigDecimal num)
Get the fractional part of a number. This is the same as the number subtracting the whole part. For a -ve. number, the fractional part is also -ve. For example, for -3.1415, the whole is -3 and the fractional part is -0.1415.- Parameters:
num- aBigDecimal- Returns:
- the fractional part of the number
-
pow
public static BigDecimal pow(BigDecimal a, BigDecimal b)
Compute a to the power of b.- Parameters:
a- a baseb- an exponent- Returns:
- ab
-
pow
public static BigDecimal pow(BigDecimal a, BigDecimal b, int scale)
Compute a to the power of b.- Parameters:
a- a baseb- an exponentscale- a precision parameter as inBigDecimal- Returns:
- ab
-
pow
public static BigDecimal pow(BigDecimal a, int n)
Compute a to the power of n, where n is an integer.- Parameters:
a- a basen- an integer exponent- Returns:
- an
-
pow
public static BigDecimal pow(BigDecimal a, int n, int scale)
Compute a to the power of n, where n is an integer. This is simply a wrapper aroundBigDecimal.pow(int)but handles also negative exponents. UseBigDecimal.pow(int)for arbitrary precision if the exponent is positive.- Parameters:
a- a basen- an exponentscale- a precision parameter as inBigDecimal- Returns:
- an
-
log
public static BigDecimal log(BigDecimal x)
Compute log(x). The base is e, hence the natural log.- Parameters:
x- a number- Returns:
- log(x)
- See Also:
- Wikipedia: Natural logarithm
-
log
public static BigDecimal log(BigDecimal x, int scale)
Compute log(x) up to a scale. The base is e, hence the natural log.- Parameters:
x- a numberscale- a precision parameter as inBigDecimal- Returns:
- log(x)
- See Also:
- Wikipedia: Natural logarithm
-
exp
public static BigDecimal exp(double x)
Compute ex.- Parameters:
x- the exponent- Returns:
- ex
- See Also:
- Wikipedia: Exponential function
-
exp
public static BigDecimal exp(double x, int scale)
Compute ex.- Parameters:
x- the exponentscale- a precision parameter as inBigDecimal- Returns:
- ex
- See Also:
- Wikipedia: Exponential function
-
exp
public static BigDecimal exp(BigDecimal x)
Compute ex.- Parameters:
x- the exponent- Returns:
- ex
- See Also:
- Wikipedia: Exponential function
-
exp
public static BigDecimal exp(BigDecimal x, int scale)
Compute ex.- Parameters:
x- the exponentscale- a precision parameter as inBigDecimal- Returns:
- ex
- See Also:
- Wikipedia: Exponential function
-
-