public class DefaultBigDecimalMath
extends java.lang.Object
BigDecimalMath that passes a current MathContext to the
functions that need a MathContext argument.
The initial default MathContext is equivalent to MathContext.DECIMAL128
but this can be overridden by setting the following system properties:
ch.obermuhlner.math.big.default.precision to a positive integer precision (default=34)ch.obermuhlner.math.big.default.rounding to a RoundingMode name (default=HALF_UP) It is also possible to programmatically set the default MathContext using setDefaultMathContext(MathContext).
It is recommended to set the desired precision in the MathContext very early in the startup of the application and to not change it afterwards.
Important: Avoid the pitfall of setting the precision temporarily using setDefaultMathContext(MathContext) for a calculation.
This can lead to race conditions and calculations with the wrong precision
if other threads in your application do the same thing.
To set a temporary MathContext you have to choice to use either:
DefaultBigDecimalMath.createLocalMathContext() in a try-with-resources statementDefaultBigDecimalMath.withLocalMathContext() with a lambda functionDefaultBigDecimalMath.createLocalMathContext():
System.out.println("Pi[default]: " + DefaultBigDecimalMath.pi());
try (DefaultBigDecimalMath.LocalMathContext context = DefaultBigDecimalMath.createLocalMathContext(5)) {
System.out.println("Pi[5]: " + DefaultBigDecimalMath.pi());
try (DefaultBigDecimalMath.LocalMathContext context2 = DefaultBigDecimalMath.createLocalMathContext(10)) {
System.out.println("Pi[10]: " + DefaultBigDecimalMath.pi());
}
System.out.println("Pi[5]: " + DefaultBigDecimalMath.pi());
}
System.out.println("Pi[default]: " + DefaultBigDecimalMath.pi());
Example code using DefaultBigDecimalMath.withLocalMathContext():
System.out.println("Pi[default]: " + DefaultBigDecimalMath.pi());
DefaultBigDecimalMath.withPrecision(5, () -> {
System.out.println("Pi[5]: " + DefaultBigDecimalMath.pi());
DefaultBigDecimalMath.withPrecision(10, () -> {
System.out.println("Pi[10]: " + DefaultBigDecimalMath.pi());
});
System.out.println("Pi[5]: " + DefaultBigDecimalMath.pi());
});
System.out.println("Pi[default]: " + DefaultBigDecimalMath.pi());
Both snippets with give the following ouput:
Pi[default]: 3.141592653589793238462643383279503 Pi[5]: 3.1416 Pi[10]: 3.141592654 Pi[5]: 3.1416 Pi[default]: 3.141592653589793238462643383279503
The temporary MathContext are stored in ThreadLocal variables
and will therefore not conflict with each other when used in multi-threaded use case.
Important: Due to the ThreadLocal variables the local MathContext will
not be available in other threads.
This includes streams using parallel(), thread pools and manually started threads.
If you need temporary MathContext for calculations then you must
set the local MathContext inside every separate thread.
try (DefaultBigDecimalMath.LocalMathContext context = DefaultBigDecimalMath.createLocalMathContext(5)) {
BigDecimalStream.range(0.0, 1.0, 0.01, DefaultBigDecimalMath.currentMathContext())
.map(b -> DefaultBigDecimalMath.cos(b))
.map(b -> "sequential " + Thread.currentThread().getName() + " [5]: " + b)
.forEach(System.out::println);
BigDecimalStream.range(0.0, 1.0, 0.01, DefaultBigDecimalMath.currentMathContext())
.parallel()
.map(b -> {
try (DefaultBigDecimalMath.LocalMathContext context2 = DefaultBigDecimalMath.createLocalMathContext(5)) {
return DefaultBigDecimalMath.cos(b);
}
})
.map(b -> "parallel " + Thread.currentThread().getName() + " [5]: " + b)
.forEach(System.out::println);
}
| Modifier and Type | Class and Description |
|---|---|
static class |
DefaultBigDecimalMath.LocalMathContext
The local context used to push and pop a
MathContext on the stack. |
| Constructor and Description |
|---|
DefaultBigDecimalMath() |
| Modifier and Type | Method and Description |
|---|---|
static java.math.BigDecimal |
acos(java.math.BigDecimal x)
Calculates the arc cosine (inverted cosine) of
BigDecimal x using the current MathContext. |
static java.math.BigDecimal |
acosh(java.math.BigDecimal x)
Calculates the arc hyperbolic cosine (inverse hyperbolic cosine) of
BigDecimal x using the current MathContext. |
static java.math.BigDecimal |
acot(java.math.BigDecimal x)
Calculates the inverse cotangens (arc cotangens) of
BigDecimal x using the current MathContext. |
static java.math.BigDecimal |
acoth(java.math.BigDecimal x)
Calculates the arc hyperbolic cotangens (inverse hyperbolic cotangens) of
BigDecimal x using the current MathContext. |
static java.math.BigDecimal |
add(java.math.BigDecimal x,
java.math.BigDecimal y)
Returns the
BigDecimal that is x + y using the current MathContext. |
static java.math.BigDecimal |
asin(java.math.BigDecimal x)
Calculates the arc sine (inverted sine) of
BigDecimal x using the current MathContext. |
static java.math.BigDecimal |
asinh(java.math.BigDecimal x)
Calculates the arc hyperbolic sine (inverse hyperbolic sine) of
BigDecimal x using the current MathContext. |
static java.math.BigDecimal |
atan(java.math.BigDecimal x)
Calculates the arc tangens (inverted tangens) of
BigDecimal x using the current MathContext. |
static java.math.BigDecimal |
atan2(java.math.BigDecimal y,
java.math.BigDecimal x)
Calculates the arc tangens (inverted tangens) of
BigDecimal y / x in the range -pi to pi using the current MathContext. |
static java.math.BigDecimal |
atanh(java.math.BigDecimal x)
Calculates the arc hyperbolic tangens (inverse hyperbolic tangens) of
BigDecimal x using the current MathContext. |
static java.math.BigDecimal |
bernoulli(int n)
Calculates the Bernoulli number for the specified index using the current
MathContext. |
static java.math.BigDecimal |
cos(java.math.BigDecimal x)
Calculates the cosine (cosinus) of
BigDecimal x using the current MathContext. |
static java.math.BigDecimal |
cosh(java.math.BigDecimal x)
Calculates the hyperbolic cosine of
BigDecimal x using the current MathContext. |
static java.math.BigDecimal |
cot(java.math.BigDecimal x)
Calculates the cotangens of
BigDecimal x using the current MathContext. |
static java.math.BigDecimal |
coth(java.math.BigDecimal x)
Calculates the hyperbolic cotangens of
BigDecimal x using the current MathContext. |
static DefaultBigDecimalMath.LocalMathContext |
createLocalMathContext(int precision)
Executes the given
Runnable using the specified precision. |
static DefaultBigDecimalMath.LocalMathContext |
createLocalMathContext(int precision,
java.math.RoundingMode roundingMode)
Executes the given
Runnable using the specified precision and RoundingMode. |
static DefaultBigDecimalMath.LocalMathContext |
createLocalMathContext(java.math.MathContext mathContext)
Executes the given
Runnable using the specified MathContext. |
static java.math.MathContext |
currentMathContext()
Returns the current
MathContext used for all mathematical functions in this class. |
static java.math.BigDecimal |
divide(java.math.BigDecimal x,
java.math.BigDecimal y)
Returns the
BigDecimal that is x / y using the current MathContext. |
static java.math.BigDecimal |
e()
Returns the number e using the current
MathContext. |
static java.math.BigDecimal |
exp(java.math.BigDecimal x)
Calculates the natural exponent of
BigDecimal x (ex) using the current MathContext. |
static java.math.BigDecimal |
factorial(java.math.BigDecimal x)
Calculates the factorial of the specified
BigDecimal using the current MathContext. |
static java.math.BigDecimal |
gamma(java.math.BigDecimal x)
Calculates the gamma function of the specified
BigDecimal using the current MathContext. |
static java.math.MathContext |
getDefaultMathContext()
Returns the default
MathContext used for all mathematical functions in this class. |
static java.math.BigDecimal |
log(java.math.BigDecimal x)
Calculates the natural logarithm of
BigDecimal x using the current MathContext. |
static java.math.BigDecimal |
log10(java.math.BigDecimal x)
Calculates the logarithm of
BigDecimal x to the base 10 using the current MathContext. |
static java.math.BigDecimal |
log2(java.math.BigDecimal x)
Calculates the logarithm of
BigDecimal x to the base 2 using the current MathContext. |
static java.math.BigDecimal |
multiply(java.math.BigDecimal x,
java.math.BigDecimal y)
Returns the
BigDecimal that is x * y using the current MathContext. |
static java.math.BigDecimal |
pi()
Returns the number pi using the current
MathContext. |
static java.math.BigDecimal |
pow(java.math.BigDecimal x,
java.math.BigDecimal y)
Calculates
BigDecimal x to the power of BigDecimal y (xy) using the current MathContext. |
static java.math.BigDecimal |
pow(java.math.BigDecimal x,
long y)
Calculates
BigDecimal x to the power of long y (xy) using the current MathContext. |
static java.math.BigDecimal |
reciprocal(java.math.BigDecimal x)
Calculates the reciprocal of the specified
BigDecimal using the current MathContext. |
static java.math.BigDecimal |
remainder(java.math.BigDecimal x,
java.math.BigDecimal y)
Returns the
BigDecimal that is x % y using the current MathContext. |
static java.math.BigDecimal |
root(java.math.BigDecimal x,
java.math.BigDecimal n)
Calculates the n'th root of
BigDecimal x using the current MathContext. |
static java.math.BigDecimal |
round(java.math.BigDecimal value)
Rounds the specified
BigDecimal to the precision of the current MathContext. |
static java.math.BigDecimal |
roundWithTrailingZeroes(java.math.BigDecimal value)
Rounds the specified
BigDecimal to the precision of the current MathContext including trailing zeroes. |
static void |
setDefaultMathContext(java.math.MathContext defaultMathContext)
Sets the default
MathContext used if no other MathContext is defined using withLocalMathContext(MathContext, Runnable). |
static java.math.BigDecimal |
sin(java.math.BigDecimal x)
Calculates the sine (sinus) of
BigDecimal x using the current MathContext. |
static java.math.BigDecimal |
sinh(java.math.BigDecimal x)
Calculates the hyperbolic sine of
BigDecimal x using the current MathContext. |
static java.math.BigDecimal |
sqrt(java.math.BigDecimal x)
Calculates the square root of
BigDecimal x using the current MathContext. |
static java.math.BigDecimal |
subtract(java.math.BigDecimal x,
java.math.BigDecimal y)
Returns the
BigDecimal that is x - y using the current MathContext. |
static java.math.BigDecimal |
tan(java.math.BigDecimal x)
Calculates the tangens of
BigDecimal x using the current MathContext. |
static java.math.BigDecimal |
tanh(java.math.BigDecimal x)
Calculates the hyperbolic tangens of
BigDecimal x using the current MathContext. |
static void |
withLocalMathContext(int precision,
java.math.RoundingMode roundingMode,
java.lang.Runnable runnable)
Executes the given
Runnable using the specified precision and RoundingMode. |
static void |
withLocalMathContext(int precision,
java.lang.Runnable runnable)
Executes the given
Runnable using the specified precision. |
static void |
withLocalMathContext(java.math.MathContext mathContext,
java.lang.Runnable runnable)
Executes the given
Runnable using the specified MathContext. |
public static void setDefaultMathContext(java.math.MathContext defaultMathContext)
MathContext used if no other MathContext is defined using withLocalMathContext(MathContext, Runnable).defaultMathContext - the default MathContextcurrentMathContext(),
withLocalMathContext(int, Runnable),
withLocalMathContext(int, RoundingMode, Runnable),
withLocalMathContext(MathContext, Runnable)public static java.math.MathContext getDefaultMathContext()
MathContext used for all mathematical functions in this class.MathContextpublic static void withLocalMathContext(int precision,
java.lang.Runnable runnable)
Runnable using the specified precision.precision - the precision to use for calculations in the runnablerunnable - the Runnable to executepublic static void withLocalMathContext(int precision,
java.math.RoundingMode roundingMode,
java.lang.Runnable runnable)
Runnable using the specified precision and RoundingMode.precision - the precision to use for calculations in the runnableroundingMode - the RoundingMode to use for calculations in the runnablerunnable - the Runnable to executepublic static void withLocalMathContext(java.math.MathContext mathContext,
java.lang.Runnable runnable)
Runnable using the specified MathContext.mathContext - the MathContext to use for calculations in the runnablerunnable - the Runnable to executepublic static DefaultBigDecimalMath.LocalMathContext createLocalMathContext(int precision)
Runnable using the specified precision.precision - the precision to use for calculationsDefaultBigDecimalMath.LocalMathContext to be used in a try-with-resources statementpublic static DefaultBigDecimalMath.LocalMathContext createLocalMathContext(int precision, java.math.RoundingMode roundingMode)
Runnable using the specified precision and RoundingMode.precision - the precision to use for calculationsroundingMode - the RoundingMode to use for calculations in the runnableDefaultBigDecimalMath.LocalMathContext to be used in a try-with-resources statementpublic static DefaultBigDecimalMath.LocalMathContext createLocalMathContext(java.math.MathContext mathContext)
Runnable using the specified MathContext.mathContext - the MathContext to use for calculationsDefaultBigDecimalMath.LocalMathContext to be used in a try-with-resources statementpublic static java.math.MathContext currentMathContext()
MathContext used for all mathematical functions in this class.
The current MathContext is the last MathContext specified
using withLocalMathContext(MathContext, Runnable)
or the default MathContext if none was specified.
MathContextcurrentMathContext(),
withLocalMathContext(int, Runnable),
withLocalMathContext(int, RoundingMode, Runnable),
withLocalMathContext(MathContext, Runnable)public static java.math.BigDecimal round(java.math.BigDecimal value)
BigDecimal to the precision of the current MathContext.value - the BigDecimal to roundBigDecimal valuecurrentMathContext(),
BigDecimalMath.round(BigDecimal, MathContext)public static java.math.BigDecimal roundWithTrailingZeroes(java.math.BigDecimal value)
BigDecimal to the precision of the current MathContext including trailing zeroes.value - the BigDecimal to roundBigDecimal value including trailing zeroescurrentMathContext(),
BigDecimalMath.roundWithTrailingZeroes(BigDecimal, MathContext)public static java.math.BigDecimal add(java.math.BigDecimal x,
java.math.BigDecimal y)
BigDecimal that is x + y using the current MathContext.x - the x valuey - the y value to addBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimal.add(BigDecimal, MathContext)public static java.math.BigDecimal subtract(java.math.BigDecimal x,
java.math.BigDecimal y)
BigDecimal that is x - y using the current MathContext.x - the x valuey - the y value to subtractBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimal.subtract(BigDecimal, MathContext)public static java.math.BigDecimal multiply(java.math.BigDecimal x,
java.math.BigDecimal y)
BigDecimal that is x * y using the current MathContext.x - the x valuey - the y value to multiplyBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimal.multiply(BigDecimal, MathContext)public static java.math.BigDecimal divide(java.math.BigDecimal x,
java.math.BigDecimal y)
BigDecimal that is x / y using the current MathContext.x - the x valuey - the y value to divideBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimal.divide(BigDecimal, MathContext)public static java.math.BigDecimal remainder(java.math.BigDecimal x,
java.math.BigDecimal y)
BigDecimal that is x % y using the current MathContext.x - the x valuey - the y value to divideBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimal.remainder(BigDecimal, MathContext)public static java.math.BigDecimal reciprocal(java.math.BigDecimal x)
BigDecimal using the current MathContext.x - the BigDecimalBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.reciprocal(BigDecimal, MathContext)public static java.math.BigDecimal factorial(java.math.BigDecimal x)
BigDecimal using the current MathContext.x - the BigDecimalBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.factorial(BigDecimal, MathContext)public static java.math.BigDecimal gamma(java.math.BigDecimal x)
BigDecimal using the current MathContext.x - the BigDecimalBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.gamma(BigDecimal, MathContext)public static java.math.BigDecimal bernoulli(int n)
MathContext.n - the index of the Bernoulli number to be calculated (starting at 0)MathContextcurrentMathContext(),
BigDecimalMath.bernoulli(int, MathContext)public static java.math.BigDecimal pow(java.math.BigDecimal x,
java.math.BigDecimal y)
BigDecimal x to the power of BigDecimal y (xy) using the current MathContext.x - the BigDecimal value to take to the powery - the BigDecimal value to serve as exponentMathContextcurrentMathContext(),
BigDecimalMath.pow(BigDecimal, BigDecimal, MathContext)public static java.math.BigDecimal pow(java.math.BigDecimal x,
long y)
BigDecimal x to the power of long y (xy) using the current MathContext.x - the BigDecimal value to take to the powery - the long value to serve as exponentMathContextcurrentMathContext(),
BigDecimalMath.pow(BigDecimal, long, MathContext)public static java.math.BigDecimal sqrt(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal value to calculate the square rootMathContextcurrentMathContext(),
BigDecimalMath.sqrt(BigDecimal, MathContext)public static java.math.BigDecimal root(java.math.BigDecimal x,
java.math.BigDecimal n)
BigDecimal x using the current MathContext.x - the BigDecimal value to calculate the n'th rootn - the BigDecimal defining the rootMathContextcurrentMathContext(),
BigDecimalMath.root(BigDecimal, BigDecimal, MathContext)public static java.math.BigDecimal log(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal to calculate the natural logarithm forBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.log(BigDecimal, MathContext)public static java.math.BigDecimal log2(java.math.BigDecimal x)
BigDecimal x to the base 2 using the current MathContext.x - the BigDecimal to calculate the logarithm base 2 forBigDecimal to the base 2 with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.log2(BigDecimal, MathContext)public static java.math.BigDecimal log10(java.math.BigDecimal x)
BigDecimal x to the base 10 using the current MathContext.x - the BigDecimal to calculate the logarithm base 10 forBigDecimal to the base 10 with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.log10(BigDecimal, MathContext)public static java.math.BigDecimal pi()
MathContext.MathContextcurrentMathContext(),
BigDecimalMath.pi(MathContext)public static java.math.BigDecimal e()
MathContext.MathContextcurrentMathContext(),
BigDecimalMath.e(MathContext)public static java.math.BigDecimal exp(java.math.BigDecimal x)
BigDecimal x (ex) using the current MathContext.x - the BigDecimal to calculate the exponent forBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.exp(BigDecimal, MathContext)public static java.math.BigDecimal sin(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal to calculate the sine forBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.sin(BigDecimal, MathContext)public static java.math.BigDecimal asin(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal to calculate the arc sine forBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.asin(BigDecimal, MathContext)public static java.math.BigDecimal cos(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal to calculate the cosine forBigDecimal with the precision specified in the current MathContextpublic static java.math.BigDecimal acos(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal to calculate the arc cosine forBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.acos(BigDecimal, MathContext)public static java.math.BigDecimal tan(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal to calculate the tangens forBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.tan(BigDecimal, MathContext)public static java.math.BigDecimal atan(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal to calculate the arc tangens forBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.atan(BigDecimal, MathContext)public static java.math.BigDecimal atan2(java.math.BigDecimal y,
java.math.BigDecimal x)
BigDecimal y / x in the range -pi to pi using the current MathContext.y - the BigDecimalx - the BigDecimalBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
atan2(BigDecimal, BigDecimal)public static java.math.BigDecimal cot(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal to calculate the cotangens forBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.cot(BigDecimal, MathContext)public static java.math.BigDecimal acot(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal to calculate the arc cotangens forBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.acot(BigDecimal, MathContext)public static java.math.BigDecimal sinh(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal to calculate the hyperbolic sine forBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.sinh(BigDecimal, MathContext)public static java.math.BigDecimal cosh(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal to calculate the hyperbolic cosine forBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.cosh(BigDecimal, MathContext)public static java.math.BigDecimal tanh(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal to calculate the hyperbolic tangens forBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.tanh(BigDecimal, MathContext)public static java.math.BigDecimal coth(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal to calculate the hyperbolic cotangens forBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.coth(BigDecimal, MathContext)public static java.math.BigDecimal asinh(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal to calculate the arc hyperbolic sine forBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.asinh(BigDecimal, MathContext)public static java.math.BigDecimal acosh(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal to calculate the arc hyperbolic cosine forBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.acosh(BigDecimal, MathContext)public static java.math.BigDecimal atanh(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal to calculate the arc hyperbolic tangens forBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.atanh(BigDecimal, MathContext)public static java.math.BigDecimal acoth(java.math.BigDecimal x)
BigDecimal x using the current MathContext.x - the BigDecimal to calculate the arc hyperbolic cotangens forBigDecimal with the precision specified in the current MathContextcurrentMathContext(),
BigDecimalMath.acoth(BigDecimal, MathContext)