Class MathContext
java.lang.Object
org.episteme.core.mathematics.context.MathContext
Computation context for mathematical operations.
Controls precision preferences, overflow checking, and other computational settings. Can be configured globally, per-thread, or per-operation.
Usage
// Thread-local context
MathContext.setCurrent(MathContext.fast());
Real r = Real.of(3.14); // Uses float
// Computation block
Real result = MathContext.exact().compute(() -> {
Real a = Real.of("1.5");
Real b = Real.of("2.7");
return a.add(b);
});
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumOverflow checking strategystatic enum -
Method Summary
Modifier and TypeMethodDescriptionstatic voidSynonym for checkCurrentCancelled to match legacy and provider APIs.static voidChecks if the current thread's context is cancelled.<T> TExecutes a computation with this context.static MathContextexact()Creates an exact computation context (BigDecimal).static MathContextfast()Creates a fast computation context (float precision).Gets the compute mode (CPU/GPU/AUTO).static MathContextReturns the current thread-local context.Gets the java.math.MathContext.static NumericalConfigurationReturns the numerical configuration for the current thread.Gets the overflow checking mode.longReturns the precision in bits (binary digits).Gets the real number precision mode.static booleanReturns true if the current context has been cancelled.booleanReturns true if the precision is NORMAL or EXACT.booleanReturns true if the precision is FAST.booleanReturns true if the precision is EXACT.booleanReturns true if the precision is NORMAL.booleanChecks if overflow checking is enabled.static MathContextnormal()Creates a normal computation context (double precision).static voidreset()Resets the current thread-local context to defaults.static voidsetCancelled(boolean cancelled) Sets the cancellation state for the current context.static voidsetCurrent(MathContext context) Sets the current thread-local context.toString()static MathContextunsafe()Creates an unsafe context (no overflow checking).withComputeMode(ComputeMode computeMode) Returns a new context with the specified compute mode.withJavaMathContext(MathContext javaMathContext) Returns a new context with the specified java.math.MathContext.withOverflowMode(MathContext.OverflowMode overflowMode) Returns a new context with the specified overflow mode.static MathContextwithPrecision(int precision) Returns a new context with the specified precision (EXACT mode).static MathContextwithPrecision(int precision, RoundingMode rm) Returns a new context with the specified precision and rounding mode (EXACT mode).withRealPrecision(MathContext.RealPrecision realPrecision) Returns a new context with the specified real precision.
-
Method Details
-
getCurrent
Returns the current thread-local context. -
setCurrent
Sets the current thread-local context. -
reset
public static void reset()Resets the current thread-local context to defaults. -
isCancelled
public static boolean isCancelled()Returns true if the current context has been cancelled. -
setCancelled
public static void setCancelled(boolean cancelled) Sets the cancellation state for the current context. -
checkCurrentCancelled
public static void checkCurrentCancelled()Checks if the current thread's context is cancelled.- Throws:
RuntimeException- if cancelled
-
checkCancelled
public static void checkCancelled()Synonym for checkCurrentCancelled to match legacy and provider APIs. -
getNumericalConfiguration
Returns the numerical configuration for the current thread. -
fast
Creates a fast computation context (float precision). -
normal
Creates a normal computation context (double precision). -
exact
Creates an exact computation context (BigDecimal). -
unsafe
Creates an unsafe context (no overflow checking). -
compute
Executes a computation with this context. -
getRealPrecision
Gets the real number precision mode. -
isHighPrecision
public boolean isHighPrecision()Returns true if the precision is EXACT. -
isNormalPrecision
public boolean isNormalPrecision()Returns true if the precision is NORMAL. -
isDoubleOrHigherPrecision
public boolean isDoubleOrHigherPrecision()Returns true if the precision is NORMAL or EXACT. -
isFastPrecision
public boolean isFastPrecision()Returns true if the precision is FAST. -
getOverflowMode
Gets the overflow checking mode. -
isOverflowCheckingEnabled
public boolean isOverflowCheckingEnabled()Checks if overflow checking is enabled. -
getComputeMode
Gets the compute mode (CPU/GPU/AUTO). -
getJavaMathContext
Gets the java.math.MathContext. -
getPrecisionBits
public long getPrecisionBits()Returns the precision in bits (binary digits).Calculated as ceil(digits * log2(10)).
- Returns:
- the number of bits required for the current decimal precision
-
withRealPrecision
Returns a new context with the specified real precision. -
withOverflowMode
Returns a new context with the specified overflow mode. -
withComputeMode
Returns a new context with the specified compute mode. -
withJavaMathContext
Returns a new context with the specified java.math.MathContext. -
withPrecision
Returns a new context with the specified precision (EXACT mode).- Parameters:
precision- the number of digits- Returns:
- the new context
-
withPrecision
Returns a new context with the specified precision and rounding mode (EXACT mode).- Parameters:
precision- the number of digitsrm- the rounding mode- Returns:
- the new context
-
toString
-