Class BigDecimalMath
- java.lang.Object
-
- ch.obermuhlner.math.big.BigDecimalMath
-
public class BigDecimalMath extends java.lang.Object
Provides advanced functions operating onBigDecimal
s.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.math.BigDecimal
DOUBLE_MAX_VALUE
private static java.math.BigDecimal
eCache
private static java.lang.Object
eCacheLock
private static int
EXPECTED_INITIAL_PRECISION
private static java.math.BigDecimal[]
factorialCache
private static java.math.BigDecimal
log10Cache
private static java.lang.Object
log10CacheLock
private static java.math.BigDecimal
log2Cache
private static java.lang.Object
log2CacheLock
private static java.math.BigDecimal
log3Cache
private static java.lang.Object
log3CacheLock
private static java.math.BigDecimal
MINUS_ONE
private static java.math.BigDecimal
ONE_HALF
private static java.math.BigDecimal
ONE_HUNDRED_EIGHTY
private static java.math.BigDecimal
piCache
private static java.lang.Object
piCacheLock
private static java.math.BigDecimal
ROUGHLY_TWO_PI
private static java.util.Map<java.lang.Integer,java.util.List<java.math.BigDecimal>>
spougeFactorialConstantsCache
private static java.lang.Object
spougeFactorialConstantsCacheLock
private static java.math.BigDecimal
THREE
private static java.math.BigDecimal
TWO
-
Constructor Summary
Constructors Modifier Constructor Description private
BigDecimalMath()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.math.BigDecimal
acos(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the arc cosine (inverted cosine) ofBigDecimal
x.static java.math.BigDecimal
acosh(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the arc hyperbolic cosine (inverse hyperbolic cosine) ofBigDecimal
x.static java.math.BigDecimal
acot(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the inverse cotangens (arc cotangens) ofBigDecimal
x.static java.math.BigDecimal
acoth(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the arc hyperbolic cotangens (inverse hyperbolic cotangens) ofBigDecimal
x.private static int
adjustScale(int scale, long exp)
static java.math.BigDecimal
asin(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the arc sine (inverted sine) ofBigDecimal
x.static java.math.BigDecimal
asinh(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the arc hyperbolic sine (inverse hyperbolic sine) ofBigDecimal
x.static java.math.BigDecimal
atan(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the arc tangens (inverted tangens) ofBigDecimal
x.static java.math.BigDecimal
atan2(java.math.BigDecimal y, java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the arc tangens (inverted tangens) ofBigDecimal
y / x in the range -pi to pi.static java.math.BigDecimal
atanh(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the arc hyperbolic tangens (inverse hyperbolic tangens) ofBigDecimal
x.static java.math.BigDecimal
bernoulli(int n, java.math.MathContext mathContext)
Calculates the Bernoulli number for the specified index.private static void
checkMathContext(java.math.MathContext mathContext)
static java.math.BigDecimal
cos(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the cosine (cosinus) ofBigDecimal
x.static java.math.BigDecimal
cosh(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the hyperbolic cosine ofBigDecimal
x.static java.math.BigDecimal
cot(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the cotangens ofBigDecimal
x.static java.math.BigDecimal
coth(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the hyperbolic cotangens ofBigDecimal
x.static java.math.BigDecimal
e(java.math.MathContext mathContext)
Returns the number e.static java.math.BigDecimal
exp(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the natural exponent ofBigDecimal
x (ex).private static java.math.BigDecimal
expIntegralFractional(java.math.BigDecimal x, java.math.MathContext mathContext)
static int
exponent(java.math.BigDecimal value)
Returns the exponent of the specifiedBigDecimal
written as mantissa * 10exponent.private static java.math.BigDecimal
expTaylor(java.math.BigDecimal x, java.math.MathContext mathContext)
static java.math.BigDecimal
factorial(int n)
Calculates the factorial of the specified integer argument.static java.math.BigDecimal
factorial(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the factorial of the specifiedBigDecimal
.private static java.math.BigDecimal
factorialLoop(int n1, int n2)
private static java.math.BigDecimal
factorialRecursion(int n1, int n2)
static java.math.BigDecimal
fractionalPart(java.math.BigDecimal value)
Returns the fractional part of the specifiedBigDecimal
(right of the decimal point).static java.math.BigDecimal
gamma(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.BigDecimal
integralPart(java.math.BigDecimal value)
Returns the integral part of the specifiedBigDecimal
(left of the decimal point).static boolean
isDoubleValue(java.math.BigDecimal value)
Returns whether the specifiedBigDecimal
value can be represented asdouble
.static boolean
isIntValue(java.math.BigDecimal value)
Returns whether the specifiedBigDecimal
value can be represented asint
.static boolean
isLongValue(java.math.BigDecimal value)
Returns whether the specifiedBigDecimal
value can be represented aslong
.static java.math.BigDecimal
log(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the natural logarithm ofBigDecimal
x.static java.math.BigDecimal
log10(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the logarithm ofBigDecimal
x to the base 10.static java.math.BigDecimal
log2(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the logarithm ofBigDecimal
x to the base 2.private static java.math.BigDecimal
logTen(java.math.MathContext mathContext)
private static java.math.BigDecimal
logThree(java.math.MathContext mathContext)
private static java.math.BigDecimal
logTwo(java.math.MathContext mathContext)
private static java.math.BigDecimal
logUsingExponent(java.math.BigDecimal x, java.math.MathContext mathContext)
private static java.math.BigDecimal
logUsingNewton(java.math.BigDecimal x, java.math.MathContext mathContext)
private static java.math.BigDecimal
logUsingTwoThree(java.math.BigDecimal x, java.math.MathContext mathContext)
static java.math.BigDecimal
mantissa(java.math.BigDecimal value)
Returns the mantissa of the specifiedBigDecimal
written as mantissa * 10exponent.static java.math.BigDecimal
pi(java.math.MathContext mathContext)
Returns the number pi.private static java.math.BigDecimal
piChudnovski(java.math.MathContext mathContext)
static java.math.BigDecimal
pow(java.math.BigDecimal x, long y, java.math.MathContext mathContext)
CalculatesBigDecimal
x to the power oflong
y (xy).static java.math.BigDecimal
pow(java.math.BigDecimal x, java.math.BigDecimal y, java.math.MathContext mathContext)
CalculatesBigDecimal
x to the power ofBigDecimal
y (xy).private static java.math.BigDecimal
powInteger(java.math.BigDecimal x, java.math.BigDecimal integerY, java.math.MathContext mathContext)
CalculatesBigDecimal
x to the power of the integer value y (xy).static java.math.BigDecimal
reciprocal(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the reciprocal of the specifiedBigDecimal
.static java.math.BigDecimal
root(java.math.BigDecimal x, java.math.BigDecimal n, java.math.MathContext mathContext)
Calculates the n'th root ofBigDecimal
x.private static java.math.BigDecimal
rootUsingNewtonRaphson(java.math.BigDecimal x, java.math.BigDecimal n, java.math.BigDecimal initialResult, java.math.MathContext mathContext)
static java.math.BigDecimal
round(java.math.BigDecimal value, java.math.MathContext mathContext)
Rounds the specifiedBigDecimal
to the precision of the specifiedMathContext
.static java.math.BigDecimal
roundWithTrailingZeroes(java.math.BigDecimal value, java.math.MathContext mathContext)
Rounds the specifiedBigDecimal
to the precision of the specifiedMathContext
including trailing zeroes.static int
significantDigits(java.math.BigDecimal value)
Returns the number of significant digits of the specifiedBigDecimal
.static java.math.BigDecimal
sin(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the sine (sinus) ofBigDecimal
x.static java.math.BigDecimal
sinh(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the hyperbolic sine ofBigDecimal
x.static java.math.BigDecimal
sqrt(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the square root ofBigDecimal
x.static java.math.BigDecimal
tan(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the tangens ofBigDecimal
x.static java.math.BigDecimal
tanh(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the hyperbolic tangens ofBigDecimal
x.static java.math.BigDecimal
toBigDecimal(java.lang.String string)
Creates aBigDecimal
from the specifiedString
representation.static java.math.BigDecimal
toBigDecimal(java.lang.String string, java.math.MathContext mathContext)
Creates aBigDecimal
from the specifiedString
representation.(package private) static java.math.BigDecimal
toBigDecimal(java.lang.String string, java.math.MathContext mathContext, int splitLength)
private static java.math.BigDecimal
toBigDecimalRecursive(char[] chars, int offset, int length, int scale, int splitLength)
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.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.
-
-
-
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 aBigDecimal
from the specifiedString
representation.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
- ifstring
is 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 aBigDecimal
from the specifiedString
representation.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
- theMathContext
used for the result- Returns:
- the created
BigDecimal
- Throws:
java.lang.NumberFormatException
- ifstring
is not a valid representation of aBigDecimal
java.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 specifiedBigDecimal
value can be represented asint
.If this returns
true
you can callBigDecimal.intValueExact()
without fear of anArithmeticException
.- Parameters:
value
- theBigDecimal
to check- Returns:
true
if the value can be represented asint
value
-
isLongValue
public static boolean isLongValue(java.math.BigDecimal value)
Returns whether the specifiedBigDecimal
value can be represented aslong
.If this returns
true
you can callBigDecimal.longValueExact()
without fear of anArithmeticException
.- Parameters:
value
- theBigDecimal
to check- Returns:
true
if the value can be represented aslong
value
-
isDoubleValue
public static boolean isDoubleValue(java.math.BigDecimal value)
Returns whether the specifiedBigDecimal
value can be represented asdouble
.If this returns
true
you can callBigDecimal.doubleValue()
without fear of gettingDouble.POSITIVE_INFINITY
orDouble.NEGATIVE_INFINITY
as 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 returntrue
although this value is smaller thanDouble.MIN_VALUE
(and therefore outside the range of values that can be represented asdouble
) becausenew BigDecimal("1E-325").doubleValue()
returns0
which is a legal value with loss of precision.- Parameters:
value
- theBigDecimal
to check- Returns:
true
if the value can be represented asdouble
value
-
mantissa
public static java.math.BigDecimal mantissa(java.math.BigDecimal value)
Returns the mantissa of the specifiedBigDecimal
written 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 specifiedBigDecimal
written 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 specifiedBigDecimal
to the precision of the specifiedMathContext
.This method calls
BigDecimal.round(MathContext)
.- Parameters:
value
- theBigDecimal
to roundmathContext
- theMathContext
used for the result- Returns:
- the rounded
BigDecimal
value - 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 specifiedBigDecimal
to the precision of the specifiedMathContext
including 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
- theBigDecimal
to roundmathContext
- theMathContext
used for the result- Returns:
- the rounded
BigDecimal
value 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
- theBigDecimal
mathContext
- theMathContext
used 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 isUNNECESSARY
ormc.precision == 0
and 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
- theBigDecimal
mathContext
- theMathContext
used 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 theMathContext
has 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
- theBigDecimal
mathContext
- theMathContext
used 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 theMathContext
has 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
- theMathContext
used 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 isUNNECESSARY
ormc.precision == 0
and 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)
CalculatesBigDecimal
x to the power ofBigDecimal
y (xy).- Parameters:
x
- theBigDecimal
value to take to the powery
- theBigDecimal
value to serve as exponentmathContext
- theMathContext
used for the result- Returns:
- the calculated x to the power of y with the precision specified in the
mathContext
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has 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)
CalculatesBigDecimal
x to the power oflong
y (xy).The implementation tries to minimize the number of multiplications of
x
(using squares whenever possible).- Parameters:
x
- theBigDecimal
value to take to the powery
- thelong
value to serve as exponentmathContext
- theMathContext
used 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 isUNNECESSARY
ormc.precision == 0
and the quotient has a non-terminating decimal expansion.java.lang.ArithmeticException
- if the rounding mode isUNNECESSARY
and theBigDecimal
operation would require rounding.
-
powInteger
private static java.math.BigDecimal powInteger(java.math.BigDecimal x, java.math.BigDecimal integerY, java.math.MathContext mathContext)
CalculatesBigDecimal
x to the power of the integer value y (xy).The value y MUST be an integer value.
- Parameters:
x
- theBigDecimal
value to take to the powerintegerY
- theBigDecimal
integer value to serve as exponentmathContext
- theMathContext
used 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 ofBigDecimal
x.- Parameters:
x
- theBigDecimal
value to calculate the square rootmathContext
- theMathContext
used 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 theMathContext
has 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 ofBigDecimal
x.- Parameters:
x
- theBigDecimal
value to calculate the n'th rootn
- theBigDecimal
defining the rootmathContext
- theMathContext
used 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 theMathContext
has 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 ofBigDecimal
x.- Parameters:
x
- theBigDecimal
to calculate the natural logarithm formathContext
- theMathContext
used for the result- Returns:
- the calculated natural logarithm
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.ArithmeticException
- if x <= 0java.lang.UnsupportedOperationException
- if theMathContext
has unlimited precision
-
log2
public static java.math.BigDecimal log2(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the logarithm ofBigDecimal
x to the base 2.- Parameters:
x
- theBigDecimal
to calculate the logarithm base 2 formathContext
- theMathContext
used for the result- Returns:
- the calculated natural logarithm
BigDecimal
to the base 2 with the precision specified in themathContext
- Throws:
java.lang.ArithmeticException
- if x <= 0java.lang.UnsupportedOperationException
- if theMathContext
has unlimited precision
-
log10
public static java.math.BigDecimal log10(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the logarithm ofBigDecimal
x to the base 10.- Parameters:
x
- theBigDecimal
to calculate the logarithm base 10 formathContext
- theMathContext
used for the result- Returns:
- the calculated natural logarithm
BigDecimal
to the base 10 with the precision specified in themathContext
- Throws:
java.lang.ArithmeticException
- if x <= 0java.lang.UnsupportedOperationException
- if theMathContext
has 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
- theMathContext
used for the result- Returns:
- the number pi with the precision specified in the
mathContext
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has 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
- theMathContext
used for the result- Returns:
- the number e with the precision specified in the
mathContext
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has 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 ofBigDecimal
x (ex).See: Wikipedia: Exponent
- Parameters:
x
- theBigDecimal
to calculate the exponent formathContext
- theMathContext
used for the result- Returns:
- the calculated exponent
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has 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) ofBigDecimal
x.See: Wikipedia: Sine
- Parameters:
x
- theBigDecimal
to calculate the sine formathContext
- theMathContext
used for the result- Returns:
- the calculated sine
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has unlimited precision
-
asin
public static java.math.BigDecimal asin(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the arc sine (inverted sine) ofBigDecimal
x.See: Wikipedia: Arcsine
- Parameters:
x
- theBigDecimal
to calculate the arc sine formathContext
- theMathContext
used for the result- Returns:
- the calculated arc sine
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.ArithmeticException
- if x > 1 or x < -1java.lang.UnsupportedOperationException
- if theMathContext
has unlimited precision
-
cos
public static java.math.BigDecimal cos(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the cosine (cosinus) ofBigDecimal
x.See: Wikipedia: Cosine
- Parameters:
x
- theBigDecimal
to calculate the cosine formathContext
- theMathContext
used for the result- Returns:
- the calculated cosine
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has unlimited precision
-
acos
public static java.math.BigDecimal acos(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the arc cosine (inverted cosine) ofBigDecimal
x.See: Wikipedia: Arccosine
- Parameters:
x
- theBigDecimal
to calculate the arc cosine formathContext
- theMathContext
used for the result- Returns:
- the calculated arc sine
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.ArithmeticException
- if x > 1 or x < -1java.lang.UnsupportedOperationException
- if theMathContext
has unlimited precision
-
tan
public static java.math.BigDecimal tan(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the tangens ofBigDecimal
x.See: Wikipedia: Tangens
- Parameters:
x
- theBigDecimal
to calculate the tangens formathContext
- theMathContext
used for the result- Returns:
- the calculated tangens
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has unlimited precision
-
atan
public static java.math.BigDecimal atan(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the arc tangens (inverted tangens) ofBigDecimal
x.- Parameters:
x
- theBigDecimal
to calculate the arc tangens formathContext
- theMathContext
used for the result- Returns:
- the calculated arc tangens
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has 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) ofBigDecimal
y / 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
- theBigDecimal
x
- theBigDecimal
mathContext
- theMathContext
used for the result- Returns:
- the calculated arc tangens
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.ArithmeticException
- if x = 0 and y = 0java.lang.UnsupportedOperationException
- if theMathContext
has unlimited precision
-
cot
public static java.math.BigDecimal cot(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the cotangens ofBigDecimal
x.See: Wikipedia: Cotangens
- Parameters:
x
- theBigDecimal
to calculate the cotangens formathContext
- theMathContext
used for the result- Returns:
- the calculated cotanges
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.ArithmeticException
- if x = 0java.lang.UnsupportedOperationException
- if theMathContext
has unlimited precision
-
acot
public static java.math.BigDecimal acot(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the inverse cotangens (arc cotangens) ofBigDecimal
x.- Parameters:
x
- theBigDecimal
to calculate the arc cotangens formathContext
- theMathContext
used for the result- Returns:
- the calculated arc cotangens
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has unlimited precision
-
sinh
public static java.math.BigDecimal sinh(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the hyperbolic sine ofBigDecimal
x.- Parameters:
x
- theBigDecimal
to calculate the hyperbolic sine formathContext
- theMathContext
used for the result- Returns:
- the calculated hyperbolic sine
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has unlimited precision
-
cosh
public static java.math.BigDecimal cosh(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the hyperbolic cosine ofBigDecimal
x.- Parameters:
x
- theBigDecimal
to calculate the hyperbolic cosine formathContext
- theMathContext
used for the result- Returns:
- the calculated hyperbolic cosine
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has unlimited precision
-
tanh
public static java.math.BigDecimal tanh(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the hyperbolic tangens ofBigDecimal
x.- Parameters:
x
- theBigDecimal
to calculate the hyperbolic tangens formathContext
- theMathContext
used for the result- Returns:
- the calculated hyperbolic tangens
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has unlimited precision
-
coth
public static java.math.BigDecimal coth(java.math.BigDecimal x, java.math.MathContext mathContext)
Calculates the hyperbolic cotangens ofBigDecimal
x.- Parameters:
x
- theBigDecimal
to calculate the hyperbolic cotangens formathContext
- theMathContext
used for the result- Returns:
- the calculated hyperbolic cotangens
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has 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) ofBigDecimal
x.- Parameters:
x
- theBigDecimal
to calculate the arc hyperbolic sine formathContext
- theMathContext
used for the result- Returns:
- the calculated arc hyperbolic sine
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has 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) ofBigDecimal
x.- Parameters:
x
- theBigDecimal
to calculate the arc hyperbolic cosine formathContext
- theMathContext
used for the result- Returns:
- the calculated arc hyperbolic cosine
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has 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) ofBigDecimal
x.- Parameters:
x
- theBigDecimal
to calculate the arc hyperbolic tangens formathContext
- theMathContext
used for the result- Returns:
- the calculated arc hyperbolic tangens
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has 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) ofBigDecimal
x.- Parameters:
x
- theBigDecimal
to calculate the arc hyperbolic cotangens formathContext
- theMathContext
used for the result- Returns:
- the calculated arc hyperbolic cotangens
BigDecimal
with the precision specified in themathContext
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has 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
- theMathContext
used for the result- Returns:
- The angle in degrees.
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has 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
- theMathContext
used for the result- Returns:
- The angle in radians.
- Throws:
java.lang.UnsupportedOperationException
- if theMathContext
has unlimited precision
-
checkMathContext
private static void checkMathContext(java.math.MathContext mathContext)
-
-