Class MathContext

java.lang.Object
org.episteme.core.mathematics.context.MathContext

public final class MathContext extends Object
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);
});
  • Method Details

    • getCurrent

      public static MathContext getCurrent()
      Returns the current thread-local context.
    • setCurrent

      public static void setCurrent(MathContext context)
      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

      public static NumericalConfiguration getNumericalConfiguration()
      Returns the numerical configuration for the current thread.
    • fast

      public static MathContext fast()
      Creates a fast computation context (float precision).
    • normal

      public static MathContext normal()
      Creates a normal computation context (double precision).
    • exact

      public static MathContext exact()
      Creates an exact computation context (BigDecimal).
    • unsafe

      public static MathContext unsafe()
      Creates an unsafe context (no overflow checking).
    • compute

      public <T> T compute(Supplier<T> computation)
      Executes a computation with this context.
    • getRealPrecision

      public MathContext.RealPrecision 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

      public MathContext.OverflowMode getOverflowMode()
      Gets the overflow checking mode.
    • isOverflowCheckingEnabled

      public boolean isOverflowCheckingEnabled()
      Checks if overflow checking is enabled.
    • getComputeMode

      public ComputeMode getComputeMode()
      Gets the compute mode (CPU/GPU/AUTO).
    • getJavaMathContext

      public MathContext 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

      public MathContext withRealPrecision(MathContext.RealPrecision realPrecision)
      Returns a new context with the specified real precision.
    • withOverflowMode

      public MathContext withOverflowMode(MathContext.OverflowMode overflowMode)
      Returns a new context with the specified overflow mode.
    • withComputeMode

      public MathContext withComputeMode(ComputeMode computeMode)
      Returns a new context with the specified compute mode.
    • withJavaMathContext

      public MathContext withJavaMathContext(MathContext javaMathContext)
      Returns a new context with the specified java.math.MathContext.
    • withPrecision

      public static MathContext withPrecision(int precision)
      Returns a new context with the specified precision (EXACT mode).
      Parameters:
      precision - the number of digits
      Returns:
      the new context
    • withPrecision

      public static MathContext withPrecision(int precision, RoundingMode rm)
      Returns a new context with the specified precision and rounding mode (EXACT mode).
      Parameters:
      precision - the number of digits
      rm - the rounding mode
      Returns:
      the new context
    • toString

      public String toString()
      Overrides:
      toString in class Object