Class DefaultBigDecimalMath
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.precisionto a positive integer precision (default=34)ch.obermuhlner.math.big.default.roundingto aRoundingModename (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 function
DefaultBigDecimalMath.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);
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThe local context used to push and pop aMathContexton the stack. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static MathContextprivate static ThreadLocal<Deque<MathContext>> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic BigDecimalacos(BigDecimal x) Calculates the arc cosine (inverted cosine) ofBigDecimalx using the currentMathContext.static BigDecimalacosh(BigDecimal x) Calculates the arc hyperbolic cosine (inverse hyperbolic cosine) ofBigDecimalx using the currentMathContext.static BigDecimalacot(BigDecimal x) Calculates the inverse cotangens (arc cotangens) ofBigDecimalx using the currentMathContext.static BigDecimalacoth(BigDecimal x) Calculates the arc hyperbolic cotangens (inverse hyperbolic cotangens) ofBigDecimalx using the currentMathContext.static BigDecimaladd(BigDecimal x, BigDecimal y) static BigDecimalasin(BigDecimal x) Calculates the arc sine (inverted sine) ofBigDecimalx using the currentMathContext.static BigDecimalasinh(BigDecimal x) Calculates the arc hyperbolic sine (inverse hyperbolic sine) ofBigDecimalx using the currentMathContext.static BigDecimalatan(BigDecimal x) Calculates the arc tangens (inverted tangens) ofBigDecimalx using the currentMathContext.static BigDecimalatan2(BigDecimal y, BigDecimal x) Calculates the arc tangens (inverted tangens) ofBigDecimaly / x in the range -pi to pi using the currentMathContext.static BigDecimalatanh(BigDecimal x) Calculates the arc hyperbolic tangens (inverse hyperbolic tangens) ofBigDecimalx using the currentMathContext.static BigDecimalbernoulli(int n) Calculates the Bernoulli number for the specified index using the currentMathContext.static BigDecimalcos(BigDecimal x) Calculates the cosine (cosinus) ofBigDecimalx using the currentMathContext.static BigDecimalcosh(BigDecimal x) Calculates the hyperbolic cosine ofBigDecimalx using the currentMathContext.static BigDecimalcot(BigDecimal x) Calculates the cotangens ofBigDecimalx using the currentMathContext.static BigDecimalcoth(BigDecimal x) Calculates the hyperbolic cotangens ofBigDecimalx using the currentMathContext.private static MathContextcreateLocalMathContext(int precision) Executes the givenRunnableusing the specified precision.createLocalMathContext(int precision, RoundingMode roundingMode) Executes the givenRunnableusing the specified precision andRoundingMode.createLocalMathContext(MathContext mathContext) Executes the givenRunnableusing the specifiedMathContext.static MathContextReturns the currentMathContextused for all mathematical functions in this class.static BigDecimaldivide(BigDecimal x, BigDecimal y) static BigDecimale()Returns the number e using the currentMathContext.static BigDecimalexp(BigDecimal x) Calculates the natural exponent ofBigDecimalx (ex) using the currentMathContext.static BigDecimalCalculates the factorial of the specifiedBigDecimalusing the currentMathContext.static BigDecimalgamma(BigDecimal x) Calculates the gamma function of the specifiedBigDecimalusing the currentMathContext.static MathContextReturns the defaultMathContextused for all mathematical functions in this class.private static intgetIntSystemProperty(String propertyKey, int defaultValue) private static RoundingModegetRoundingModeSystemProperty(String propertyKey, RoundingMode defaultValue) static BigDecimallog(BigDecimal x) Calculates the natural logarithm ofBigDecimalx using the currentMathContext.static BigDecimallog10(BigDecimal x) Calculates the logarithm ofBigDecimalx to the base 10 using the currentMathContext.static BigDecimallog2(BigDecimal x) Calculates the logarithm ofBigDecimalx to the base 2 using the currentMathContext.static BigDecimalmultiply(BigDecimal x, BigDecimal y) static BigDecimalpi()Returns the number pi using the currentMathContext.private static MathContextstatic BigDecimalpow(BigDecimal x, long y) static BigDecimalpow(BigDecimal x, BigDecimal y) private static <T> TpropertyException(String propertyKey, String propertyValue, T defaultValue) private static voidpushMathContext(MathContext mathContext) static BigDecimalCalculates the reciprocal of the specifiedBigDecimalusing the currentMathContext.static BigDecimalremainder(BigDecimal x, BigDecimal y) static BigDecimalroot(BigDecimal x, BigDecimal n) Calculates the n'th root ofBigDecimalx using the currentMathContext.static BigDecimalround(BigDecimal value) Rounds the specifiedBigDecimalto the precision of the currentMathContext.static BigDecimalRounds the specifiedBigDecimalto the precision of the currentMathContextincluding trailing zeroes.static voidsetDefaultMathContext(MathContext defaultMathContext) Sets the defaultMathContextused if no otherMathContextis defined usingwithLocalMathContext(MathContext, Runnable).static BigDecimalsin(BigDecimal x) Calculates the sine (sinus) ofBigDecimalx using the currentMathContext.static BigDecimalsinh(BigDecimal x) Calculates the hyperbolic sine ofBigDecimalx using the currentMathContext.static BigDecimalsqrt(BigDecimal x) Calculates the square root ofBigDecimalx using the currentMathContext.static BigDecimalsubtract(BigDecimal x, BigDecimal y) static BigDecimaltan(BigDecimal x) Calculates the tangens ofBigDecimalx using the currentMathContext.static BigDecimaltanh(BigDecimal x) Calculates the hyperbolic tangens ofBigDecimalx using the currentMathContext.static voidwithLocalMathContext(int precision, Runnable runnable) Executes the givenRunnableusing the specified precision.static voidwithLocalMathContext(int precision, RoundingMode roundingMode, Runnable runnable) Executes the givenRunnableusing the specified precision andRoundingMode.static voidwithLocalMathContext(MathContext mathContext, Runnable runnable) Executes the givenRunnableusing the specifiedMathContext.
-
Field Details
-
defaultMathContext
-
mathContextStack
-
-
Constructor Details
-
DefaultBigDecimalMath
public DefaultBigDecimalMath()
-
-
Method Details
-
createDefaultMathContext
-
pushMathContext
-
popMathContext
-
getIntSystemProperty
-
getRoundingModeSystemProperty
private static RoundingMode getRoundingModeSystemProperty(String propertyKey, RoundingMode defaultValue) -
propertyException
-
setDefaultMathContext
Sets the defaultMathContextused if no otherMathContextis defined usingwithLocalMathContext(MathContext, Runnable).- Parameters:
defaultMathContext- the defaultMathContext- See Also:
-
getDefaultMathContext
Returns the defaultMathContextused for all mathematical functions in this class.- Returns:
- the default
MathContext
-
withLocalMathContext
Executes the givenRunnableusing the specified precision.- Parameters:
precision- the precision to use for calculations in therunnablerunnable- theRunnableto execute
-
withLocalMathContext
public static void withLocalMathContext(int precision, RoundingMode roundingMode, Runnable runnable) Executes the givenRunnableusing the specified precision andRoundingMode.- Parameters:
precision- the precision to use for calculations in therunnableroundingMode- theRoundingModeto use for calculations in therunnablerunnable- theRunnableto execute
-
withLocalMathContext
Executes the givenRunnableusing the specifiedMathContext.- Parameters:
mathContext- theMathContextto use for calculations in therunnablerunnable- theRunnableto execute
-
createLocalMathContext
Executes the givenRunnableusing the specified precision.- Parameters:
precision- the precision to use for calculations- Returns:
- the created
DefaultBigDecimalMath.LocalMathContextto be used in a try-with-resources statement
-
createLocalMathContext
public static DefaultBigDecimalMath.LocalMathContext createLocalMathContext(int precision, RoundingMode roundingMode) Executes the givenRunnableusing the specified precision andRoundingMode.- Parameters:
precision- the precision to use for calculationsroundingMode- theRoundingModeto use for calculations in therunnable- Returns:
- the created
DefaultBigDecimalMath.LocalMathContextto be used in a try-with-resources statement
-
createLocalMathContext
public static DefaultBigDecimalMath.LocalMathContext createLocalMathContext(MathContext mathContext) Executes the givenRunnableusing the specifiedMathContext.- Parameters:
mathContext- theMathContextto use for calculations- Returns:
- the created
DefaultBigDecimalMath.LocalMathContextto be used in a try-with-resources statement
-
currentMathContext
Returns the currentMathContextused for all mathematical functions in this class.The current
MathContextis the lastMathContextspecified usingwithLocalMathContext(MathContext, Runnable)or the defaultMathContextif none was specified.- Returns:
- the current
MathContext - See Also:
-
round
Rounds the specifiedBigDecimalto the precision of the currentMathContext.- Parameters:
value- theBigDecimalto round- Returns:
- the rounded
BigDecimalvalue - See Also:
-
roundWithTrailingZeroes
Rounds the specifiedBigDecimalto the precision of the currentMathContextincluding trailing zeroes.- Parameters:
value- theBigDecimalto round- Returns:
- the rounded
BigDecimalvalue including trailing zeroes - See Also:
-
add
- Parameters:
x- the x valuey- the y value to add- Returns:
- the resulting
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
subtract
- Parameters:
x- the x valuey- the y value to subtract- Returns:
- the resulting
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
multiply
- Parameters:
x- the x valuey- the y value to multiply- Returns:
- the resulting
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
divide
- Parameters:
x- the x valuey- the y value to divide- Returns:
- the resulting
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
remainder
- Parameters:
x- the x valuey- the y value to divide- Returns:
- the resulting
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
reciprocal
Calculates the reciprocal of the specifiedBigDecimalusing the currentMathContext.- Parameters:
x- theBigDecimal- Returns:
- the reciprocal
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
factorial
Calculates the factorial of the specifiedBigDecimalusing the currentMathContext.- Parameters:
x- theBigDecimal- Returns:
- the factorial
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
gamma
Calculates the gamma function of the specifiedBigDecimalusing the currentMathContext.- Parameters:
x- theBigDecimal- Returns:
- the gamma
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
bernoulli
Calculates the Bernoulli number for the specified index using the currentMathContext.- Parameters:
n- the index of the Bernoulli number to be calculated (starting at 0)- Returns:
- the Bernoulli number for the specified index with the precision specified in the current
MathContext - See Also:
-
pow
- Parameters:
x- theBigDecimalvalue to take to the powery- theBigDecimalvalue to serve as exponent- Returns:
- the calculated x to the power of y with the precision specified in the current
MathContext - See Also:
-
pow
- Parameters:
x- theBigDecimalvalue to take to the powery- thelongvalue to serve as exponent- Returns:
- the calculated x to the power of y with the precision specified in the current
MathContext - See Also:
-
sqrt
Calculates the square root ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalvalue to calculate the square root- Returns:
- the calculated square root of x with the precision specified in the current
MathContext - See Also:
-
root
Calculates the n'th root ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalvalue to calculate the n'th rootn- theBigDecimaldefining the root- Returns:
- the calculated n'th root of x with the precision specified in the current
MathContext - See Also:
-
log
Calculates the natural logarithm ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the natural logarithm for- Returns:
- the calculated natural logarithm
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
log2
Calculates the logarithm ofBigDecimalx to the base 2 using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the logarithm base 2 for- Returns:
- the calculated natural logarithm
BigDecimalto the base 2 with the precision specified in the currentMathContext - See Also:
-
log10
Calculates the logarithm ofBigDecimalx to the base 10 using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the logarithm base 10 for- Returns:
- the calculated natural logarithm
BigDecimalto the base 10 with the precision specified in the currentMathContext - See Also:
-
pi
Returns the number pi using the currentMathContext.- Returns:
- the number pi with the precision specified in the current
MathContext - See Also:
-
e
Returns the number e using the currentMathContext.- Returns:
- the number e with the precision specified in the current
MathContext - See Also:
-
exp
Calculates the natural exponent ofBigDecimalx (ex) using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the exponent for- Returns:
- the calculated exponent
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
sin
Calculates the sine (sinus) ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the sine for- Returns:
- the calculated sine
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
asin
Calculates the arc sine (inverted sine) ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the arc sine for- Returns:
- the calculated arc sine
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
cos
Calculates the cosine (cosinus) ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the cosine for- Returns:
- the calculated cosine
BigDecimalwith the precision specified in the currentMathContext
-
acos
Calculates the arc cosine (inverted cosine) ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the arc cosine for- Returns:
- the calculated arc sine
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
tan
Calculates the tangens ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the tangens for- Returns:
- the calculated tangens
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
atan
Calculates the arc tangens (inverted tangens) ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the arc tangens for- Returns:
- the calculated arc tangens
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
atan2
Calculates the arc tangens (inverted tangens) ofBigDecimaly / x in the range -pi to pi using the currentMathContext.- Parameters:
y- theBigDecimalx- theBigDecimal- Returns:
- the calculated arc tangens
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
cot
Calculates the cotangens ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the cotangens for- Returns:
- the calculated cotanges
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
acot
Calculates the inverse cotangens (arc cotangens) ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the arc cotangens for- Returns:
- the calculated arc cotangens
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
sinh
Calculates the hyperbolic sine ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the hyperbolic sine for- Returns:
- the calculated hyperbolic sine
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
cosh
Calculates the hyperbolic cosine ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the hyperbolic cosine for- Returns:
- the calculated hyperbolic cosine
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
tanh
Calculates the hyperbolic tangens ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the hyperbolic tangens for- Returns:
- the calculated hyperbolic tangens
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
coth
Calculates the hyperbolic cotangens ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the hyperbolic cotangens for- Returns:
- the calculated hyperbolic cotangens
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
asinh
Calculates the arc hyperbolic sine (inverse hyperbolic sine) ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the arc hyperbolic sine for- Returns:
- the calculated arc hyperbolic sine
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
acosh
Calculates the arc hyperbolic cosine (inverse hyperbolic cosine) ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the arc hyperbolic cosine for- Returns:
- the calculated arc hyperbolic cosine
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
atanh
Calculates the arc hyperbolic tangens (inverse hyperbolic tangens) ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the arc hyperbolic tangens for- Returns:
- the calculated arc hyperbolic tangens
BigDecimalwith the precision specified in the currentMathContext - See Also:
-
acoth
Calculates the arc hyperbolic cotangens (inverse hyperbolic cotangens) ofBigDecimalx using the currentMathContext.- Parameters:
x- theBigDecimalto calculate the arc hyperbolic cotangens for- Returns:
- the calculated arc hyperbolic cotangens
BigDecimalwith the precision specified in the currentMathContext - See Also:
-