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 BigDecimal
PI
the value of PI
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
compare(BigDecimal n1, BigDecimal n2, int p)
Compare twoBigDecimal
s up to a precision.static boolean
equals(BigDecimal n1, BigDecimal n2, int precision)
Check if twoBigDecimal
s are equal up to a precision.static BigDecimal
exp(double x)
Compute ex.static BigDecimal
exp(double x, int scale)
Compute ex.static BigDecimal
exp(BigDecimal x)
Compute ex.static BigDecimal
exp(BigDecimal x, int scale)
Compute ex.static BigDecimal
getFractional(BigDecimal num)
Get the fractional part of a number.static BigDecimal
getWhole(BigDecimal num)
Get the integral part of a number (discarding the fractional part).static BigDecimal
log(BigDecimal x)
Compute log(x).static BigDecimal
log(BigDecimal x, int scale)
Compute log(x) up to a scale.static BigDecimal
pow(BigDecimal a, int n)
Compute a to the power of n, where n is an integer.static BigDecimal
pow(BigDecimal a, int n, int scale)
Compute a to the power of n, where n is an integer.static BigDecimal
pow(BigDecimal a, BigDecimal b)
Compute a to the power of b.static BigDecimal
pow(BigDecimal a, BigDecimal b, int scale)
Compute a to the power of b.static BigDecimal
sum(double... big)
Sum up big numbers.static BigDecimal
sum(BigDecimal... big)
Sum up theBigDecimal
numbers.
-
-
-
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 twoBigDecimal
s 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
- aBigDecimal
n2
- aBigDecimal
p
- the threshold is 1e-p- Returns:
- -1, 0, or 1 when
n1
is numerically less than, equal to, or greater thann2
, respectively
-
equals
public static boolean equals(BigDecimal n1, BigDecimal n2, int precision)
Check if twoBigDecimal
s are equal up to a precision.- Parameters:
n1
- aBigDecimal
n2
- aBigDecimal
precision
- the threshold is 1e-p- Returns:
true
if the numbers are equal up to a precision
-
sum
public static BigDecimal sum(BigDecimal... big)
Sum up theBigDecimal
numbers.- Parameters:
big
-BigDecimal
numbers- 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
-
-