Class BigDecimalMath
- java.lang.Object
-
- ch.obermuhlner.math.big.BigDecimalMath
-
public class BigDecimalMath extends java.lang.ObjectProvides advanced functions operating onBigDecimals.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.math.BigDecimalDOUBLE_MAX_VALUEprivate static java.math.BigDecimaleCacheprivate static java.lang.ObjecteCacheLockprivate static intEXPECTED_INITIAL_PRECISIONprivate static java.math.BigDecimal[]factorialCacheprivate static java.math.BigDecimallog10Cacheprivate static java.lang.Objectlog10CacheLockprivate static java.math.BigDecimallog2Cacheprivate static java.lang.Objectlog2CacheLockprivate static java.math.BigDecimallog3Cacheprivate static java.lang.Objectlog3CacheLockprivate static java.math.BigDecimalMINUS_ONEprivate static java.math.BigDecimalONE_HALFprivate static java.math.BigDecimalONE_HUNDRED_EIGHTYprivate static java.math.BigDecimalpiCacheprivate static java.lang.ObjectpiCacheLockprivate static java.math.BigDecimalROUGHLY_TWO_PIprivate static java.util.Map<java.lang.Integer,java.util.List<java.math.BigDecimal>>spougeFactorialConstantsCacheprivate static java.lang.ObjectspougeFactorialConstantsCacheLockprivate static java.math.BigDecimalTHREEprivate static java.math.BigDecimalTWO
-
Constructor Summary
Constructors Modifier Constructor Description privateBigDecimalMath()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.math.BigDecimalacos(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the arc cosine (inverted cosine) ofBigDecimalx.static java.math.BigDecimalacosh(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the arc hyperbolic cosine (inverse hyperbolic cosine) ofBigDecimalx.static java.math.BigDecimalacot(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the inverse cotangens (arc cotangens) ofBigDecimalx.static java.math.BigDecimalacoth(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the arc hyperbolic cotangens (inverse hyperbolic cotangens) ofBigDecimalx.private static intadjustScale(int scale, long exp)static java.math.BigDecimalasin(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the arc sine (inverted sine) ofBigDecimalx.static java.math.BigDecimalasinh(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the arc hyperbolic sine (inverse hyperbolic sine) ofBigDecimalx.static java.math.BigDecimalatan(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the arc tangens (inverted tangens) ofBigDecimalx.static java.math.BigDecimalatan2(java.math.BigDecimal y, java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the arc tangens (inverted tangens) ofBigDecimaly / x in the range -pi to pi.static java.math.BigDecimalatanh(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the arc hyperbolic tangens (inverse hyperbolic tangens) ofBigDecimalx.static java.math.BigDecimalbernoulli(int n, java.math.MathContext mathContext)Calculates the Bernoulli number for the specified index.private static voidcheckMathContext(java.math.MathContext mathContext)static java.math.BigDecimalcos(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the cosine (cosinus) ofBigDecimalx.static java.math.BigDecimalcosh(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the hyperbolic cosine ofBigDecimalx.static java.math.BigDecimalcot(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the cotangens ofBigDecimalx.static java.math.BigDecimalcoth(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the hyperbolic cotangens ofBigDecimalx.static java.math.BigDecimale(java.math.MathContext mathContext)Returns the number e.static java.math.BigDecimalexp(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the natural exponent ofBigDecimalx (ex).private static java.math.BigDecimalexpIntegralFractional(java.math.BigDecimal x, java.math.MathContext mathContext)static intexponent(java.math.BigDecimal value)Returns the exponent of the specifiedBigDecimalwritten as mantissa * 10exponent.private static java.math.BigDecimalexpTaylor(java.math.BigDecimal x, java.math.MathContext mathContext)static java.math.BigDecimalfactorial(int n)Calculates the factorial of the specified integer argument.static java.math.BigDecimalfactorial(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the factorial of the specifiedBigDecimal.private static java.math.BigDecimalfactorialLoop(int n1, int n2)private static java.math.BigDecimalfactorialRecursion(int n1, int n2)static java.math.BigDecimalfractionalPart(java.math.BigDecimal value)Returns the fractional part of the specifiedBigDecimal(right of the decimal point).static java.math.BigDecimalgamma(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the gamma function of the specifiedBigDecimal.(package private) static java.util.List<java.math.BigDecimal>getSpougeFactorialConstants(int a)static java.math.BigDecimalintegralPart(java.math.BigDecimal value)Returns the integral part of the specifiedBigDecimal(left of the decimal point).static booleanisDoubleValue(java.math.BigDecimal value)Returns whether the specifiedBigDecimalvalue can be represented asdouble.static booleanisIntValue(java.math.BigDecimal value)Returns whether the specifiedBigDecimalvalue can be represented asint.static booleanisLongValue(java.math.BigDecimal value)Returns whether the specifiedBigDecimalvalue can be represented aslong.static java.math.BigDecimallog(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the natural logarithm ofBigDecimalx.static java.math.BigDecimallog10(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the logarithm ofBigDecimalx to the base 10.static java.math.BigDecimallog2(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the logarithm ofBigDecimalx to the base 2.private static java.math.BigDecimallogTen(java.math.MathContext mathContext)private static java.math.BigDecimallogThree(java.math.MathContext mathContext)private static java.math.BigDecimallogTwo(java.math.MathContext mathContext)private static java.math.BigDecimallogUsingExponent(java.math.BigDecimal x, java.math.MathContext mathContext)private static java.math.BigDecimallogUsingNewton(java.math.BigDecimal x, java.math.MathContext mathContext)private static java.math.BigDecimallogUsingTwoThree(java.math.BigDecimal x, java.math.MathContext mathContext)static java.math.BigDecimalmantissa(java.math.BigDecimal value)Returns the mantissa of the specifiedBigDecimalwritten as mantissa * 10exponent.static java.math.BigDecimalpi(java.math.MathContext mathContext)Returns the number pi.private static java.math.BigDecimalpiChudnovski(java.math.MathContext mathContext)static java.math.BigDecimalpow(java.math.BigDecimal x, long y, java.math.MathContext mathContext)CalculatesBigDecimalx to the power oflongy (xy).static java.math.BigDecimalpow(java.math.BigDecimal x, java.math.BigDecimal y, java.math.MathContext mathContext)CalculatesBigDecimalx to the power ofBigDecimaly (xy).private static java.math.BigDecimalpowInteger(java.math.BigDecimal x, java.math.BigDecimal integerY, java.math.MathContext mathContext)CalculatesBigDecimalx to the power of the integer value y (xy).static java.math.BigDecimalreciprocal(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the reciprocal of the specifiedBigDecimal.static java.math.BigDecimalroot(java.math.BigDecimal x, java.math.BigDecimal n, java.math.MathContext mathContext)Calculates the n'th root ofBigDecimalx.private static java.math.BigDecimalrootUsingNewtonRaphson(java.math.BigDecimal x, java.math.BigDecimal n, java.math.BigDecimal initialResult, java.math.MathContext mathContext)static java.math.BigDecimalround(java.math.BigDecimal value, java.math.MathContext mathContext)Rounds the specifiedBigDecimalto the precision of the specifiedMathContext.static java.math.BigDecimalroundWithTrailingZeroes(java.math.BigDecimal value, java.math.MathContext mathContext)Rounds the specifiedBigDecimalto the precision of the specifiedMathContextincluding trailing zeroes.static intsignificantDigits(java.math.BigDecimal value)Returns the number of significant digits of the specifiedBigDecimal.static java.math.BigDecimalsin(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the sine (sinus) ofBigDecimalx.static java.math.BigDecimalsinh(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the hyperbolic sine ofBigDecimalx.static java.math.BigDecimalsqrt(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the square root ofBigDecimalx.static java.math.BigDecimaltan(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the tangens ofBigDecimalx.static java.math.BigDecimaltanh(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the hyperbolic tangens ofBigDecimalx.static java.math.BigDecimaltoBigDecimal(java.lang.String string)Creates aBigDecimalfrom the specifiedStringrepresentation.static java.math.BigDecimaltoBigDecimal(java.lang.String string, java.math.MathContext mathContext)Creates aBigDecimalfrom the specifiedStringrepresentation.(package private) static java.math.BigDecimaltoBigDecimal(java.lang.String string, java.math.MathContext mathContext, int splitLength)private static java.math.BigDecimaltoBigDecimalRecursive(char[] chars, int offset, int length, int scale, int splitLength)static java.math.BigDecimaltoDegrees(java.math.BigDecimal x, java.math.MathContext mathContext)Converts an angle measured in radians to an approximately equivalent angle measured in degrees.static java.math.BigDecimaltoRadians(java.math.BigDecimal x, java.math.MathContext mathContext)/** Converts an angle measured in degrees to an approximately equivalent angle measured in radians.
-
-
-
Field Detail
-
TWO
private static final java.math.BigDecimal TWO
-
THREE
private static final java.math.BigDecimal THREE
-
MINUS_ONE
private static final java.math.BigDecimal MINUS_ONE
-
ONE_HALF
private static final java.math.BigDecimal ONE_HALF
-
ONE_HUNDRED_EIGHTY
private static final java.math.BigDecimal ONE_HUNDRED_EIGHTY
-
DOUBLE_MAX_VALUE
private static final java.math.BigDecimal DOUBLE_MAX_VALUE
-
log2Cache
private static volatile java.math.BigDecimal log2Cache
-
log2CacheLock
private static final java.lang.Object log2CacheLock
-
log3Cache
private static volatile java.math.BigDecimal log3Cache
-
log3CacheLock
private static final java.lang.Object log3CacheLock
-
log10Cache
private static volatile java.math.BigDecimal log10Cache
-
log10CacheLock
private static final java.lang.Object log10CacheLock
-
piCache
private static volatile java.math.BigDecimal piCache
-
piCacheLock
private static final java.lang.Object piCacheLock
-
eCache
private static volatile java.math.BigDecimal eCache
-
eCacheLock
private static final java.lang.Object eCacheLock
-
ROUGHLY_TWO_PI
private static final java.math.BigDecimal ROUGHLY_TWO_PI
-
EXPECTED_INITIAL_PRECISION
private static final int EXPECTED_INITIAL_PRECISION
- See Also:
- Constant Field Values
-
factorialCache
private static java.math.BigDecimal[] factorialCache
-
spougeFactorialConstantsCache
private static final java.util.Map<java.lang.Integer,java.util.List<java.math.BigDecimal>> spougeFactorialConstantsCache
-
spougeFactorialConstantsCacheLock
private static final java.lang.Object spougeFactorialConstantsCacheLock
-
-
Method Detail
-
toBigDecimal
public static java.math.BigDecimal toBigDecimal(java.lang.String string)
Creates aBigDecimalfrom the specifiedStringrepresentation.This method is equivalent to the String constructor
BigDecimal(String)but has been optimized for large strings (several thousand digits).- Parameters:
string- the String representation- Returns:
- the created
BigDecimal - Throws:
java.lang.NumberFormatException- ifstringis not a valid representation of aBigDecimal- See Also:
BigDecimal(String),toBigDecimal(String, MathContext)
-
toBigDecimal
public static java.math.BigDecimal toBigDecimal(java.lang.String string, java.math.MathContext mathContext)Creates aBigDecimalfrom the specifiedStringrepresentation.This method is equivalent to the String constructor
BigDecimal(String, MathContext)but has been optimized for large strings (several thousand digits).- Parameters:
string- the string representationmathContext- theMathContextused for the result- Returns:
- the created
BigDecimal - Throws:
java.lang.NumberFormatException- ifstringis not a valid representation of aBigDecimaljava.lang.ArithmeticException- if the result is inexact but the rounding mode isUNNECESSARY- See Also:
BigDecimal(String, MathContext),toBigDecimal(String)
-
toBigDecimal
static java.math.BigDecimal toBigDecimal(java.lang.String string, java.math.MathContext mathContext, int splitLength)
-
adjustScale
private static int adjustScale(int scale, long exp)
-
toBigDecimalRecursive
private static java.math.BigDecimal toBigDecimalRecursive(char[] chars, int offset, int length, int scale, int splitLength)
-
isIntValue
public static boolean isIntValue(java.math.BigDecimal value)
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
-
isLongValue
public static boolean isLongValue(java.math.BigDecimal value)
Returns whether the specifiedBigDecimalvalue can be represented aslong.If this returns
trueyou can callBigDecimal.longValueExact()without fear of anArithmeticException.- Parameters:
value- theBigDecimalto check- Returns:
trueif the value can be represented aslongvalue
-
isDoubleValue
public static boolean isDoubleValue(java.math.BigDecimal value)
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
public static java.math.BigDecimal mantissa(java.math.BigDecimal value)
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(BigDecimal)
-
exponent
public static int exponent(java.math.BigDecimal value)
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:
mantissa(BigDecimal)
-
significantDigits
public static int significantDigits(java.math.BigDecimal value)
Returns the number of significant digits of the specifiedBigDecimal.The result contains the number of all digits before the decimal point and all digits after the decimal point excluding trailing zeroes.
Examples:
significantDigits(new BigDecimal("12300.00"))returns 5significantDigits(new BigDecimal("1.23000"))returns 3significantDigits(new BigDecimal("0.00012300"))returns 3significantDigits(new BigDecimal("12300.4500"))returns 7
- Parameters:
value- theBigDecimal- Returns:
- the number of significant digits
- See Also:
BigDecimal.stripTrailingZeros(),BigDecimal.precision()
-
integralPart
public static java.math.BigDecimal integralPart(java.math.BigDecimal value)
Returns the integral part of the specifiedBigDecimal(left of the decimal point).- Parameters:
value- theBigDecimal- Returns:
- the integral part
- See Also:
fractionalPart(BigDecimal)
-
fractionalPart
public static java.math.BigDecimal fractionalPart(java.math.BigDecimal value)
Returns the fractional part of the specifiedBigDecimal(right of the decimal point).- Parameters:
value- theBigDecimal- Returns:
- the fractional part
- See Also:
integralPart(BigDecimal)
-
round
public static java.math.BigDecimal round(java.math.BigDecimal value, java.math.MathContext mathContext)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:
BigDecimal.round(MathContext),roundWithTrailingZeroes(BigDecimal, MathContext)
-
roundWithTrailingZeroes
public static java.math.BigDecimal roundWithTrailingZeroes(java.math.BigDecimal value, java.math.MathContext mathContext)Rounds the specifiedBigDecimalto the precision of the specifiedMathContextincluding trailing zeroes.This method is similar to
BigDecimal.round(MathContext)but does not remove the trailing zeroes.Example:
MathContext mc = new MathContext(5); System.out.println(BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.234567"), mc)); // 1.2346 System.out.println(BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("123.4567"), mc)); // 123.46 System.out.println(BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("0.001234567"), mc)); // 0.0012346 System.out.println(BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.23"), mc)); // 1.2300 System.out.println(BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.230000"), mc)); // 1.2300 System.out.println(BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("0.00123"), mc)); // 0.0012300 System.out.println(BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("0"), mc)); // 0.0000 System.out.println(BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("0.00000000"), mc)); // 0.0000- Parameters:
value- theBigDecimalto roundmathContext- theMathContextused for the result- Returns:
- the rounded
BigDecimalvalue including trailing zeroes - See Also:
BigDecimal.round(MathContext),round(BigDecimal, MathContext)
-
reciprocal
public static java.math.BigDecimal reciprocal(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the reciprocal of the specifiedBigDecimal.- Parameters:
x- theBigDecimalmathContext- theMathContextused for the result- Returns:
- the reciprocal
BigDecimal - Throws:
java.lang.ArithmeticException- if x = 0java.lang.ArithmeticException- if the result is inexact but the rounding mode isUNNECESSARYormc.precision == 0and the quotient has a non-terminating decimal expansion.
-
factorial
public static java.math.BigDecimal factorial(int n)
Calculates the factorial of the specified integer argument.factorial = 1 * 2 * 3 * ... n
- Parameters:
n- theBigDecimal- Returns:
- the factorial
BigDecimal - Throws:
java.lang.ArithmeticException- if x < 0
-
factorialLoop
private static java.math.BigDecimal factorialLoop(int n1, int n2)
-
factorialRecursion
private static java.math.BigDecimal factorialRecursion(int n1, int n2)
-
factorial
public static java.math.BigDecimal factorial(java.math.BigDecimal x, java.math.MathContext mathContext)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:
java.lang.ArithmeticException- if x is a negative integer value (-1, -2, -3, ...)java.lang.UnsupportedOperationException- if x is a non-integer value and theMathContexthas unlimited precision- See Also:
factorial(int),gamma(BigDecimal, MathContext)
-
getSpougeFactorialConstants
static java.util.List<java.math.BigDecimal> getSpougeFactorialConstants(int a)
-
gamma
public static java.math.BigDecimal gamma(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the gamma function of the specifiedBigDecimal.This implementation uses
factorial(BigDecimal, MathContext)internally, therefore the performance implications described there apply also for this method.- Parameters:
x- theBigDecimalmathContext- theMathContextused for the result- Returns:
- the gamma
BigDecimal - Throws:
java.lang.ArithmeticException- if x-1 is a negative integer value (-1, -2, -3, ...)java.lang.UnsupportedOperationException- if x is a non-integer value and theMathContexthas unlimited precision- See Also:
factorial(BigDecimal, MathContext)
-
bernoulli
public static java.math.BigDecimal bernoulli(int n, java.math.MathContext mathContext)Calculates the Bernoulli number for the specified index.This function calculates the first Bernoulli numbers and therefore
bernoulli(1)returns -0.5Note that
bernoulli(x)for all odd x > 1 returns 0- Parameters:
n- the index of the Bernoulli number to be calculated (starting at 0)mathContext- theMathContextused for the result- Returns:
- the Bernoulli number for the specified index
- Throws:
java.lang.ArithmeticException- if x < 0java.lang.ArithmeticException- if the result is inexact but the rounding mode isUNNECESSARYormc.precision == 0and the quotient has a non-terminating decimal expansion.
-
pow
public static java.math.BigDecimal pow(java.math.BigDecimal x, java.math.BigDecimal y, java.math.MathContext mathContext)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:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision- See Also:
pow(BigDecimal, long, MathContext)
-
pow
public static java.math.BigDecimal pow(java.math.BigDecimal x, long y, java.math.MathContext mathContext)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:
java.lang.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.java.lang.ArithmeticException- if the rounding mode isUNNECESSARYand theBigDecimaloperation would require rounding.
-
powInteger
private static java.math.BigDecimal powInteger(java.math.BigDecimal x, java.math.BigDecimal integerY, java.math.MathContext mathContext)CalculatesBigDecimalx to the power of the integer value y (xy).The value y MUST be an integer value.
- Parameters:
x- theBigDecimalvalue to take to the powerintegerY- theBigDecimalinteger value to serve as exponentmathContext- theMathContextused for the result- Returns:
- the calculated x to the power of y with the precision specified in the
mathContext - See Also:
pow(BigDecimal, long, MathContext)
-
sqrt
public static java.math.BigDecimal sqrt(java.math.BigDecimal x, java.math.MathContext mathContext)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:
java.lang.ArithmeticException- if x < 0java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
root
public static java.math.BigDecimal root(java.math.BigDecimal x, java.math.BigDecimal n, java.math.MathContext mathContext)Calculates the n'th root ofBigDecimalx.- Parameters:
x- theBigDecimalvalue to calculate the n'th rootn- theBigDecimaldefining the rootmathContext- theMathContextused for the result- Returns:
- the calculated n'th root of x with the precision specified in the
mathContext - Throws:
java.lang.ArithmeticException- if n <= 0java.lang.ArithmeticException- if x < 0java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
rootUsingNewtonRaphson
private static java.math.BigDecimal rootUsingNewtonRaphson(java.math.BigDecimal x, java.math.BigDecimal n, java.math.BigDecimal initialResult, java.math.MathContext mathContext)
-
log
public static java.math.BigDecimal log(java.math.BigDecimal x, java.math.MathContext mathContext)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:
java.lang.ArithmeticException- if x <= 0java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
log2
public static java.math.BigDecimal log2(java.math.BigDecimal x, java.math.MathContext mathContext)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:
java.lang.ArithmeticException- if x <= 0java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
log10
public static java.math.BigDecimal log10(java.math.BigDecimal x, java.math.MathContext mathContext)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:
java.lang.ArithmeticException- if x <= 0java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
logUsingNewton
private static java.math.BigDecimal logUsingNewton(java.math.BigDecimal x, java.math.MathContext mathContext)
-
logUsingExponent
private static java.math.BigDecimal logUsingExponent(java.math.BigDecimal x, java.math.MathContext mathContext)
-
logUsingTwoThree
private static java.math.BigDecimal logUsingTwoThree(java.math.BigDecimal x, java.math.MathContext mathContext)
-
pi
public static java.math.BigDecimal pi(java.math.MathContext mathContext)
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:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
piChudnovski
private static java.math.BigDecimal piChudnovski(java.math.MathContext mathContext)
-
e
public static java.math.BigDecimal e(java.math.MathContext mathContext)
Returns the number e.- Parameters:
mathContext- theMathContextused for the result- Returns:
- the number e with the precision specified in the
mathContext - Throws:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
logTen
private static java.math.BigDecimal logTen(java.math.MathContext mathContext)
-
logTwo
private static java.math.BigDecimal logTwo(java.math.MathContext mathContext)
-
logThree
private static java.math.BigDecimal logThree(java.math.MathContext mathContext)
-
exp
public static java.math.BigDecimal exp(java.math.BigDecimal x, java.math.MathContext mathContext)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:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
expIntegralFractional
private static java.math.BigDecimal expIntegralFractional(java.math.BigDecimal x, java.math.MathContext mathContext)
-
expTaylor
private static java.math.BigDecimal expTaylor(java.math.BigDecimal x, java.math.MathContext mathContext)
-
sin
public static java.math.BigDecimal sin(java.math.BigDecimal x, java.math.MathContext mathContext)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:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
asin
public static java.math.BigDecimal asin(java.math.BigDecimal x, java.math.MathContext mathContext)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:
java.lang.ArithmeticException- if x > 1 or x < -1java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
cos
public static java.math.BigDecimal cos(java.math.BigDecimal x, java.math.MathContext mathContext)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:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
acos
public static java.math.BigDecimal acos(java.math.BigDecimal x, java.math.MathContext mathContext)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:
java.lang.ArithmeticException- if x > 1 or x < -1java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
tan
public static java.math.BigDecimal tan(java.math.BigDecimal x, java.math.MathContext mathContext)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:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
atan
public static java.math.BigDecimal atan(java.math.BigDecimal x, java.math.MathContext mathContext)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:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
atan2
public static java.math.BigDecimal atan2(java.math.BigDecimal y, java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the arc tangens (inverted tangens) ofBigDecimaly / x in the range -pi to pi.This is useful to calculate the angle theta from the conversion of rectangular coordinates (
x,y) to polar coordinates (r, theta).See: Wikipedia: Atan2
- Parameters:
y- theBigDecimalx- theBigDecimalmathContext- theMathContextused for the result- Returns:
- the calculated arc tangens
BigDecimalwith the precision specified in themathContext - Throws:
java.lang.ArithmeticException- if x = 0 and y = 0java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
cot
public static java.math.BigDecimal cot(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the cotangens ofBigDecimalx.See: Wikipedia: Cotangens
- Parameters:
x- theBigDecimalto calculate the cotangens formathContext- theMathContextused for the result- Returns:
- the calculated cotanges
BigDecimalwith the precision specified in themathContext - Throws:
java.lang.ArithmeticException- if x = 0java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
acot
public static java.math.BigDecimal acot(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the inverse cotangens (arc cotangens) ofBigDecimalx.- Parameters:
x- theBigDecimalto calculate the arc cotangens formathContext- theMathContextused for the result- Returns:
- the calculated arc cotangens
BigDecimalwith the precision specified in themathContext - Throws:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
sinh
public static java.math.BigDecimal sinh(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the hyperbolic sine ofBigDecimalx.- Parameters:
x- theBigDecimalto calculate the hyperbolic sine formathContext- theMathContextused for the result- Returns:
- the calculated hyperbolic sine
BigDecimalwith the precision specified in themathContext - Throws:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
cosh
public static java.math.BigDecimal cosh(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the hyperbolic cosine ofBigDecimalx.- Parameters:
x- theBigDecimalto calculate the hyperbolic cosine formathContext- theMathContextused for the result- Returns:
- the calculated hyperbolic cosine
BigDecimalwith the precision specified in themathContext - Throws:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
tanh
public static java.math.BigDecimal tanh(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the hyperbolic tangens ofBigDecimalx.- Parameters:
x- theBigDecimalto calculate the hyperbolic tangens formathContext- theMathContextused for the result- Returns:
- the calculated hyperbolic tangens
BigDecimalwith the precision specified in themathContext - Throws:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
coth
public static java.math.BigDecimal coth(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the hyperbolic cotangens ofBigDecimalx.- Parameters:
x- theBigDecimalto calculate the hyperbolic cotangens formathContext- theMathContextused for the result- Returns:
- the calculated hyperbolic cotangens
BigDecimalwith the precision specified in themathContext - Throws:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
asinh
public static java.math.BigDecimal asinh(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the arc hyperbolic sine (inverse hyperbolic sine) ofBigDecimalx.- Parameters:
x- theBigDecimalto calculate the arc hyperbolic sine formathContext- theMathContextused for the result- Returns:
- the calculated arc hyperbolic sine
BigDecimalwith the precision specified in themathContext - Throws:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
acosh
public static java.math.BigDecimal acosh(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the arc hyperbolic cosine (inverse hyperbolic cosine) ofBigDecimalx.- Parameters:
x- theBigDecimalto calculate the arc hyperbolic cosine formathContext- theMathContextused for the result- Returns:
- the calculated arc hyperbolic cosine
BigDecimalwith the precision specified in themathContext - Throws:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
atanh
public static java.math.BigDecimal atanh(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the arc hyperbolic tangens (inverse hyperbolic tangens) ofBigDecimalx.- Parameters:
x- theBigDecimalto calculate the arc hyperbolic tangens formathContext- theMathContextused for the result- Returns:
- the calculated arc hyperbolic tangens
BigDecimalwith the precision specified in themathContext - Throws:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
acoth
public static java.math.BigDecimal acoth(java.math.BigDecimal x, java.math.MathContext mathContext)Calculates the arc hyperbolic cotangens (inverse hyperbolic cotangens) ofBigDecimalx.- Parameters:
x- theBigDecimalto calculate the arc hyperbolic cotangens formathContext- theMathContextused for the result- Returns:
- the calculated arc hyperbolic cotangens
BigDecimalwith the precision specified in themathContext - Throws:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
toDegrees
public static java.math.BigDecimal toDegrees(java.math.BigDecimal x, java.math.MathContext mathContext)Converts an angle measured in radians to an approximately equivalent angle measured in degrees. The conversion from radians to degrees is generally inexact, it uses the number PI with the precision specified in the mathContext.- Parameters:
x- An angle in radians.mathContext- theMathContextused for the result- Returns:
- The angle in degrees.
- Throws:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
toRadians
public static java.math.BigDecimal toRadians(java.math.BigDecimal x, java.math.MathContext mathContext)/** Converts an angle measured in degrees to an approximately equivalent angle measured in radians. The conversion from degrees to radians is generally inexact, it uses the number PI with the precision specified in the mathContext.- Parameters:
x- An angle in degrees.mathContext- theMathContextused for the result- Returns:
- The angle in radians.
- Throws:
java.lang.UnsupportedOperationException- if theMathContexthas unlimited precision
-
checkMathContext
private static void checkMathContext(java.math.MathContext mathContext)
-
-