Class BigDecimalMath
BigDecimals.
Taken from @obermuhlner's Github
I do not claim ownership of this code, it is the intellectual property of @obermuhlner.-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic BigDecimalacos(BigDecimal x, MathContext mathContext) Calculates the arc cosine (inverted cosine) ofBigDecimalx.static BigDecimalasin(BigDecimal x, MathContext mathContext) Calculates the arc sine (inverted sine) ofBigDecimalx.static BigDecimalatan(BigDecimal x, MathContext mathContext) Calculates the arc tangens (inverted tangens) ofBigDecimalx.static BigDecimalcos(BigDecimal x, MathContext mathContext) Calculates the cosine (cosinus) ofBigDecimalx.static BigDecimale(MathContext mathContext) Returns the number e.static BigDecimalexp(BigDecimal x, MathContext mathContext) Calculates the natural exponent ofBigDecimalx (ex).static intexponent(BigDecimal value) Returns the exponent of the specifiedBigDecimalwritten as mantissa * 10exponent.static BigDecimalfactorial(int n) Calculates the factorial of the specified integer argument.static BigDecimalfactorial(BigDecimal x, MathContext mathContext) Calculates the factorial of the specifiedBigDecimal.static BigDecimalfractionalPart(BigDecimal value) Returns the fractional part of the specifiedBigDecimal(right of the decimal point).static BigDecimalstatic BigIntegerstatic BigDecimalintegralPart(BigDecimal value) Returns the integral part of the specifiedBigDecimal(left of the decimal point).static booleanisDoubleValue(BigDecimal value) Returns whether the specifiedBigDecimalvalue can be represented asdouble.static booleanisIntValue(BigDecimal value) Returns whether the specifiedBigDecimalvalue can be represented asint.static BigDecimallog(BigDecimal x, MathContext mathContext) Calculates the natural logarithm ofBigDecimalx.static BigDecimallog10(BigDecimal x, MathContext mathContext) Calculates the logarithm ofBigDecimalx to the base 10.static BigDecimallog2(BigDecimal x, MathContext mathContext) Calculates the logarithm ofBigDecimalx to the base 2.static BigDecimalmantissa(BigDecimal value) Returns the mantissa of the specifiedBigDecimalwritten as mantissa * 10exponent.static BigDecimalpi(MathContext mathContext) Returns the number pi.static BigDecimalpow(BigDecimal x, long y, MathContext mathContext) CalculatesBigDecimalx to the power oflongy (xy).static BigDecimalpow(BigDecimal x, BigDecimal y, MathContext mathContext) CalculatesBigDecimalx to the power ofBigDecimaly (xy).static BigDecimalreciprocal(BigDecimal x, MathContext mathContext) Calculates the reciprocal of the specifiedBigDecimal.static BigDecimalround(BigDecimal value, MathContext mathContext) Rounds the specifiedBigDecimalto the precision of the specifiedMathContext.static BigDecimalsin(BigDecimal x, MathContext mathContext) Calculates the sine (sinus) ofBigDecimalx.static BigDecimalsqrt(BigDecimal x, MathContext mathContext) Calculates the square root ofBigDecimalx.static BigDecimaltan(BigDecimal x, MathContext mathContext) Calculates the tangens ofBigDecimalx.
-
Field Details
-
DEFAULT_ROUNDING_MODE
-
DEFAULT_CONTEXT
-
-
Method Details
-
isIntValue
Returns whether the specifiedBigDecimalvalue can be represented asint.If this returns
trueyou can callBigDecimal.intValueExact()without fear of anArithmeticException.- Parameters:
value- theBigDecimalto check- Returns:
trueif the value can be represented asintvalue
-
isDoubleValue
Returns whether the specifiedBigDecimalvalue can be represented asdouble.If this returns
trueyou can callBigDecimal.doubleValue()without fear of gettingDouble.POSITIVE_INFINITYorDouble.NEGATIVE_INFINITYas result.Example:
BigDecimalMath.isDoubleValue(new BigDecimal("1E309"))returnsfalse, becausenew BigDecimal("1E309").doubleValue()returnsInfinity.Note: This method does not check for possible loss of precision.
For example
BigDecimalMath.isDoubleValue(new BigDecimal("1.23400000000000000000000000000000001"))will returntrue, becausenew BigDecimal("1.23400000000000000000000000000000001").doubleValue()returns a valid double value, although it loses precision and returns1.234.BigDecimalMath.isDoubleValue(new BigDecimal("1E-325"))will returntruealthough this value is smaller thanDouble.MIN_VALUE(and therefore outside the range of values that can be represented asdouble) becausenew BigDecimal("1E-325").doubleValue()returns0which is a legal value with loss of precision.- Parameters:
value- theBigDecimalto check- Returns:
trueif the value can be represented asdoublevalue
-
mantissa
Returns the mantissa of the specifiedBigDecimalwritten as mantissa * 10exponent.The mantissa is defined as having exactly 1 digit before the decimal point.
- Parameters:
value- theBigDecimal- Returns:
- the mantissa
- See Also:
-
exponent
Returns the exponent of the specifiedBigDecimalwritten as mantissa * 10exponent.The mantissa is defined as having exactly 1 digit before the decimal point.
- Parameters:
value- theBigDecimal- Returns:
- the exponent
- See Also:
-
integralPart
Returns the integral part of the specifiedBigDecimal(left of the decimal point).- Parameters:
value- theBigDecimal- Returns:
- the integral part
- See Also:
-
fractionalPart
Returns the fractional part of the specifiedBigDecimal(right of the decimal point).- Parameters:
value- theBigDecimal- Returns:
- the fractional part
- See Also:
-
round
Rounds the specifiedBigDecimalto the precision of the specifiedMathContext.This method calls
BigDecimal.round(MathContext).- Parameters:
value- theBigDecimalto roundmathContext- theMathContextused for the result- Returns:
- the rounded
BigDecimalvalue - See Also:
-
reciprocal
Calculates the reciprocal of the specifiedBigDecimal.- Parameters:
x- theBigDecimalmathContext- theMathContextused for the result- Returns:
- the reciprocal
BigDecimal - Throws:
ArithmeticException- if x = 0ArithmeticException- if the result is inexact but the rounding mode isUNNECESSARYormc.precision == 0and the quotient has a non-terminating decimal expansion.
-
factorial
Calculates the factorial of the specified integer argument.factorial = 1 * 2 * 3 * ... n
- Parameters:
n- theBigDecimal- Returns:
- the factorial
BigDecimal - Throws:
ArithmeticException- if x < 0
-
factorial
Calculates the factorial of the specifiedBigDecimal.This implementation uses Spouge's approximation to calculate the factorial for non-integer values.
This involves calculating a series of constants that depend on the desired precision. Since this constant calculation is quite expensive (especially for higher precisions), the constants for a specific precision will be cached and subsequent calls to this method with the same precision will be much faster.
It is therefore recommended to do one call to this method with the standard precision of your application during the startup phase and to avoid calling it with many different precisions.
See: Wikipedia: Factorial - Extension of factorial to non-integer values of argument
- Parameters:
x- theBigDecimalmathContext- theMathContextused for the result- Returns:
- the factorial
BigDecimal - Throws:
ArithmeticException- if x is a negative integer value (-1, -2, -3, ...)UnsupportedOperationException- if x is a non-integer value and theMathContexthas unlimited precision- See Also:
-
pow
CalculatesBigDecimalx to the power ofBigDecimaly (xy).- Parameters:
x- theBigDecimalvalue to take to the powery- theBigDecimalvalue to serve as exponentmathContext- theMathContextused for the result- Returns:
- the calculated x to the power of y with the precision specified in the
mathContext - Throws:
UnsupportedOperationException- if theMathContexthas unlimited precision- See Also:
-
pow
CalculatesBigDecimalx to the power oflongy (xy).The implementation tries to minimize the number of multiplications of
x(using squares whenever possible).- Parameters:
x- theBigDecimalvalue to take to the powery- thelongvalue to serve as exponentmathContext- theMathContextused for the result- Returns:
- the calculated x to the power of y with the precision specified in the
mathContext - Throws:
ArithmeticException- if y is negative and the result is inexact but the rounding mode isUNNECESSARYormc.precision == 0and the quotient has a non-terminating decimal expansion.ArithmeticException- if the rounding mode isUNNECESSARYand theBigDecimaloperation would require rounding.
-
sqrt
Calculates the square root ofBigDecimalx.- Parameters:
x- theBigDecimalvalue to calculate the square rootmathContext- theMathContextused for the result- Returns:
- the calculated square root of x with the precision specified in the
mathContext - Throws:
ArithmeticException- if x < 0UnsupportedOperationException- if theMathContexthas unlimited precision
-
log
Calculates the natural logarithm ofBigDecimalx.- Parameters:
x- theBigDecimalto calculate the natural logarithm formathContext- theMathContextused for the result- Returns:
- the calculated natural logarithm
BigDecimalwith the precision specified in themathContext - Throws:
ArithmeticException- if x <= 0UnsupportedOperationException- if theMathContexthas unlimited precision
-
log2
Calculates the logarithm ofBigDecimalx to the base 2.- Parameters:
x- theBigDecimalto calculate the logarithm base 2 formathContext- theMathContextused for the result- Returns:
- the calculated natural logarithm
BigDecimalto the base 2 with the precision specified in themathContext - Throws:
ArithmeticException- if x <= 0UnsupportedOperationException- if theMathContexthas unlimited precision
-
log10
Calculates the logarithm ofBigDecimalx to the base 10.- Parameters:
x- theBigDecimalto calculate the logarithm base 10 formathContext- theMathContextused for the result- Returns:
- the calculated natural logarithm
BigDecimalto the base 10 with the precision specified in themathContext - Throws:
ArithmeticException- if x <= 0UnsupportedOperationException- if theMathContexthas unlimited precision
-
e
Returns the number e.- Parameters:
mathContext- theMathContextused for the result- Returns:
- the number e with the precision specified in the
mathContext - Throws:
UnsupportedOperationException- if theMathContexthas unlimited precision
-
pi
Returns the number pi.See Wikipedia: Pi
- Parameters:
mathContext- theMathContextused for the result- Returns:
- the number pi with the precision specified in the
mathContext - Throws:
UnsupportedOperationException- if theMathContexthas unlimited precision
-
exp
Calculates the natural exponent ofBigDecimalx (ex).See: Wikipedia: Exponent
- Parameters:
x- theBigDecimalto calculate the exponent formathContext- theMathContextused for the result- Returns:
- the calculated exponent
BigDecimalwith the precision specified in themathContext - Throws:
UnsupportedOperationException- if theMathContexthas unlimited precision
-
sin
Calculates the sine (sinus) ofBigDecimalx.See: Wikipedia: Sine
- Parameters:
x- theBigDecimalto calculate the sine formathContext- theMathContextused for the result- Returns:
- the calculated sine
BigDecimalwith the precision specified in themathContext - Throws:
UnsupportedOperationException- if theMathContexthas unlimited precision
-
asin
Calculates the arc sine (inverted sine) ofBigDecimalx.See: Wikipedia: Arcsine
- Parameters:
x- theBigDecimalto calculate the arc sine formathContext- theMathContextused for the result- Returns:
- the calculated arc sine
BigDecimalwith the precision specified in themathContext - Throws:
ArithmeticException- if x > 1 or x < -1UnsupportedOperationException- if theMathContexthas unlimited precision
-
cos
Calculates the cosine (cosinus) ofBigDecimalx.See: Wikipedia: Cosine
- Parameters:
x- theBigDecimalto calculate the cosine formathContext- theMathContextused for the result- Returns:
- the calculated cosine
BigDecimalwith the precision specified in themathContext - Throws:
UnsupportedOperationException- if theMathContexthas unlimited precision
-
acos
Calculates the arc cosine (inverted cosine) ofBigDecimalx.See: Wikipedia: Arccosine
- Parameters:
x- theBigDecimalto calculate the arc cosine formathContext- theMathContextused for the result- Returns:
- the calculated arc sine
BigDecimalwith the precision specified in themathContext - Throws:
ArithmeticException- if x > 1 or x < -1UnsupportedOperationException- if theMathContexthas unlimited precision
-
tan
Calculates the tangens ofBigDecimalx.See: Wikipedia: Tangens
- Parameters:
x- theBigDecimalto calculate the tangens formathContext- theMathContextused for the result- Returns:
- the calculated tangens
BigDecimalwith the precision specified in themathContext - Throws:
UnsupportedOperationException- if theMathContexthas unlimited precision
-
atan
Calculates the arc tangens (inverted tangens) ofBigDecimalx.- Parameters:
x- theBigDecimalto calculate the arc tangens formathContext- theMathContextused for the result- Returns:
- the calculated arc tangens
BigDecimalwith the precision specified in themathContext - Throws:
UnsupportedOperationException- if theMathContexthas unlimited precision
-
getBigDecimal
-
getBigInteger
-