# SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.number.big

## Class BigDecimalUtils

• ### Field Summary

Fields
Modifier and Type Field and Description
static BigDecimal PI
the value of PI
• ### Method Summary

All Methods
Modifier and Type Method and Description
static int compare(BigDecimal n1, BigDecimal n2, int p)
Compare two BigDecimals up to a precision.
static boolean equals(BigDecimal n1, BigDecimal n2, int precision)
Check if two BigDecimals are equal up to a precision.
static BigDecimal exp(BigDecimal x)
Compute ex.
static BigDecimal exp(BigDecimal x, int scale)
Compute ex.
static BigDecimal exp(double x)
Compute ex.
static BigDecimal exp(double 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, 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 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 sum(BigDecimal... big)
Sum up the BigDecimal numbers.
static BigDecimal sum(double... big)
Sum up big numbers.
• ### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
• ### Field Detail

• #### PI

public static final BigDecimal PI
the value of PI
Joy of PI
• ### Method Detail

• #### compare

public static int compare(BigDecimal n1,
BigDecimal n2,
int p)
Compare two BigDecimals 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 - a BigDecimal
n2 - a BigDecimal
p - the threshold is 1e-p
Returns:
-1, 0, or 1 when n1 is numerically less than, equal to, or greater than n2, respectively
• #### equals

public static boolean equals(BigDecimal n1,
BigDecimal n2,
int precision)
Check if two BigDecimals are equal up to a precision.
Parameters:
n1 - a BigDecimal
n2 - a BigDecimal
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 the BigDecimal 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 - a BigDecimal
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 - a BigDecimal
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 base
b - 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 base
b - an exponent
scale - a precision parameter as in BigDecimal
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 base
n - 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 around BigDecimal.pow(int) but handles also negative exponents. Use BigDecimal.pow(int) for arbitrary precision if the exponent is positive.
Parameters:
a - a base
n - an exponent
scale - a precision parameter as in BigDecimal
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)
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 number
scale - a precision parameter as in BigDecimal
Returns:
log(x)
Wikipedia: Natural logarithm
• #### exp

public static BigDecimal exp(double x)
Compute ex.
Parameters:
x - the exponent
Returns:
ex
Wikipedia: Exponential function
• #### exp

public static BigDecimal exp(double x,
int scale)
Compute ex.
Parameters:
x - the exponent
scale - a precision parameter as in BigDecimal
Returns:
ex
Wikipedia: Exponential function
• #### exp

public static BigDecimal exp(BigDecimal x)
Compute ex.
Parameters:
x - the exponent
Returns:
ex
Wikipedia: Exponential function
• #### exp

public static BigDecimal exp(BigDecimal x,
int scale)
Compute ex.
Parameters:
x - the exponent
scale - a precision parameter as in BigDecimal
Returns:
ex